blob: c56f13fc42306b23ac73b8413f47b37a4214b128 [file] [log] [blame]
Barry Warsaw04f357c2002-07-23 19:04:11 +00001from test.test_support import verbose, verify
Barry Warsaw924e5d51996-12-10 16:28:53 +00002import sys
3import new
4
5class Eggs:
6 def get_yolks(self):
Guido van Rossum41360a41998-03-26 19:42:58 +00007 return self.yolks
Barry Warsaw924e5d51996-12-10 16:28:53 +00008
Barry Warsaw5e056bb1996-12-23 23:39:42 +00009print 'new.module()'
Barry Warsaw924e5d51996-12-10 16:28:53 +000010m = new.module('Spam')
Barry Warsaw5e056bb1996-12-23 23:39:42 +000011if verbose:
12 print m
Barry Warsaw924e5d51996-12-10 16:28:53 +000013m.Eggs = Eggs
14sys.modules['Spam'] = m
15import Spam
16
17def get_more_yolks(self):
18 return self.yolks + 3
19
Barry Warsaw5e056bb1996-12-23 23:39:42 +000020print 'new.classobj()'
Barry Warsaw924e5d51996-12-10 16:28:53 +000021C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks})
Barry Warsaw5e056bb1996-12-23 23:39:42 +000022if verbose:
23 print C
24print 'new.instance()'
Barry Warsaw924e5d51996-12-10 16:28:53 +000025c = new.instance(C, {'yolks': 3})
Barry Warsaw5e056bb1996-12-23 23:39:42 +000026if verbose:
27 print c
Fred Drake64d42c52001-01-28 03:57:39 +000028o = new.instance(C)
29verify(o.__dict__ == {},
30 "new __dict__ should be empty")
31del o
32o = new.instance(C, None)
33verify(o.__dict__ == {},
34 "new __dict__ should be empty")
35del o
Barry Warsaw924e5d51996-12-10 16:28:53 +000036
37def break_yolks(self):
38 self.yolks = self.yolks - 2
Barry Warsaw5e056bb1996-12-23 23:39:42 +000039print 'new.instancemethod()'
Barry Warsaw924e5d51996-12-10 16:28:53 +000040im = new.instancemethod(break_yolks, c, C)
Barry Warsaw5e056bb1996-12-23 23:39:42 +000041if verbose:
42 print im
Barry Warsaw924e5d51996-12-10 16:28:53 +000043
Fred Drake64d42c52001-01-28 03:57:39 +000044verify(c.get_yolks() == 3 and c.get_more_yolks() == 6,
45 'Broken call of hand-crafted class instance')
Barry Warsaw924e5d51996-12-10 16:28:53 +000046im()
Fred Drake64d42c52001-01-28 03:57:39 +000047verify(c.get_yolks() == 1 and c.get_more_yolks() == 4,
48 'Broken call of hand-crafted instance method')
Barry Warsaw924e5d51996-12-10 16:28:53 +000049
Barry Warsawdfdac1a2001-03-23 16:13:30 +000050# 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.
Barry Warsaw924e5d51996-12-10 16:28:53 +000054codestr = '''
Barry Warsawdfdac1a2001-03-23 16:13:30 +000055global c
Barry Warsaw924e5d51996-12-10 16:28:53 +000056a = 1
57b = 2
58c = a + b
59'''
60
61ccode = compile(codestr, '<string>', 'exec')
Barry Warsawdfdac1a2001-03-23 16:13:30 +000062# Jython doesn't have a __builtins__, so use a portable alternative
63import __builtin__
64g = {'c': 0, '__builtins__': __builtin__}
Barry Warsaw924e5d51996-12-10 16:28:53 +000065# this test could be more robust
Barry Warsaw5e056bb1996-12-23 23:39:42 +000066print 'new.function()'
Barry Warsaw924e5d51996-12-10 16:28:53 +000067func = new.function(ccode, g)
Barry Warsaw5e056bb1996-12-23 23:39:42 +000068if verbose:
69 print func
Barry Warsaw924e5d51996-12-10 16:28:53 +000070func()
Fred Drake64d42c52001-01-28 03:57:39 +000071verify(g['c'] == 3,
72 'Could not create a proper function object')
Barry Warsaw924e5d51996-12-10 16:28:53 +000073
Jeremy Hyltondf3f7932002-07-11 18:30:27 +000074# test the various extended flavors of function.new
75def f(x):
76 def g(y):
77 return x + y
78 return g
79g = f(4)
80new.function(f.func_code, {}, "blah")
81g2 = new.function(g.func_code, {}, "blah", (2,), g.func_closure)
82verify(g2() == 6)
83g3 = new.function(g.func_code, {}, "blah", None, g.func_closure)
84verify(g3(5) == 9)
85def test_closure(func, closure, exc):
86 try:
87 new.function(func.func_code, {}, "", None, closure)
88 except exc:
89 pass
90 else:
91 print "corrupt closure accepted"
92
93test_closure(g, None, TypeError) # invalid closure
94test_closure(g, (1,), TypeError) # non-cell in closure
95test_closure(g, (1, 1), ValueError) # closure is wrong size
96test_closure(f, g.func_closure, ValueError) # no closure needed
97
Finn Bockd08011a2001-12-09 10:19:25 +000098print 'new.code()'
Barry Warsaw924e5d51996-12-10 16:28:53 +000099# bogus test of new.code()
Barry Warsawdfdac1a2001-03-23 16:13:30 +0000100# Note: Jython will never have new.code()
101if hasattr(new, 'code'):
Barry Warsawdfdac1a2001-03-23 16:13:30 +0000102 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, "")
107 if verbose:
108 print d