blob: e90fd66d049e9fb0379166e2dc045853b2005d92 [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
Barry Warsaw924e5d51996-12-10 16:28:53 +000024
25def break_yolks(self):
26 self.yolks = self.yolks - 2
Barry Warsaw5e056bb1996-12-23 23:39:42 +000027print 'new.instancemethod()'
Guido van Rossum65810fe2006-05-26 19:12:38 +000028c = C()
29c.yolks = 3
Barry Warsaw924e5d51996-12-10 16:28:53 +000030im = new.instancemethod(break_yolks, c, C)
Barry Warsaw5e056bb1996-12-23 23:39:42 +000031if verbose:
32 print im
Barry Warsaw924e5d51996-12-10 16:28:53 +000033
Fred Drake64d42c52001-01-28 03:57:39 +000034verify(c.get_yolks() == 3 and c.get_more_yolks() == 6,
35 'Broken call of hand-crafted class instance')
Barry Warsaw924e5d51996-12-10 16:28:53 +000036im()
Fred Drake64d42c52001-01-28 03:57:39 +000037verify(c.get_yolks() == 1 and c.get_more_yolks() == 4,
38 'Broken call of hand-crafted instance method')
Barry Warsaw924e5d51996-12-10 16:28:53 +000039
Michael W. Hudsone2749cb2005-03-30 16:32:10 +000040im = new.instancemethod(break_yolks, c)
41im()
42verify(c.get_yolks() == -1)
43try:
44 new.instancemethod(break_yolks, None)
45except TypeError:
46 pass
47else:
48 raise TestFailed, "dangerous instance method creation allowed"
49
Thomas Wouters89f507f2006-12-13 04:49:30 +000050# Verify that instancemethod() doesn't allow keyword args
51try:
52 new.instancemethod(break_yolks, c, kw=1)
53except TypeError:
54 pass
55else:
56 raise TestFailed, "instancemethod shouldn't accept keyword args"
57
Barry Warsawdfdac1a2001-03-23 16:13:30 +000058# It's unclear what the semantics should be for a code object compiled at
59# module scope, but bound and run in a function. In CPython, `c' is global
60# (by accident?) while in Jython, `c' is local. The intent of the test
61# clearly is to make `c' global, so let's be explicit about it.
Barry Warsaw924e5d51996-12-10 16:28:53 +000062codestr = '''
Barry Warsawdfdac1a2001-03-23 16:13:30 +000063global c
Barry Warsaw924e5d51996-12-10 16:28:53 +000064a = 1
65b = 2
66c = a + b
67'''
68
69ccode = compile(codestr, '<string>', 'exec')
Barry Warsawdfdac1a2001-03-23 16:13:30 +000070# Jython doesn't have a __builtins__, so use a portable alternative
71import __builtin__
72g = {'c': 0, '__builtins__': __builtin__}
Barry Warsaw924e5d51996-12-10 16:28:53 +000073# this test could be more robust
Barry Warsaw5e056bb1996-12-23 23:39:42 +000074print 'new.function()'
Barry Warsaw924e5d51996-12-10 16:28:53 +000075func = new.function(ccode, g)
Barry Warsaw5e056bb1996-12-23 23:39:42 +000076if verbose:
77 print func
Barry Warsaw924e5d51996-12-10 16:28:53 +000078func()
Fred Drake64d42c52001-01-28 03:57:39 +000079verify(g['c'] == 3,
80 'Could not create a proper function object')
Barry Warsaw924e5d51996-12-10 16:28:53 +000081
Jeremy Hyltondf3f7932002-07-11 18:30:27 +000082# test the various extended flavors of function.new
83def f(x):
84 def g(y):
85 return x + y
86 return g
87g = f(4)
88new.function(f.func_code, {}, "blah")
89g2 = new.function(g.func_code, {}, "blah", (2,), g.func_closure)
90verify(g2() == 6)
91g3 = new.function(g.func_code, {}, "blah", None, g.func_closure)
92verify(g3(5) == 9)
93def test_closure(func, closure, exc):
94 try:
95 new.function(func.func_code, {}, "", None, closure)
96 except exc:
97 pass
98 else:
99 print "corrupt closure accepted"
100
101test_closure(g, None, TypeError) # invalid closure
102test_closure(g, (1,), TypeError) # non-cell in closure
103test_closure(g, (1, 1), ValueError) # closure is wrong size
104test_closure(f, g.func_closure, ValueError) # no closure needed
105
Finn Bockd08011a2001-12-09 10:19:25 +0000106print 'new.code()'
Barry Warsaw924e5d51996-12-10 16:28:53 +0000107# bogus test of new.code()
Barry Warsawdfdac1a2001-03-23 16:13:30 +0000108# Note: Jython will never have new.code()
109if hasattr(new, 'code'):
Michael W. Hudson60934622004-08-12 17:56:29 +0000110 def f(a): pass
Tim Petersbf9ac4b2004-08-13 03:57:22 +0000111
Michael W. Hudson60934622004-08-12 17:56:29 +0000112 c = f.func_code
113 argcount = c.co_argcount
Guido van Rossum4f72a782006-10-27 23:31:49 +0000114 kwonlyargcount = c.co_kwonlyargcount
Michael W. Hudson60934622004-08-12 17:56:29 +0000115 nlocals = c.co_nlocals
116 stacksize = c.co_stacksize
117 flags = c.co_flags
118 codestring = c.co_code
119 constants = c.co_consts
120 names = c.co_names
121 varnames = c.co_varnames
122 filename = c.co_filename
123 name = c.co_name
124 firstlineno = c.co_firstlineno
125 lnotab = c.co_lnotab
126 freevars = c.co_freevars
127 cellvars = c.co_cellvars
Tim Petersbf9ac4b2004-08-13 03:57:22 +0000128
Guido van Rossum4f72a782006-10-27 23:31:49 +0000129 d = new.code(argcount, kwonlyargcount,
130 nlocals, stacksize, flags, codestring,
Michael W. Hudson60934622004-08-12 17:56:29 +0000131 constants, names, varnames, filename, name,
132 firstlineno, lnotab, freevars, cellvars)
Tim Petersbf9ac4b2004-08-13 03:57:22 +0000133
Barry Warsawdfdac1a2001-03-23 16:13:30 +0000134 # test backwards-compatibility version with no freevars or cellvars
Guido van Rossum4f72a782006-10-27 23:31:49 +0000135 d = new.code(argcount, kwonlyargcount,
136 nlocals, stacksize, flags, codestring,
Michael W. Hudson60934622004-08-12 17:56:29 +0000137 constants, names, varnames, filename, name,
138 firstlineno, lnotab)
Tim Petersbf9ac4b2004-08-13 03:57:22 +0000139
Michael W. Hudson60934622004-08-12 17:56:29 +0000140 try: # this used to trigger a SystemError
Guido van Rossum4f72a782006-10-27 23:31:49 +0000141 d = new.code(-argcount, kwonlyargcount,
142 nlocals, stacksize, flags, codestring,
Michael W. Hudson60934622004-08-12 17:56:29 +0000143 constants, names, varnames, filename, name,
144 firstlineno, lnotab)
145 except ValueError:
146 pass
147 else:
148 raise TestFailed, "negative co_argcount didn't trigger an exception"
149
150 try: # this used to trigger a SystemError
Guido van Rossum4f72a782006-10-27 23:31:49 +0000151 d = new.code(argcount, kwonlyargcount,
152 -nlocals, stacksize, flags, codestring,
Michael W. Hudson60934622004-08-12 17:56:29 +0000153 constants, names, varnames, filename, name,
154 firstlineno, lnotab)
155 except ValueError:
156 pass
157 else:
158 raise TestFailed, "negative co_nlocals didn't trigger an exception"
Tim Petersbf9ac4b2004-08-13 03:57:22 +0000159
Michael W. Hudson60934622004-08-12 17:56:29 +0000160 try: # this used to trigger a Py_FatalError!
Guido van Rossum4f72a782006-10-27 23:31:49 +0000161 d = new.code(argcount, kwonlyargcount,
162 nlocals, stacksize, flags, codestring,
Michael W. Hudson60934622004-08-12 17:56:29 +0000163 constants, (5,), varnames, filename, name,
164 firstlineno, lnotab)
165 except TypeError:
166 pass
167 else:
168 raise TestFailed, "non-string co_name didn't trigger an exception"
169
170 # new.code used to be a way to mutate a tuple...
171 class S(str): pass
172 t = (S("ab"),)
Guido van Rossum4f72a782006-10-27 23:31:49 +0000173 d = new.code(argcount, kwonlyargcount,
174 nlocals, stacksize, flags, codestring,
Michael W. Hudson60934622004-08-12 17:56:29 +0000175 constants, t, varnames, filename, name,
176 firstlineno, lnotab)
177 verify(type(t[0]) is S, "eek, tuple changed under us!")
178
Barry Warsawdfdac1a2001-03-23 16:13:30 +0000179 if verbose:
180 print d