0001 from test.test_support import verbose, verify, TestFailed 0002 import sys 0003 import new 0004 0005 class Eggs: 0006 def get_yolks(self): 0007 return self.yolks 0008 0009 print 'new.module()' 0010 m = new.module('Spam') 0011 if verbose: 0012 print m 0013 m.Eggs = Eggs 0014 sys.modules['Spam'] = m 0015 import Spam 0016 0017 def get_more_yolks(self): 0018 return self.yolks + 3 0019 0020 print 'new.classobj()' 0021 C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks}) 0022 if verbose: 0023 print C 0024 print 'new.instance()' 0025 c = new.instance(C, {'yolks': 3}) 0026 if verbose: 0027 print c 0028 o = new.instance(C) 0029 verify(o.__dict__ == {}, 0030 "new __dict__ should be empty") 0031 del o 0032 o = new.instance(C, None) 0033 verify(o.__dict__ == {}, 0034 "new __dict__ should be empty") 0035 del o 0036 0037 def break_yolks(self): 0038 self.yolks = self.yolks - 2 0039 print 'new.instancemethod()' 0040 im = new.instancemethod(break_yolks, c, C) 0041 if verbose: 0042 print im 0043 0044 verify(c.get_yolks() == 3 and c.get_more_yolks() == 6, 0045 'Broken call of hand-crafted class instance') 0046 im() 0047 verify(c.get_yolks() == 1 and c.get_more_yolks() == 4, 0048 'Broken call of hand-crafted instance method') 0049 0050 # It's unclear what the semantics should be for a code object compiled at 0051 # module scope, but bound and run in a function. In CPython, `c' is global 0052 # (by accident?) while in Jython, `c' is local. The intent of the test 0053 # clearly is to make `c' global, so let's be explicit about it. 0054 codestr = ''' 0055 global c 0056 a = 1 0057 b = 2 0058 c = a + b 0059 ''' 0060 0061 ccode = compile(codestr, '<string>', 'exec') 0062 # Jython doesn't have a __builtins__, so use a portable alternative 0063 import __builtin__ 0064 g = {'c': 0, '__builtins__': __builtin__} 0065 # this test could be more robust 0066 print 'new.function()' 0067 func = new.function(ccode, g) 0068 if verbose: 0069 print func 0070 func() 0071 verify(g['c'] == 3, 0072 'Could not create a proper function object') 0073 0074 # test the various extended flavors of function.new 0075 def f(x): 0076 def g(y): 0077 return x + y 0078 return g 0079 g = f(4) 0080 new.function(f.func_code, {}, "blah") 0081 g2 = new.function(g.func_code, {}, "blah", (2,), g.func_closure) 0082 verify(g2() == 6) 0083 g3 = new.function(g.func_code, {}, "blah", None, g.func_closure) 0084 verify(g3(5) == 9) 0085 def test_closure(func, closure, exc): 0086 try: 0087 new.function(func.func_code, {}, "", None, closure) 0088 except exc: 0089 pass 0090 else: 0091 print "corrupt closure accepted" 0092 0093 test_closure(g, None, TypeError) # invalid closure 0094 test_closure(g, (1,), TypeError) # non-cell in closure 0095 test_closure(g, (1, 1), ValueError) # closure is wrong size 0096 test_closure(f, g.func_closure, ValueError) # no closure needed 0097 0098 print 'new.code()' 0099 # bogus test of new.code() 0100 # Note: Jython will never have new.code() 0101 if hasattr(new, 'code'): 0102 def f(a): pass 0103 0104 c = f.func_code 0105 argcount = c.co_argcount 0106 nlocals = c.co_nlocals 0107 stacksize = c.co_stacksize 0108 flags = c.co_flags 0109 codestring = c.co_code 0110 constants = c.co_consts 0111 names = c.co_names 0112 varnames = c.co_varnames 0113 filename = c.co_filename 0114 name = c.co_name 0115 firstlineno = c.co_firstlineno 0116 lnotab = c.co_lnotab 0117 freevars = c.co_freevars 0118 cellvars = c.co_cellvars 0119 0120 d = new.code(argcount, nlocals, stacksize, flags, codestring, 0121 constants, names, varnames, filename, name, 0122 firstlineno, lnotab, freevars, cellvars) 0123 0124 # test backwards-compatibility version with no freevars or cellvars 0125 d = new.code(argcount, nlocals, stacksize, flags, codestring, 0126 constants, names, varnames, filename, name, 0127 firstlineno, lnotab) 0128 0129 try: # this used to trigger a SystemError 0130 d = new.code(-argcount, nlocals, stacksize, flags, codestring, 0131 constants, names, varnames, filename, name, 0132 firstlineno, lnotab) 0133 except ValueError: 0134 pass 0135 else: 0136 raise TestFailed, "negative co_argcount didn't trigger an exception" 0137 0138 try: # this used to trigger a SystemError 0139 d = new.code(argcount, -nlocals, stacksize, flags, codestring, 0140 constants, names, varnames, filename, name, 0141 firstlineno, lnotab) 0142 except ValueError: 0143 pass 0144 else: 0145 raise TestFailed, "negative co_nlocals didn't trigger an exception" 0146 0147 try: # this used to trigger a Py_FatalError! 0148 d = new.code(argcount, nlocals, stacksize, flags, codestring, 0149 constants, (5,), varnames, filename, name, 0150 firstlineno, lnotab) 0151 except TypeError: 0152 pass 0153 else: 0154 raise TestFailed, "non-string co_name didn't trigger an exception" 0155 0156 # new.code used to be a way to mutate a tuple... 0157 class S(str): pass 0158 t = (S("ab"),) 0159 d = new.code(argcount, nlocals, stacksize, flags, codestring, 0160 constants, t, varnames, filename, name, 0161 firstlineno, lnotab) 0162 verify(type(t[0]) is S, "eek, tuple changed under us!") 0163 0164 if verbose: 0165 print d 0166
Generated by PyXR 0.9.4