blob: af26f01e6fca1a6054b5256dd1fa6bcfcbb6d987 [file] [log] [blame]
Fredrik Lundhf7850422001-01-17 21:51:36 +00001from test_support import verbose
Barry Warsaw924e5d51996-12-10 16:28:53 +00002import sys
3import new
4
5class Eggs:
6 def get_yolks(self):
Guido van Rossum41360a41998-03-26 19:42:58 +00007 return self.yolks
Barry Warsaw924e5d51996-12-10 16:28:53 +00008
Barry Warsaw5e056bb1996-12-23 23:39:42 +00009print 'new.module()'
Barry Warsaw924e5d51996-12-10 16:28:53 +000010m = new.module('Spam')
Barry Warsaw5e056bb1996-12-23 23:39:42 +000011if verbose:
12 print m
Barry Warsaw924e5d51996-12-10 16:28:53 +000013m.Eggs = Eggs
14sys.modules['Spam'] = m
15import Spam
16
17def get_more_yolks(self):
18 return self.yolks + 3
19
Barry Warsaw5e056bb1996-12-23 23:39:42 +000020print 'new.classobj()'
Barry Warsaw924e5d51996-12-10 16:28:53 +000021C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks})
Barry Warsaw5e056bb1996-12-23 23:39:42 +000022if verbose:
23 print C
24print 'new.instance()'
Barry Warsaw924e5d51996-12-10 16:28:53 +000025c = new.instance(C, {'yolks': 3})
Barry Warsaw5e056bb1996-12-23 23:39:42 +000026if verbose:
27 print c
Barry Warsaw924e5d51996-12-10 16:28:53 +000028
29def break_yolks(self):
30 self.yolks = self.yolks - 2
Barry Warsaw5e056bb1996-12-23 23:39:42 +000031print 'new.instancemethod()'
Barry Warsaw924e5d51996-12-10 16:28:53 +000032im = new.instancemethod(break_yolks, c, C)
Barry Warsaw5e056bb1996-12-23 23:39:42 +000033if verbose:
34 print im
Barry Warsaw924e5d51996-12-10 16:28:53 +000035
Fred Drake132dce22000-12-12 23:11:42 +000036if c.get_yolks() != 3 and c.get_more_yolks() != 6:
Barry Warsaw924e5d51996-12-10 16:28:53 +000037 print 'Broken call of hand-crafted class instance'
38im()
Fred Drake132dce22000-12-12 23:11:42 +000039if c.get_yolks() != 1 and c.get_more_yolks() != 4:
Barry Warsaw924e5d51996-12-10 16:28:53 +000040 print 'Broken call of hand-crafted instance method'
41
42codestr = '''
43a = 1
44b = 2
45c = a + b
46'''
47
48ccode = compile(codestr, '<string>', 'exec')
49g = {'c': 0, '__builtins__': __builtins__}
50# this test could be more robust
Barry Warsaw5e056bb1996-12-23 23:39:42 +000051print 'new.function()'
Barry Warsaw924e5d51996-12-10 16:28:53 +000052func = new.function(ccode, g)
Barry Warsaw5e056bb1996-12-23 23:39:42 +000053if verbose:
54 print func
Barry Warsaw924e5d51996-12-10 16:28:53 +000055func()
Fred Drake132dce22000-12-12 23:11:42 +000056if g['c'] != 3:
Barry Warsaw924e5d51996-12-10 16:28:53 +000057 print 'Could not create a proper function object'
58
59# bogus test of new.code()
Barry Warsaw5e056bb1996-12-23 23:39:42 +000060print 'new.code()'
Jeremy Hylton619eea62001-01-25 20:12:27 +000061d = new.code(3, 3, 3, 3, codestr, (), (), (), (), (),
62 "<string>", "<name>", 1, "")
Barry Warsaw5e056bb1996-12-23 23:39:42 +000063if verbose:
64 print d