1 from test
.test_support
import verbose
, verify
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 # It's unclear what the semantics should be for a code object compiled at
51 # module scope, but bound and run in a function. In CPython, `c' is global
52 # (by accident?) while in Jython, `c' is local. The intent of the test
53 # clearly is to make `c' global, so let's be explicit about it.
61 ccode
= compile(codestr
, '<string>', 'exec')
62 # Jython doesn't have a __builtins__, so use a portable alternative
64 g
= {'c': 0, '__builtins__': __builtin__
}
65 # this test could be more robust
66 print 'new.function()'
67 func
= new
.function(ccode
, g
)
72 'Could not create a proper function object')
74 # test the various extended flavors of function.new
80 new
.function(f
.func_code
, {}, "blah")
81 g2
= new
.function(g
.func_code
, {}, "blah", (2,), g
.func_closure
)
83 g3
= new
.function(g
.func_code
, {}, "blah", None, g
.func_closure
)
85 def test_closure(func
, closure
, exc
):
87 new
.function(func
.func_code
, {}, "", None, closure
)
91 print "corrupt closure accepted"
93 test_closure(g
, None, TypeError) # invalid closure
94 test_closure(g
, (1,), TypeError) # non-cell in closure
95 test_closure(g
, (1, 1), ValueError) # closure is wrong size
96 test_closure(f
, g
.func_closure
, ValueError) # no closure needed
99 # bogus test of new.code()
100 # Note: Jython will never have new.code()
101 if hasattr(new
, 'code'):
102 d
= new
.code(3, 3, 3, 3, codestr
, (), (), (),
103 "<string>", "<name>", 1, "", (), ())
104 # test backwards-compatibility version with no freevars or cellvars
105 d
= new
.code(3, 3, 3, 3, codestr
, (), (), (),
106 "<string>", "<name>", 1, "")