blob: eb7a40792dc4d3525a6f05b6a31e7c7a818c6fe4 [file] [log] [blame]
Michael W. Hudson60934622004-08-12 17:56:29 +00001from test.test_support import verbose, verify, TestFailed
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
Michael W. Hudsone2749cb2005-03-30 16:32:10 +000050im = new.instancemethod(break_yolks, c)
51im()
52verify(c.get_yolks() == -1)
53try:
54 new.instancemethod(break_yolks, None)
55except TypeError:
56 pass
57else:
58 raise TestFailed, "dangerous instance method creation allowed"
59
Georg Brandlaf4337a2006-09-30 08:43:50 +000060# Verify that instancemethod() doesn't allow keyword args
61try:
62 new.instancemethod(break_yolks, c, kw=1)
63except TypeError:
64 pass
65else:
66 raise TestFailed, "instancemethod shouldn't accept keyword args"
67
Barry Warsawdfdac1a2001-03-23 16:13:30 +000068# It's unclear what the semantics should be for a code object compiled at
69# module scope, but bound and run in a function. In CPython, `c' is global
70# (by accident?) while in Jython, `c' is local. The intent of the test
71# clearly is to make `c' global, so let's be explicit about it.
Barry Warsaw924e5d51996-12-10 16:28:53 +000072codestr = '''
Barry Warsawdfdac1a2001-03-23 16:13:30 +000073global c
Barry Warsaw924e5d51996-12-10 16:28:53 +000074a = 1
75b = 2
76c = a + b
77'''
78
79ccode = compile(codestr, '<string>', 'exec')
Barry Warsawdfdac1a2001-03-23 16:13:30 +000080# Jython doesn't have a __builtins__, so use a portable alternative
81import __builtin__
82g = {'c': 0, '__builtins__': __builtin__}
Barry Warsaw924e5d51996-12-10 16:28:53 +000083# this test could be more robust
Barry Warsaw5e056bb1996-12-23 23:39:42 +000084print 'new.function()'
Barry Warsaw924e5d51996-12-10 16:28:53 +000085func = new.function(ccode, g)
Barry Warsaw5e056bb1996-12-23 23:39:42 +000086if verbose:
87 print func
Barry Warsaw924e5d51996-12-10 16:28:53 +000088func()
Fred Drake64d42c52001-01-28 03:57:39 +000089verify(g['c'] == 3,
90 'Could not create a proper function object')
Barry Warsaw924e5d51996-12-10 16:28:53 +000091
Jeremy Hyltondf3f7932002-07-11 18:30:27 +000092# test the various extended flavors of function.new
93def f(x):
94 def g(y):
95 return x + y
96 return g
97g = f(4)
98new.function(f.func_code, {}, "blah")
99g2 = new.function(g.func_code, {}, "blah", (2,), g.func_closure)
100verify(g2() == 6)
101g3 = new.function(g.func_code, {}, "blah", None, g.func_closure)
102verify(g3(5) == 9)
103def test_closure(func, closure, exc):
104 try:
105 new.function(func.func_code, {}, "", None, closure)
106 except exc:
107 pass
108 else:
109 print "corrupt closure accepted"
110
111test_closure(g, None, TypeError) # invalid closure
112test_closure(g, (1,), TypeError) # non-cell in closure
113test_closure(g, (1, 1), ValueError) # closure is wrong size
114test_closure(f, g.func_closure, ValueError) # no closure needed
115
Finn Bockd08011a2001-12-09 10:19:25 +0000116print 'new.code()'
Barry Warsaw924e5d51996-12-10 16:28:53 +0000117# bogus test of new.code()
Barry Warsawdfdac1a2001-03-23 16:13:30 +0000118# Note: Jython will never have new.code()
119if hasattr(new, 'code'):
Michael W. Hudson60934622004-08-12 17:56:29 +0000120 def f(a): pass
Tim Petersbf9ac4b2004-08-13 03:57:22 +0000121
Michael W. Hudson60934622004-08-12 17:56:29 +0000122 c = f.func_code
123 argcount = c.co_argcount
124 nlocals = c.co_nlocals
125 stacksize = c.co_stacksize
126 flags = c.co_flags
127 codestring = c.co_code
128 constants = c.co_consts
129 names = c.co_names
130 varnames = c.co_varnames
131 filename = c.co_filename
132 name = c.co_name
133 firstlineno = c.co_firstlineno
134 lnotab = c.co_lnotab
135 freevars = c.co_freevars
136 cellvars = c.co_cellvars
Tim Petersbf9ac4b2004-08-13 03:57:22 +0000137
Michael W. Hudson60934622004-08-12 17:56:29 +0000138 d = new.code(argcount, nlocals, stacksize, flags, codestring,
139 constants, names, varnames, filename, name,
140 firstlineno, lnotab, freevars, cellvars)
Tim Petersbf9ac4b2004-08-13 03:57:22 +0000141
Barry Warsawdfdac1a2001-03-23 16:13:30 +0000142 # test backwards-compatibility version with no freevars or cellvars
Michael W. Hudson60934622004-08-12 17:56:29 +0000143 d = new.code(argcount, nlocals, stacksize, flags, codestring,
144 constants, names, varnames, filename, name,
145 firstlineno, lnotab)
Tim Petersbf9ac4b2004-08-13 03:57:22 +0000146
Michael W. Hudson60934622004-08-12 17:56:29 +0000147 try: # this used to trigger a SystemError
148 d = new.code(-argcount, nlocals, stacksize, flags, codestring,
149 constants, names, varnames, filename, name,
150 firstlineno, lnotab)
151 except ValueError:
152 pass
153 else:
154 raise TestFailed, "negative co_argcount didn't trigger an exception"
155
156 try: # this used to trigger a SystemError
157 d = new.code(argcount, -nlocals, stacksize, flags, codestring,
158 constants, names, varnames, filename, name,
159 firstlineno, lnotab)
160 except ValueError:
161 pass
162 else:
163 raise TestFailed, "negative co_nlocals didn't trigger an exception"
Tim Petersbf9ac4b2004-08-13 03:57:22 +0000164
Michael W. Hudson60934622004-08-12 17:56:29 +0000165 try: # this used to trigger a Py_FatalError!
166 d = new.code(argcount, nlocals, stacksize, flags, codestring,
167 constants, (5,), varnames, filename, name,
168 firstlineno, lnotab)
169 except TypeError:
170 pass
171 else:
172 raise TestFailed, "non-string co_name didn't trigger an exception"
173
174 # new.code used to be a way to mutate a tuple...
175 class S(str): pass
176 t = (S("ab"),)
177 d = new.code(argcount, nlocals, stacksize, flags, codestring,
178 constants, t, varnames, filename, name,
179 firstlineno, lnotab)
180 verify(type(t[0]) is S, "eek, tuple changed under us!")
181
Barry Warsawdfdac1a2001-03-23 16:13:30 +0000182 if verbose:
183 print d