2 from test
import test_support
4 new
= test_support
.import_module('new', deprecated
=True)
6 class NewTest(unittest
.TestCase
):
12 m
= new
.module('Spam')
14 sys
.modules
['Spam'] = m
17 def get_more_yolks(self
):
21 C
= new
.classobj('Spam', (Spam
.Eggs
,), {'get_more_yolks': get_more_yolks
})
24 c
= new
.instance(C
, {'yolks': 3})
27 self
.assertEqual(o
.__dict
__, {}, "new __dict__ should be empty")
29 o
= new
.instance(C
, None)
30 self
.assertEqual(o
.__dict
__, {}, "new __dict__ should be empty")
33 def break_yolks(self
):
34 self
.yolks
= self
.yolks
- 2
36 # new.instancemethod()
37 im
= new
.instancemethod(break_yolks
, c
, C
)
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')
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')
50 im
= new
.instancemethod(break_yolks
, c
)
52 self
.assertEqual(c
.get_yolks(), -1)
54 # Verify that dangerous instance method creation is forbidden
55 self
.assertRaises(TypeError, new
.instancemethod
, break_yolks
, None)
57 # Verify that instancemethod() doesn't allow keyword args
58 self
.assertRaises(TypeError, new
.instancemethod
, break_yolks
, c
, kw
=1)
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.
72 codestr
= "\n".join(l
.strip() for l
in codestr
.splitlines())
74 ccode
= compile(codestr
, '<string>', 'exec')
75 # Jython doesn't have a __builtins__, so use a portable alternative
77 g
= {'c': 0, '__builtins__': __builtin__
}
79 # this test could be more robust
80 func
= new
.function(ccode
, g
)
82 self
.assertEqual(g
['c'], 3, 'Could not create a proper function object')
84 def test_function(self
):
85 # test the various extended flavors of function.new
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
)
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
104 # Note: Jython will never have new.code()
105 if hasattr(new
, 'code'):
107 # bogus test of new.code()
111 argcount
= c
.co_argcount
112 nlocals
= c
.co_nlocals
113 stacksize
= c
.co_stacksize
115 codestring
= c
.co_code
116 constants
= c
.co_consts
118 varnames
= c
.co_varnames
119 filename
= c
.co_filename
121 firstlineno
= c
.co_firstlineno
123 freevars
= c
.co_freevars
124 cellvars
= c
.co_cellvars
126 d
= new
.code(argcount
, nlocals
, stacksize
, flags
, codestring
,
127 constants
, names
, varnames
, filename
, name
,
128 firstlineno
, lnotab
, freevars
, cellvars
)
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
,
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
)
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
)
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
)
150 # new.code used to be a way to mutate a tuple...
154 d
= new
.code(argcount
, nlocals
, stacksize
, flags
, codestring
,
155 constants
, t
, varnames
, filename
, name
,
157 self
.assertTrue(type(t
[0]) is S
, "eek, tuple changed under us!")
160 test_support
.run_unittest(NewTest
)
162 if __name__
== "__main__":