1 from test
.test_support
import verbose
, verify
, TestFailed
10 m
= new
.module('Spam')
14 sys
.modules
['Spam'] = m
17 def get_more_yolks(self
):
20 print 'new.classobj()'
21 C
= new
.classobj('Spam', (Spam
.Eggs
,), {'get_more_yolks': get_more_yolks
})
24 print 'new.instance()'
25 c
= new
.instance(C
, {'yolks': 3})
29 verify(o
.__dict
__ == {},
30 "new __dict__ should be empty")
32 o
= new
.instance(C
, None)
33 verify(o
.__dict
__ == {},
34 "new __dict__ should be empty")
37 def break_yolks(self
):
38 self
.yolks
= self
.yolks
- 2
39 print 'new.instancemethod()'
40 im
= new
.instancemethod(break_yolks
, c
, C
)
44 verify(c
.get_yolks() == 3 and c
.get_more_yolks() == 6,
45 'Broken call of hand-crafted class instance')
47 verify(c
.get_yolks() == 1 and c
.get_more_yolks() == 4,
48 'Broken call of hand-crafted instance method')
50 im
= new
.instancemethod(break_yolks
, c
)
52 verify(c
.get_yolks() == -1)
54 new
.instancemethod(break_yolks
, None)
58 raise TestFailed
, "dangerous instance method creation allowed"
60 # It's unclear what the semantics should be for a code object compiled at
61 # module scope, but bound and run in a function. In CPython, `c' is global
62 # (by accident?) while in Jython, `c' is local. The intent of the test
63 # clearly is to make `c' global, so let's be explicit about it.
71 ccode
= compile(codestr
, '<string>', 'exec')
72 # Jython doesn't have a __builtins__, so use a portable alternative
74 g
= {'c': 0, '__builtins__': __builtin__
}
75 # this test could be more robust
76 print 'new.function()'
77 func
= new
.function(ccode
, g
)
82 'Could not create a proper function object')
84 # test the various extended flavors of function.new
90 new
.function(f
.func_code
, {}, "blah")
91 g2
= new
.function(g
.func_code
, {}, "blah", (2,), g
.func_closure
)
93 g3
= new
.function(g
.func_code
, {}, "blah", None, g
.func_closure
)
95 def test_closure(func
, closure
, exc
):
97 new
.function(func
.func_code
, {}, "", None, closure
)
101 print "corrupt closure accepted"
103 test_closure(g
, None, TypeError) # invalid closure
104 test_closure(g
, (1,), TypeError) # non-cell in closure
105 test_closure(g
, (1, 1), ValueError) # closure is wrong size
106 test_closure(f
, g
.func_closure
, ValueError) # no closure needed
109 # bogus test of new.code()
110 # Note: Jython will never have new.code()
111 if hasattr(new
, 'code'):
115 argcount
= c
.co_argcount
116 nlocals
= c
.co_nlocals
117 stacksize
= c
.co_stacksize
119 codestring
= c
.co_code
120 constants
= c
.co_consts
122 varnames
= c
.co_varnames
123 filename
= c
.co_filename
125 firstlineno
= c
.co_firstlineno
127 freevars
= c
.co_freevars
128 cellvars
= c
.co_cellvars
130 d
= new
.code(argcount
, nlocals
, stacksize
, flags
, codestring
,
131 constants
, names
, varnames
, filename
, name
,
132 firstlineno
, lnotab
, freevars
, cellvars
)
134 # test backwards-compatibility version with no freevars or cellvars
135 d
= new
.code(argcount
, nlocals
, stacksize
, flags
, codestring
,
136 constants
, names
, varnames
, filename
, name
,
139 try: # this used to trigger a SystemError
140 d
= new
.code(-argcount
, nlocals
, stacksize
, flags
, codestring
,
141 constants
, names
, varnames
, filename
, name
,
146 raise TestFailed
, "negative co_argcount didn't trigger an exception"
148 try: # this used to trigger a SystemError
149 d
= new
.code(argcount
, -nlocals
, stacksize
, flags
, codestring
,
150 constants
, names
, varnames
, filename
, name
,
155 raise TestFailed
, "negative co_nlocals didn't trigger an exception"
157 try: # this used to trigger a Py_FatalError!
158 d
= new
.code(argcount
, nlocals
, stacksize
, flags
, codestring
,
159 constants
, (5,), varnames
, filename
, name
,
164 raise TestFailed
, "non-string co_name didn't trigger an exception"
166 # new.code used to be a way to mutate a tuple...
169 d
= new
.code(argcount
, nlocals
, stacksize
, flags
, codestring
,
170 constants
, t
, varnames
, filename
, name
,
172 verify(type(t
[0]) is S
, "eek, tuple changed under us!")