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 |
| 24 | print 'new.instance()' |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 25 | c = new.instance(C, {'yolks': 3}) |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 26 | if verbose: |
| 27 | print c |
Fred Drake | 64d42c5 | 2001-01-28 03:57:39 +0000 | [diff] [blame] | 28 | o = new.instance(C) |
| 29 | verify(o.__dict__ == {}, |
| 30 | "new __dict__ should be empty") |
| 31 | del o |
| 32 | o = new.instance(C, None) |
| 33 | verify(o.__dict__ == {}, |
| 34 | "new __dict__ should be empty") |
| 35 | del o |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 36 | |
| 37 | def break_yolks(self): |
| 38 | self.yolks = self.yolks - 2 |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 39 | print 'new.instancemethod()' |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 40 | im = new.instancemethod(break_yolks, c, C) |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 41 | if verbose: |
| 42 | print im |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 43 | |
Fred Drake | 64d42c5 | 2001-01-28 03:57:39 +0000 | [diff] [blame] | 44 | verify(c.get_yolks() == 3 and c.get_more_yolks() == 6, |
| 45 | 'Broken call of hand-crafted class instance') |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 46 | im() |
Fred Drake | 64d42c5 | 2001-01-28 03:57:39 +0000 | [diff] [blame] | 47 | verify(c.get_yolks() == 1 and c.get_more_yolks() == 4, |
| 48 | 'Broken call of hand-crafted instance method') |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 49 | |
Michael W. Hudson | e2749cb | 2005-03-30 16:32:10 +0000 | [diff] [blame] | 50 | im = new.instancemethod(break_yolks, c) |
| 51 | im() |
| 52 | verify(c.get_yolks() == -1) |
| 53 | try: |
| 54 | new.instancemethod(break_yolks, None) |
| 55 | except TypeError: |
| 56 | pass |
| 57 | else: |
| 58 | raise TestFailed, "dangerous instance method creation allowed" |
| 59 | |
Barry Warsaw | dfdac1a | 2001-03-23 16:13:30 +0000 | [diff] [blame] | 60 | # It's unclear what the semantics should be for a code object compiled at |
| 61 | # module scope, but bound and run in a function. In CPython, `c' is global |
| 62 | # (by accident?) while in Jython, `c' is local. The intent of the test |
| 63 | # 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] | 64 | codestr = ''' |
Barry Warsaw | dfdac1a | 2001-03-23 16:13:30 +0000 | [diff] [blame] | 65 | global c |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 66 | a = 1 |
| 67 | b = 2 |
| 68 | c = a + b |
| 69 | ''' |
| 70 | |
| 71 | ccode = compile(codestr, '<string>', 'exec') |
Barry Warsaw | dfdac1a | 2001-03-23 16:13:30 +0000 | [diff] [blame] | 72 | # Jython doesn't have a __builtins__, so use a portable alternative |
| 73 | import __builtin__ |
| 74 | g = {'c': 0, '__builtins__': __builtin__} |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 75 | # this test could be more robust |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 76 | print 'new.function()' |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 77 | func = new.function(ccode, g) |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 78 | if verbose: |
| 79 | print func |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 80 | func() |
Fred Drake | 64d42c5 | 2001-01-28 03:57:39 +0000 | [diff] [blame] | 81 | verify(g['c'] == 3, |
| 82 | 'Could not create a proper function object') |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 83 | |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 84 | # test the various extended flavors of function.new |
| 85 | def f(x): |
| 86 | def g(y): |
| 87 | return x + y |
| 88 | return g |
| 89 | g = f(4) |
| 90 | new.function(f.func_code, {}, "blah") |
| 91 | g2 = new.function(g.func_code, {}, "blah", (2,), g.func_closure) |
| 92 | verify(g2() == 6) |
| 93 | g3 = new.function(g.func_code, {}, "blah", None, g.func_closure) |
| 94 | verify(g3(5) == 9) |
| 95 | def test_closure(func, closure, exc): |
| 96 | try: |
| 97 | new.function(func.func_code, {}, "", None, closure) |
| 98 | except exc: |
| 99 | pass |
| 100 | else: |
| 101 | print "corrupt closure accepted" |
| 102 | |
| 103 | test_closure(g, None, TypeError) # invalid closure |
| 104 | test_closure(g, (1,), TypeError) # non-cell in closure |
| 105 | test_closure(g, (1, 1), ValueError) # closure is wrong size |
| 106 | test_closure(f, g.func_closure, ValueError) # no closure needed |
| 107 | |
Finn Bock | d08011a | 2001-12-09 10:19:25 +0000 | [diff] [blame] | 108 | print 'new.code()' |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 109 | # bogus test of new.code() |
Barry Warsaw | dfdac1a | 2001-03-23 16:13:30 +0000 | [diff] [blame] | 110 | # Note: Jython will never have new.code() |
| 111 | if hasattr(new, 'code'): |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 112 | def f(a): pass |
Tim Peters | bf9ac4b | 2004-08-13 03:57:22 +0000 | [diff] [blame] | 113 | |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 114 | c = f.func_code |
| 115 | argcount = c.co_argcount |
| 116 | nlocals = c.co_nlocals |
| 117 | stacksize = c.co_stacksize |
| 118 | flags = c.co_flags |
| 119 | codestring = c.co_code |
| 120 | constants = c.co_consts |
| 121 | names = c.co_names |
| 122 | varnames = c.co_varnames |
| 123 | filename = c.co_filename |
| 124 | name = c.co_name |
| 125 | firstlineno = c.co_firstlineno |
| 126 | lnotab = c.co_lnotab |
| 127 | freevars = c.co_freevars |
| 128 | cellvars = c.co_cellvars |
Tim Peters | bf9ac4b | 2004-08-13 03:57:22 +0000 | [diff] [blame] | 129 | |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 130 | d = new.code(argcount, nlocals, stacksize, flags, codestring, |
| 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 |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 135 | d = new.code(argcount, nlocals, stacksize, flags, codestring, |
| 136 | constants, names, varnames, filename, name, |
| 137 | firstlineno, lnotab) |
Tim Peters | bf9ac4b | 2004-08-13 03:57:22 +0000 | [diff] [blame] | 138 | |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 139 | try: # this used to trigger a SystemError |
| 140 | d = new.code(-argcount, nlocals, stacksize, flags, codestring, |
| 141 | constants, names, varnames, filename, name, |
| 142 | firstlineno, lnotab) |
| 143 | except ValueError: |
| 144 | pass |
| 145 | else: |
| 146 | raise TestFailed, "negative co_argcount didn't trigger an exception" |
| 147 | |
| 148 | try: # this used to trigger a SystemError |
| 149 | d = new.code(argcount, -nlocals, stacksize, flags, codestring, |
| 150 | constants, names, varnames, filename, name, |
| 151 | firstlineno, lnotab) |
| 152 | except ValueError: |
| 153 | pass |
| 154 | else: |
| 155 | raise TestFailed, "negative co_nlocals didn't trigger an exception" |
Tim Peters | bf9ac4b | 2004-08-13 03:57:22 +0000 | [diff] [blame] | 156 | |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 157 | try: # this used to trigger a Py_FatalError! |
| 158 | d = new.code(argcount, nlocals, stacksize, flags, codestring, |
| 159 | constants, (5,), varnames, filename, name, |
| 160 | firstlineno, lnotab) |
| 161 | except TypeError: |
| 162 | pass |
| 163 | else: |
| 164 | raise TestFailed, "non-string co_name didn't trigger an exception" |
| 165 | |
| 166 | # new.code used to be a way to mutate a tuple... |
| 167 | class S(str): pass |
| 168 | t = (S("ab"),) |
| 169 | d = new.code(argcount, nlocals, stacksize, flags, codestring, |
| 170 | constants, t, varnames, filename, name, |
| 171 | firstlineno, lnotab) |
| 172 | verify(type(t[0]) is S, "eek, tuple changed under us!") |
| 173 | |
Barry Warsaw | dfdac1a | 2001-03-23 16:13:30 +0000 | [diff] [blame] | 174 | if verbose: |
| 175 | print d |