test_new.py 1.29 KB
Newer Older
1
from test_support import verbose
2 3 4 5 6 7 8
import sys
import new

class Eggs:
    def get_yolks(self):
	return self.yolks

9
print 'new.module()'
10
m = new.module('Spam')
11 12
if verbose:
    print m
13 14 15 16 17 18 19
m.Eggs = Eggs
sys.modules['Spam'] = m
import Spam

def get_more_yolks(self):
    return self.yolks + 3

20
print 'new.classobj()'
21
C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks})
22 23 24
if verbose:
    print C
print 'new.instance()'
25
c = new.instance(C, {'yolks': 3})
26 27
if verbose:
    print c
28 29 30

def break_yolks(self):
    self.yolks = self.yolks - 2
31
print 'new.instancemethod()'
32
im = new.instancemethod(break_yolks, c, C)
33 34
if verbose:
    print im
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

if c.get_yolks() <> 3 and c.get_more_yolks() <> 6:
    print 'Broken call of hand-crafted class instance'
im()
if c.get_yolks() <> 1 and c.get_more_yolks() <> 4:
    print 'Broken call of hand-crafted instance method'

codestr = '''
a = 1
b = 2
c = a + b
'''

ccode = compile(codestr, '<string>', 'exec')
g = {'c': 0, '__builtins__': __builtins__}
# this test could be more robust
51
print 'new.function()'
52
func = new.function(ccode, g)
53 54
if verbose:
    print func
55 56 57 58 59
func()
if g['c'] <> 3:
    print 'Could not create a proper function object'

# bogus test of new.code()
60
print 'new.code()'
61
d = new.code(3, 3, 3, 3, codestr, (), (), (), "<string>", "<name>", 1, "")
62 63
if verbose:
    print d