2001-01-27 23:57:39 -04:00
|
|
|
from test_support import verbose, verify
|
1996-12-10 12:28:53 -04:00
|
|
|
import sys
|
|
|
|
import new
|
|
|
|
|
|
|
|
class Eggs:
|
|
|
|
def get_yolks(self):
|
1998-03-26 15:42:58 -04:00
|
|
|
return self.yolks
|
1996-12-10 12:28:53 -04:00
|
|
|
|
1996-12-23 19:39:42 -04:00
|
|
|
print 'new.module()'
|
1996-12-10 12:28:53 -04:00
|
|
|
m = new.module('Spam')
|
1996-12-23 19:39:42 -04:00
|
|
|
if verbose:
|
|
|
|
print m
|
1996-12-10 12:28:53 -04:00
|
|
|
m.Eggs = Eggs
|
|
|
|
sys.modules['Spam'] = m
|
|
|
|
import Spam
|
|
|
|
|
|
|
|
def get_more_yolks(self):
|
|
|
|
return self.yolks + 3
|
|
|
|
|
1996-12-23 19:39:42 -04:00
|
|
|
print 'new.classobj()'
|
1996-12-10 12:28:53 -04:00
|
|
|
C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks})
|
1996-12-23 19:39:42 -04:00
|
|
|
if verbose:
|
|
|
|
print C
|
|
|
|
print 'new.instance()'
|
1996-12-10 12:28:53 -04:00
|
|
|
c = new.instance(C, {'yolks': 3})
|
1996-12-23 19:39:42 -04:00
|
|
|
if verbose:
|
|
|
|
print c
|
2001-01-27 23:57:39 -04:00
|
|
|
o = new.instance(C)
|
|
|
|
verify(o.__dict__ == {},
|
|
|
|
"new __dict__ should be empty")
|
|
|
|
del o
|
|
|
|
o = new.instance(C, None)
|
|
|
|
verify(o.__dict__ == {},
|
|
|
|
"new __dict__ should be empty")
|
|
|
|
del o
|
1996-12-10 12:28:53 -04:00
|
|
|
|
|
|
|
def break_yolks(self):
|
|
|
|
self.yolks = self.yolks - 2
|
1996-12-23 19:39:42 -04:00
|
|
|
print 'new.instancemethod()'
|
1996-12-10 12:28:53 -04:00
|
|
|
im = new.instancemethod(break_yolks, c, C)
|
1996-12-23 19:39:42 -04:00
|
|
|
if verbose:
|
|
|
|
print im
|
1996-12-10 12:28:53 -04:00
|
|
|
|
2001-01-27 23:57:39 -04:00
|
|
|
verify(c.get_yolks() == 3 and c.get_more_yolks() == 6,
|
|
|
|
'Broken call of hand-crafted class instance')
|
1996-12-10 12:28:53 -04:00
|
|
|
im()
|
2001-01-27 23:57:39 -04:00
|
|
|
verify(c.get_yolks() == 1 and c.get_more_yolks() == 4,
|
|
|
|
'Broken call of hand-crafted instance method')
|
1996-12-10 12:28:53 -04:00
|
|
|
|
|
|
|
codestr = '''
|
|
|
|
a = 1
|
|
|
|
b = 2
|
|
|
|
c = a + b
|
|
|
|
'''
|
|
|
|
|
|
|
|
ccode = compile(codestr, '<string>', 'exec')
|
|
|
|
g = {'c': 0, '__builtins__': __builtins__}
|
|
|
|
# this test could be more robust
|
1996-12-23 19:39:42 -04:00
|
|
|
print 'new.function()'
|
1996-12-10 12:28:53 -04:00
|
|
|
func = new.function(ccode, g)
|
1996-12-23 19:39:42 -04:00
|
|
|
if verbose:
|
|
|
|
print func
|
1996-12-10 12:28:53 -04:00
|
|
|
func()
|
2001-01-27 23:57:39 -04:00
|
|
|
verify(g['c'] == 3,
|
|
|
|
'Could not create a proper function object')
|
1996-12-10 12:28:53 -04:00
|
|
|
|
|
|
|
# bogus test of new.code()
|
1996-12-23 19:39:42 -04:00
|
|
|
print 'new.code()'
|
2001-02-09 16:17:14 -04:00
|
|
|
d = new.code(3, 3, 3, 3, codestr, (), (), (),
|
2001-02-01 15:50:29 -04:00
|
|
|
"<string>", "<name>", 1, "", (), ())
|
|
|
|
# test backwards-compatibility version with no freevars or cellvars
|
2001-02-09 16:17:14 -04:00
|
|
|
d = new.code(3, 3, 3, 3, codestr, (), (), (),
|
2001-01-25 16:12:27 -04:00
|
|
|
"<string>", "<name>", 1, "")
|
1996-12-23 19:39:42 -04:00
|
|
|
if verbose:
|
|
|
|
print d
|