test_new.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import unittest
  2. from test import test_support
  3. import sys
  4. new = test_support.import_module('new', deprecated=True)
  5. class NewTest(unittest.TestCase):
  6. def test_spam(self):
  7. class Eggs:
  8. def get_yolks(self):
  9. return self.yolks
  10. m = new.module('Spam')
  11. m.Eggs = Eggs
  12. sys.modules['Spam'] = m
  13. import Spam
  14. def get_more_yolks(self):
  15. return self.yolks + 3
  16. # new.classobj()
  17. C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks})
  18. # new.instance()
  19. c = new.instance(C, {'yolks': 3})
  20. o = new.instance(C)
  21. self.assertEqual(o.__dict__, {}, "new __dict__ should be empty")
  22. del o
  23. o = new.instance(C, None)
  24. self.assertEqual(o.__dict__, {}, "new __dict__ should be empty")
  25. del o
  26. def break_yolks(self):
  27. self.yolks = self.yolks - 2
  28. # new.instancemethod()
  29. im = new.instancemethod(break_yolks, c, C)
  30. self.assertEqual(c.get_yolks(), 3,
  31. 'Broken call of hand-crafted class instance')
  32. self.assertEqual(c.get_more_yolks(), 6,
  33. 'Broken call of hand-crafted class instance')
  34. im()
  35. self.assertEqual(c.get_yolks(), 1,
  36. 'Broken call of hand-crafted instance method')
  37. self.assertEqual(c.get_more_yolks(), 4,
  38. 'Broken call of hand-crafted instance method')
  39. im = new.instancemethod(break_yolks, c)
  40. im()
  41. self.assertEqual(c.get_yolks(), -1)
  42. # Verify that dangerous instance method creation is forbidden
  43. self.assertRaises(TypeError, new.instancemethod, break_yolks, None)
  44. # Verify that instancemethod() doesn't allow keyword args
  45. self.assertRaises(TypeError, new.instancemethod, break_yolks, c, kw=1)
  46. def test_scope(self):
  47. # It's unclear what the semantics should be for a code object compiled
  48. # at module scope, but bound and run in a function. In CPython, `c' is
  49. # global (by accident?) while in Jython, `c' is local. The intent of
  50. # the test clearly is to make `c' global, so let's be explicit about it.
  51. codestr = '''
  52. global c
  53. a = 1
  54. b = 2
  55. c = a + b
  56. '''
  57. codestr = "\n".join(l.strip() for l in codestr.splitlines())
  58. ccode = compile(codestr, '<string>', 'exec')
  59. # Jython doesn't have a __builtins__, so use a portable alternative
  60. import __builtin__
  61. g = {'c': 0, '__builtins__': __builtin__}
  62. # this test could be more robust
  63. func = new.function(ccode, g)
  64. func()
  65. self.assertEqual(g['c'], 3, 'Could not create a proper function object')
  66. def test_function(self):
  67. # test the various extended flavors of function.new
  68. def f(x):
  69. def g(y):
  70. return x + y
  71. return g
  72. g = f(4)
  73. new.function(f.func_code, {}, "blah")
  74. g2 = new.function(g.func_code, {}, "blah", (2,), g.func_closure)
  75. self.assertEqual(g2(), 6)
  76. g3 = new.function(g.func_code, {}, "blah", None, g.func_closure)
  77. self.assertEqual(g3(5), 9)
  78. def test_closure(func, closure, exc):
  79. self.assertRaises(exc, new.function, func.func_code, {}, "", None, closure)
  80. test_closure(g, None, TypeError) # invalid closure
  81. test_closure(g, (1,), TypeError) # non-cell in closure
  82. test_closure(g, (1, 1), ValueError) # closure is wrong size
  83. test_closure(f, g.func_closure, ValueError) # no closure needed
  84. # Note: Jython will never have new.code()
  85. if hasattr(new, 'code'):
  86. def test_code(self):
  87. # bogus test of new.code()
  88. def f(a): pass
  89. c = f.func_code
  90. argcount = c.co_argcount
  91. nlocals = c.co_nlocals
  92. stacksize = c.co_stacksize
  93. flags = c.co_flags
  94. codestring = c.co_code
  95. constants = c.co_consts
  96. names = c.co_names
  97. varnames = c.co_varnames
  98. filename = c.co_filename
  99. name = c.co_name
  100. firstlineno = c.co_firstlineno
  101. lnotab = c.co_lnotab
  102. freevars = c.co_freevars
  103. cellvars = c.co_cellvars
  104. d = new.code(argcount, nlocals, stacksize, flags, codestring,
  105. constants, names, varnames, filename, name,
  106. firstlineno, lnotab, freevars, cellvars)
  107. # test backwards-compatibility version with no freevars or cellvars
  108. d = new.code(argcount, nlocals, stacksize, flags, codestring,
  109. constants, names, varnames, filename, name,
  110. firstlineno, lnotab)
  111. # negative co_argcount used to trigger a SystemError
  112. self.assertRaises(ValueError, new.code,
  113. -argcount, nlocals, stacksize, flags, codestring,
  114. constants, names, varnames, filename, name, firstlineno, lnotab)
  115. # negative co_nlocals used to trigger a SystemError
  116. self.assertRaises(ValueError, new.code,
  117. argcount, -nlocals, stacksize, flags, codestring,
  118. constants, names, varnames, filename, name, firstlineno, lnotab)
  119. # non-string co_name used to trigger a Py_FatalError
  120. self.assertRaises(TypeError, new.code,
  121. argcount, nlocals, stacksize, flags, codestring,
  122. constants, (5,), varnames, filename, name, firstlineno, lnotab)
  123. # new.code used to be a way to mutate a tuple...
  124. class S(str):
  125. pass
  126. t = (S("ab"),)
  127. d = new.code(argcount, nlocals, stacksize, flags, codestring,
  128. constants, t, varnames, filename, name,
  129. firstlineno, lnotab)
  130. self.assertTrue(type(t[0]) is S, "eek, tuple changed under us!")
  131. def test_main():
  132. test_support.run_unittest(NewTest)
  133. if __name__ == "__main__":
  134. test_main()