Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame^] | 1 | import sys |
| 2 | import new |
| 3 | |
| 4 | class Eggs: |
| 5 | def get_yolks(self): |
| 6 | return self.yolks |
| 7 | |
| 8 | m = new.module('Spam') |
| 9 | m.Eggs = Eggs |
| 10 | sys.modules['Spam'] = m |
| 11 | import Spam |
| 12 | |
| 13 | def get_more_yolks(self): |
| 14 | return self.yolks + 3 |
| 15 | |
| 16 | C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks}) |
| 17 | c = new.instance(C, {'yolks': 3}) |
| 18 | |
| 19 | def break_yolks(self): |
| 20 | self.yolks = self.yolks - 2 |
| 21 | im = new.instancemethod(break_yolks, c, C) |
| 22 | |
| 23 | if c.get_yolks() <> 3 and c.get_more_yolks() <> 6: |
| 24 | print 'Broken call of hand-crafted class instance' |
| 25 | im() |
| 26 | if c.get_yolks() <> 1 and c.get_more_yolks() <> 4: |
| 27 | print 'Broken call of hand-crafted instance method' |
| 28 | |
| 29 | codestr = ''' |
| 30 | a = 1 |
| 31 | b = 2 |
| 32 | c = a + b |
| 33 | ''' |
| 34 | |
| 35 | ccode = compile(codestr, '<string>', 'exec') |
| 36 | g = {'c': 0, '__builtins__': __builtins__} |
| 37 | # this test could be more robust |
| 38 | func = new.function(ccode, g) |
| 39 | func() |
| 40 | if g['c'] <> 3: |
| 41 | print 'Could not create a proper function object' |
| 42 | |
| 43 | # bogus test of new.code() |
| 44 | new.code(3, 3, 3, codestr, (), (), (), "<string>", "<name>") |