~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to lib-python/2.4.1/test/test_new.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from test.test_support import verbose, verify, TestFailed
 
2
import sys
 
3
import new
 
4
 
 
5
class Eggs:
 
6
    def get_yolks(self):
 
7
        return self.yolks
 
8
 
 
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
 
16
 
 
17
def get_more_yolks(self):
 
18
    return self.yolks + 3
 
19
 
 
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
 
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
 
36
 
 
37
def break_yolks(self):
 
38
    self.yolks = self.yolks - 2
 
39
print 'new.instancemethod()'
 
40
im = new.instancemethod(break_yolks, c, C)
 
41
if verbose:
 
42
    print im
 
43
 
 
44
verify(c.get_yolks() == 3 and c.get_more_yolks() == 6,
 
45
       'Broken call of hand-crafted class instance')
 
46
im()
 
47
verify(c.get_yolks() == 1 and c.get_more_yolks() == 4,
 
48
       'Broken call of hand-crafted instance method')
 
49
 
 
50
# It's unclear what the semantics should be for a code object compiled at
 
51
# module scope, but bound and run in a function.  In CPython, `c' is global
 
52
# (by accident?) while in Jython, `c' is local.  The intent of the test
 
53
# clearly is to make `c' global, so let's be explicit about it.
 
54
codestr = '''
 
55
global c
 
56
a = 1
 
57
b = 2
 
58
c = a + b
 
59
'''
 
60
 
 
61
ccode = compile(codestr, '<string>', 'exec')
 
62
# Jython doesn't have a __builtins__, so use a portable alternative
 
63
import __builtin__
 
64
g = {'c': 0, '__builtins__': __builtin__}
 
65
# this test could be more robust
 
66
print 'new.function()'
 
67
func = new.function(ccode, g)
 
68
if verbose:
 
69
    print func
 
70
func()
 
71
verify(g['c'] == 3,
 
72
       'Could not create a proper function object')
 
73
 
 
74
# test the various extended flavors of function.new
 
75
def f(x):
 
76
    def g(y):
 
77
        return x + y
 
78
    return g
 
79
g = f(4)
 
80
new.function(f.func_code, {}, "blah")
 
81
g2 = new.function(g.func_code, {}, "blah", (2,), g.func_closure)
 
82
verify(g2() == 6)
 
83
g3 = new.function(g.func_code, {}, "blah", None, g.func_closure)
 
84
verify(g3(5) == 9)
 
85
def test_closure(func, closure, exc):
 
86
    try:
 
87
        new.function(func.func_code, {}, "", None, closure)
 
88
    except exc:
 
89
        pass
 
90
    else:
 
91
        print "corrupt closure accepted"
 
92
 
 
93
test_closure(g, None, TypeError) # invalid closure
 
94
test_closure(g, (1,), TypeError) # non-cell in closure
 
95
test_closure(g, (1, 1), ValueError) # closure is wrong size
 
96
test_closure(f, g.func_closure, ValueError) # no closure needed
 
97
 
 
98
print 'new.code()'
 
99
# bogus test of new.code()
 
100
# Note: Jython will never have new.code()
 
101
if hasattr(new, 'code'):
 
102
    def f(a): pass
 
103
 
 
104
    c = f.func_code
 
105
    argcount = c.co_argcount
 
106
    nlocals = c.co_nlocals
 
107
    stacksize = c.co_stacksize
 
108
    flags = c.co_flags
 
109
    codestring = c.co_code
 
110
    constants = c.co_consts
 
111
    names = c.co_names
 
112
    varnames = c.co_varnames
 
113
    filename = c.co_filename
 
114
    name = c.co_name
 
115
    firstlineno = c.co_firstlineno
 
116
    lnotab = c.co_lnotab
 
117
    freevars = c.co_freevars
 
118
    cellvars = c.co_cellvars
 
119
 
 
120
    d = new.code(argcount, nlocals, stacksize, flags, codestring,
 
121
                 constants, names, varnames, filename, name,
 
122
                 firstlineno, lnotab, freevars, cellvars)
 
123
 
 
124
    # test backwards-compatibility version with no freevars or cellvars
 
125
    d = new.code(argcount, nlocals, stacksize, flags, codestring,
 
126
                 constants, names, varnames, filename, name,
 
127
                 firstlineno, lnotab)
 
128
 
 
129
    try: # this used to trigger a SystemError
 
130
        d = new.code(-argcount, nlocals, stacksize, flags, codestring,
 
131
                     constants, names, varnames, filename, name,
 
132
                     firstlineno, lnotab)
 
133
    except ValueError:
 
134
        pass
 
135
    else:
 
136
        raise TestFailed, "negative co_argcount didn't trigger an exception"
 
137
 
 
138
    try: # this used to trigger a SystemError
 
139
        d = new.code(argcount, -nlocals, stacksize, flags, codestring,
 
140
                     constants, names, varnames, filename, name,
 
141
                     firstlineno, lnotab)
 
142
    except ValueError:
 
143
        pass
 
144
    else:
 
145
        raise TestFailed, "negative co_nlocals didn't trigger an exception"
 
146
 
 
147
    try: # this used to trigger a Py_FatalError!
 
148
        d = new.code(argcount, nlocals, stacksize, flags, codestring,
 
149
                     constants, (5,), varnames, filename, name,
 
150
                     firstlineno, lnotab)
 
151
    except TypeError:
 
152
        pass
 
153
    else:
 
154
        raise TestFailed, "non-string co_name didn't trigger an exception"
 
155
 
 
156
    # new.code used to be a way to mutate a tuple...
 
157
    class S(str): pass
 
158
    t = (S("ab"),)
 
159
    d = new.code(argcount, nlocals, stacksize, flags, codestring,
 
160
                 constants, t, varnames, filename, name,
 
161
                 firstlineno, lnotab)
 
162
    verify(type(t[0]) is S, "eek, tuple changed under us!")
 
163
 
 
164
    if verbose:
 
165
        print d