PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_2_0/Src/Tests/compat/sbs_newtype.py

#
Python | 130 lines | 85 code | 24 blank | 21 comment | 22 complexity | 344eaf87542c575614d02381eacbb088 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Microsoft Public License. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Microsoft Public License, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Microsoft Public License.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. from common import *
  16. class o1: pass
  17. class o2: pass
  18. class o3(o2): pass
  19. class n1(object):
  20. pass
  21. class n2(object):
  22. __slots__ = ['a', 'b']
  23. class n3(object):
  24. __slots__ = ['a', 'b', 'c']
  25. class n4(n1): pass
  26. class n5(n1):
  27. __slots__ = ['e', 'f']
  28. class n6(n2): pass
  29. class n7(n2):
  30. __slots__ = ['g', 'h', 'i']
  31. class n8(object):
  32. __slots__ = ['__dict__']
  33. os = [eval("o%s" % i) for i in range(1, 4)]
  34. ns = [eval("n%s" % i) for i in range(1, 9)]
  35. alls = os + ns + [object, float]
  36. def combinators(handle, items, n):
  37. if n == 0:
  38. yield []
  39. return
  40. for i, item in enumerate(items):
  41. this = [item]
  42. for others in combinators(handle, handle(items, i), n-1):
  43. yield this + others
  44. def combinations(items, n):
  45. def skipIthItem(items, i):
  46. return items[:i] + items[i+1:]
  47. return combinators(skipIthItem, items, n)
  48. win_exception_map = {
  49. 'Cannot create a consistent method resolution' : 'mro order',
  50. 'multiple bases have instance lay-out conflict' : 'lay-out conflicit',
  51. }
  52. cli_exception_map = {
  53. 'invalid order for base classes' : 'mro order',
  54. 'can only extend one CLI or builtin type' : 'lay-out conflicit',
  55. }
  56. def get_exception_summary():
  57. exception_map = is_cli and cli_exception_map or win_exception_map
  58. for (x, y) in exception_map.items():
  59. if x in sys.exc_value.message:
  60. return y
  61. return sys.exc_value.message
  62. count = 0
  63. class test(object):
  64. def test__pass(self):
  65. global count
  66. for i in range(1, 4):
  67. for ts in combinations(alls, i):
  68. new_class = "g%s" % count
  69. count += 1
  70. base_types = ', '.join([t.__name__ for t in ts])
  71. code = "class %s(%s): pass" % (new_class, base_types)
  72. try:
  73. printwith("case", code)
  74. exec code in globals()
  75. except:
  76. printwith("same", get_exception_summary())
  77. def test__with_slots(self):
  78. global count
  79. for i in range(1, 4):
  80. for ts in combinations(alls, i):
  81. new_class = "g%s" % count
  82. count += 1
  83. base_types = ', '.join([t.__name__ for t in ts])
  84. code = "class %s(%s): __slots__ = 'abc'" % (new_class, base_types)
  85. try:
  86. printwith("case", code)
  87. exec code in globals()
  88. except:
  89. printwith("same", get_exception_summary())
  90. # this depends on the first two tests.
  91. # no good to look into the diff if the below tests still fail
  92. def test_derive_from_g(self):
  93. all_g = [x for x in dir(sys.modules[__name__]) if x[0] == 'g' and x[1:].isdigit()]
  94. for y in all_g:
  95. code = "class dg(%s): pass" % y # __slots__ = 'a'
  96. try:
  97. printwith("case", code)
  98. exec code
  99. except:
  100. printwith("same", get_exception_summary())
  101. # TODO: reduce the test case number by merging the first two like this:
  102. # if count % 2 == 0:
  103. # code = "class %s(%s): pass" % (new_class, base_types)
  104. # else:
  105. # code = "class %s(%s): __slots__ = 'abc'" % (new_class, base_types)
  106. runtests(test)