Last set of CW Pro 5 projects (probably)
[python/dscho.git] / Lib / test / test_new.py
bloba693d8b87b883efb2b63f280f1061bff429bef33
1 from test_support import verbose
2 import sys
3 import new
5 class Eggs:
6 def get_yolks(self):
7 return self.yolks
9 print 'new.module()'
10 m = new.module('Spam')
11 if verbose:
12 print m
13 m.Eggs = Eggs
14 sys.modules['Spam'] = m
15 import Spam
17 def get_more_yolks(self):
18 return self.yolks + 3
20 print 'new.classobj()'
21 C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks})
22 if verbose:
23 print C
24 print 'new.instance()'
25 c = new.instance(C, {'yolks': 3})
26 if verbose:
27 print c
29 def break_yolks(self):
30 self.yolks = self.yolks - 2
31 print 'new.instancemethod()'
32 im = new.instancemethod(break_yolks, c, C)
33 if verbose:
34 print im
36 if c.get_yolks() <> 3 and c.get_more_yolks() <> 6:
37 print 'Broken call of hand-crafted class instance'
38 im()
39 if c.get_yolks() <> 1 and c.get_more_yolks() <> 4:
40 print 'Broken call of hand-crafted instance method'
42 codestr = '''
43 a = 1
44 b = 2
45 c = a + b
46 '''
48 ccode = compile(codestr, '<string>', 'exec')
49 g = {'c': 0, '__builtins__': __builtins__}
50 # this test could be more robust
51 print 'new.function()'
52 func = new.function(ccode, g)
53 if verbose:
54 print func
55 func()
56 if g['c'] <> 3:
57 print 'Could not create a proper function object'
59 # bogus test of new.code()
60 print 'new.code()'
61 d = new.code(3, 3, 3, 3, codestr, (), (), (), "<string>", "<name>", 1, "")
62 if verbose:
63 print d