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