Fred Drake | 64d42c5 | 2001-01-28 03:57:39 +0000 | [diff] [blame] | 1 | from test_support import verbose, verify |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 2 | import sys |
| 3 | import new |
| 4 | |
| 5 | class Eggs: |
| 6 | def get_yolks(self): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 7 | return self.yolks |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 8 | |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 9 | print 'new.module()' |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 10 | m = new.module('Spam') |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 11 | if verbose: |
| 12 | print m |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 13 | m.Eggs = Eggs |
| 14 | sys.modules['Spam'] = m |
| 15 | import Spam |
| 16 | |
| 17 | def get_more_yolks(self): |
| 18 | return self.yolks + 3 |
| 19 | |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 20 | print 'new.classobj()' |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 21 | C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks}) |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 22 | if verbose: |
| 23 | print C |
| 24 | print 'new.instance()' |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 25 | c = new.instance(C, {'yolks': 3}) |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 26 | if verbose: |
| 27 | print c |
Fred Drake | 64d42c5 | 2001-01-28 03:57:39 +0000 | [diff] [blame] | 28 | o = new.instance(C) |
| 29 | verify(o.__dict__ == {}, |
| 30 | "new __dict__ should be empty") |
| 31 | del o |
| 32 | o = new.instance(C, None) |
| 33 | verify(o.__dict__ == {}, |
| 34 | "new __dict__ should be empty") |
| 35 | del o |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 36 | |
| 37 | def break_yolks(self): |
| 38 | self.yolks = self.yolks - 2 |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 39 | print 'new.instancemethod()' |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 40 | im = new.instancemethod(break_yolks, c, C) |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 41 | if verbose: |
| 42 | print im |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 43 | |
Fred Drake | 64d42c5 | 2001-01-28 03:57:39 +0000 | [diff] [blame] | 44 | verify(c.get_yolks() == 3 and c.get_more_yolks() == 6, |
| 45 | 'Broken call of hand-crafted class instance') |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 46 | im() |
Fred Drake | 64d42c5 | 2001-01-28 03:57:39 +0000 | [diff] [blame] | 47 | verify(c.get_yolks() == 1 and c.get_more_yolks() == 4, |
| 48 | 'Broken call of hand-crafted instance method') |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 49 | |
| 50 | codestr = ''' |
| 51 | a = 1 |
| 52 | b = 2 |
| 53 | c = a + b |
| 54 | ''' |
| 55 | |
| 56 | ccode = compile(codestr, '<string>', 'exec') |
| 57 | g = {'c': 0, '__builtins__': __builtins__} |
| 58 | # this test could be more robust |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 59 | print 'new.function()' |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 60 | func = new.function(ccode, g) |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 61 | if verbose: |
| 62 | print func |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 63 | func() |
Fred Drake | 64d42c5 | 2001-01-28 03:57:39 +0000 | [diff] [blame] | 64 | verify(g['c'] == 3, |
| 65 | 'Could not create a proper function object') |
Barry Warsaw | 924e5d5 | 1996-12-10 16:28:53 +0000 | [diff] [blame] | 66 | |
| 67 | # bogus test of new.code() |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 68 | print 'new.code()' |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 69 | d = new.code(3, 3, 3, 3, codestr, (), (), (), |
Jeremy Hylton | 6fe0a82 | 2001-02-01 19:50:29 +0000 | [diff] [blame] | 70 | "<string>", "<name>", 1, "", (), ()) |
| 71 | # test backwards-compatibility version with no freevars or cellvars |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 72 | d = new.code(3, 3, 3, 3, codestr, (), (), (), |
Jeremy Hylton | 619eea6 | 2001-01-25 20:12:27 +0000 | [diff] [blame] | 73 | "<string>", "<name>", 1, "") |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 74 | if verbose: |
| 75 | print d |