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