Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 1 | from test.test_support import verbose, verify, TestFailed |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 2 | import sys |
| 3 | import new |
| 4 | |
| 5 | class Eggs: |
| 6 | def get_yolks(self): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 7 | return self.yolks |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 8 | |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 9 | print 'new.module()' |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 10 | m = new.module('Spam') |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 11 | if verbose: |
| 12 | print m |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 13 | m.Eggs = Eggs |
| 14 | sys.modules['Spam'] = m |
| 15 | import Spam |
| 16 | |
| 17 | def get_more_yolks(self): |
| 18 | return self.yolks + 3 |
| 19 | |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 20 | print 'new.classobj()' |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 21 | C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks}) |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 22 | if verbose: |
| 23 | print C |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 24 | |
| 25 | def break_yolks(self): |
| 26 | self.yolks = self.yolks - 2 |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 27 | print 'new.instancemethod()' |
Guido van Rossum | 65810fe | 2006-05-26 19:12:38 +0000 | [diff] [blame] | 28 | c = C() |
| 29 | c.yolks = 3 |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 30 | im = new.instancemethod(break_yolks, c, C) |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 31 | if verbose: |
| 32 | print im |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 33 | |
Fred Drake | 64d42c5 | 2001-01-28 03:57:39 +0000 | [diff] [blame] | 34 | verify(c.get_yolks() == 3 and c.get_more_yolks() == 6, |
| 35 | 'Broken call of hand-crafted class instance') |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 36 | im() |
Fred Drake | 64d42c5 | 2001-01-28 03:57:39 +0000 | [diff] [blame] | 37 | verify(c.get_yolks() == 1 and c.get_more_yolks() == 4, |
| 38 | 'Broken call of hand-crafted instance method') |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 39 | |
Michael W. Hudson | e2749cb | 2005-03-30 16:32:10 +0000 | [diff] [blame] | 40 | im = new.instancemethod(break_yolks, c) |
| 41 | im() |
| 42 | verify(c.get_yolks() == -1) |
| 43 | try: |
| 44 | new.instancemethod(break_yolks, None) |
| 45 | except TypeError: |
| 46 | pass |
| 47 | else: |
| 48 | raise TestFailed, "dangerous instance method creation allowed" |
| 49 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame^] | 50 | # Verify that instancemethod() doesn't allow keyword args |
| 51 | try: |
| 52 | new.instancemethod(break_yolks, c, kw=1) |
| 53 | except TypeError: |
| 54 | pass |
| 55 | else: |
| 56 | raise TestFailed, "instancemethod shouldn't accept keyword args" |
| 57 | |
Barry Warsaw | dfdac1a | 2001-03-23 16:13:30 +0000 | [diff] [blame] | 58 | # 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 Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 62 | codestr = ''' |
Barry Warsaw | dfdac1a | 2001-03-23 16:13:30 +0000 | [diff] [blame] | 63 | global c |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 64 | a = 1 |
| 65 | b = 2 |
| 66 | c = a + b |
| 67 | ''' |
| 68 | |
| 69 | ccode = compile(codestr, '<string>', 'exec') |
Barry Warsaw | dfdac1a | 2001-03-23 16:13:30 +0000 | [diff] [blame] | 70 | # Jython doesn't have a __builtins__, so use a portable alternative |
| 71 | import __builtin__ |
| 72 | g = {'c': 0, '__builtins__': __builtin__} |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 73 | # this test could be more robust |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 74 | print 'new.function()' |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 75 | func = new.function(ccode, g) |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 76 | if verbose: |
| 77 | print func |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 78 | func() |
Fred Drake | 64d42c5 | 2001-01-28 03:57:39 +0000 | [diff] [blame] | 79 | verify(g['c'] == 3, |
| 80 | 'Could not create a proper function object') |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 81 | |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 82 | # test the various extended flavors of function.new |
| 83 | def f(x): |
| 84 | def g(y): |
| 85 | return x + y |
| 86 | return g |
| 87 | g = f(4) |
| 88 | new.function(f.func_code, {}, "blah") |
| 89 | g2 = new.function(g.func_code, {}, "blah", (2,), g.func_closure) |
| 90 | verify(g2() == 6) |
| 91 | g3 = new.function(g.func_code, {}, "blah", None, g.func_closure) |
| 92 | verify(g3(5) == 9) |
| 93 | def 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 | |
| 101 | test_closure(g, None, TypeError) # invalid closure |
| 102 | test_closure(g, (1,), TypeError) # non-cell in closure |
| 103 | test_closure(g, (1, 1), ValueError) # closure is wrong size |
| 104 | test_closure(f, g.func_closure, ValueError) # no closure needed |
| 105 | |
Finn Bock | d08011a | 2001-12-09 10:19:25 +0000 | [diff] [blame] | 106 | print 'new.code()' |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 107 | # bogus test of new.code() |
Barry Warsaw | dfdac1a | 2001-03-23 16:13:30 +0000 | [diff] [blame] | 108 | # Note: Jython will never have new.code() |
| 109 | if hasattr(new, 'code'): |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 110 | def f(a): pass |
Tim Peters | bf9ac4b | 2004-08-13 03:57:22 +0000 | [diff] [blame] | 111 | |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 112 | c = f.func_code |
| 113 | argcount = c.co_argcount |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 114 | kwonlyargcount = c.co_kwonlyargcount |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 115 | 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 Peters | bf9ac4b | 2004-08-13 03:57:22 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 129 | d = new.code(argcount, kwonlyargcount, |
| 130 | nlocals, stacksize, flags, codestring, |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 131 | constants, names, varnames, filename, name, |
| 132 | firstlineno, lnotab, freevars, cellvars) |
Tim Peters | bf9ac4b | 2004-08-13 03:57:22 +0000 | [diff] [blame] | 133 | |
Barry Warsaw | dfdac1a | 2001-03-23 16:13:30 +0000 | [diff] [blame] | 134 | # test backwards-compatibility version with no freevars or cellvars |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 135 | d = new.code(argcount, kwonlyargcount, |
| 136 | nlocals, stacksize, flags, codestring, |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 137 | constants, names, varnames, filename, name, |
| 138 | firstlineno, lnotab) |
Tim Peters | bf9ac4b | 2004-08-13 03:57:22 +0000 | [diff] [blame] | 139 | |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 140 | try: # this used to trigger a SystemError |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 141 | d = new.code(-argcount, kwonlyargcount, |
| 142 | nlocals, stacksize, flags, codestring, |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 143 | 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 Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 151 | d = new.code(argcount, kwonlyargcount, |
| 152 | -nlocals, stacksize, flags, codestring, |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 153 | 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 Peters | bf9ac4b | 2004-08-13 03:57:22 +0000 | [diff] [blame] | 159 | |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 160 | try: # this used to trigger a Py_FatalError! |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 161 | d = new.code(argcount, kwonlyargcount, |
| 162 | nlocals, stacksize, flags, codestring, |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 163 | 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 Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 173 | d = new.code(argcount, kwonlyargcount, |
| 174 | nlocals, stacksize, flags, codestring, |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 175 | constants, t, varnames, filename, name, |
| 176 | firstlineno, lnotab) |
| 177 | verify(type(t[0]) is S, "eek, tuple changed under us!") |
| 178 | |
Barry Warsaw | dfdac1a | 2001-03-23 16:13:30 +0000 | [diff] [blame] | 179 | if verbose: |
| 180 | print d |