ยปCore Development>Code coverage>Lib/test/test_new.py

Python code coverage for Lib/test/test_new.py

#countcontent
11import unittest
21from test import test_support
31import sys
41new = test_support.import_module('new', deprecated=True)
5n/a
62class NewTest(unittest.TestCase):
71 def test_spam(self):
82 class Eggs:
91 def get_yolks(self):
103 return self.yolks
11n/a
121 m = new.module('Spam')
131 m.Eggs = Eggs
141 sys.modules['Spam'] = m
151 import Spam
16n/a
171 def get_more_yolks(self):
182 return self.yolks + 3
19n/a
20n/a # new.classobj()
211 C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks})
22n/a
23n/a # new.instance()
241 c = new.instance(C, {'yolks': 3})
25n/a
261 o = new.instance(C)
271 self.assertEqual(o.__dict__, {}, "new __dict__ should be empty")
281 del o
291 o = new.instance(C, None)
301 self.assertEqual(o.__dict__, {}, "new __dict__ should be empty")
311 del o
32n/a
331 def break_yolks(self):
342 self.yolks = self.yolks - 2
35n/a
36n/a # new.instancemethod()
371 im = new.instancemethod(break_yolks, c, C)
38n/a
391 self.assertEqual(c.get_yolks(), 3,
401 'Broken call of hand-crafted class instance')
411 self.assertEqual(c.get_more_yolks(), 6,
421 'Broken call of hand-crafted class instance')
43n/a
441 im()
451 self.assertEqual(c.get_yolks(), 1,
461 'Broken call of hand-crafted instance method')
471 self.assertEqual(c.get_more_yolks(), 4,
481 'Broken call of hand-crafted instance method')
49n/a
501 im = new.instancemethod(break_yolks, c)
511 im()
521 self.assertEqual(c.get_yolks(), -1)
53n/a
54n/a # Verify that dangerous instance method creation is forbidden
551 self.assertRaises(TypeError, new.instancemethod, break_yolks, None)
56n/a
57n/a # Verify that instancemethod() doesn't allow keyword args
581 self.assertRaises(TypeError, new.instancemethod, break_yolks, c, kw=1)
59n/a
601 def test_scope(self):
61n/a # It's unclear what the semantics should be for a code object compiled
62n/a # at module scope, but bound and run in a function. In CPython, `c' is
63n/a # global (by accident?) while in Jython, `c' is local. The intent of
64n/a # the test clearly is to make `c' global, so let's be explicit about it.
65n/a codestr = '''
66n/a global c
67n/a a = 1
68n/a b = 2
69n/a c = a + b
701 '''
71n/a
728 codestr = "\n".join(l.strip() for l in codestr.splitlines())
73n/a
741 ccode = compile(codestr, '<string>', 'exec')
75n/a # Jython doesn't have a __builtins__, so use a portable alternative
761 import __builtin__
771 g = {'c': 0, '__builtins__': __builtin__}
78n/a
79n/a # this test could be more robust
801 func = new.function(ccode, g)
811 func()
821 self.assertEqual(g['c'], 3, 'Could not create a proper function object')
83n/a
841 def test_function(self):
85n/a # test the various extended flavors of function.new
861 def f(x):
871 def g(y):
880 return x + y
891 return g
901 g = f(4)
911 new.function(f.func_code, {}, "blah")
921 g2 = new.function(g.func_code, {}, "blah", (2,), g.func_closure)
931 self.assertEqual(g2(), 6)
941 g3 = new.function(g.func_code, {}, "blah", None, g.func_closure)
951 self.assertEqual(g3(5), 9)
961 def test_closure(func, closure, exc):
974 self.assertRaises(exc, new.function, func.func_code, {}, "", None, closure)
98n/a
991 test_closure(g, None, TypeError) # invalid closure
1001 test_closure(g, (1,), TypeError) # non-cell in closure
1011 test_closure(g, (1, 1), ValueError) # closure is wrong size
1021 test_closure(f, g.func_closure, ValueError) # no closure needed
103n/a
104n/a # Note: Jython will never have new.code()
1051 if hasattr(new, 'code'):
1061 def test_code(self):
107n/a # bogus test of new.code()
1081 def f(a): pass
109n/a
1101 c = f.func_code
1111 argcount = c.co_argcount
1121 nlocals = c.co_nlocals
1131 stacksize = c.co_stacksize
1141 flags = c.co_flags
1151 codestring = c.co_code
1161 constants = c.co_consts
1171 names = c.co_names
1181 varnames = c.co_varnames
1191 filename = c.co_filename
1201 name = c.co_name
1211 firstlineno = c.co_firstlineno
1221 lnotab = c.co_lnotab
1231 freevars = c.co_freevars
1241 cellvars = c.co_cellvars
125n/a
1261 d = new.code(argcount, nlocals, stacksize, flags, codestring,
1271 constants, names, varnames, filename, name,
1281 firstlineno, lnotab, freevars, cellvars)
129n/a
130n/a # test backwards-compatibility version with no freevars or cellvars
1311 d = new.code(argcount, nlocals, stacksize, flags, codestring,
1321 constants, names, varnames, filename, name,
1331 firstlineno, lnotab)
134n/a
135n/a # negative co_argcount used to trigger a SystemError
1361 self.assertRaises(ValueError, new.code,
1371 -argcount, nlocals, stacksize, flags, codestring,
1381 constants, names, varnames, filename, name, firstlineno, lnotab)
139n/a
140n/a # negative co_nlocals used to trigger a SystemError
1411 self.assertRaises(ValueError, new.code,
1421 argcount, -nlocals, stacksize, flags, codestring,
1431 constants, names, varnames, filename, name, firstlineno, lnotab)
144n/a
145n/a # non-string co_name used to trigger a Py_FatalError
1461 self.assertRaises(TypeError, new.code,
1471 argcount, nlocals, stacksize, flags, codestring,
1481 constants, (5,), varnames, filename, name, firstlineno, lnotab)
149n/a
150n/a # new.code used to be a way to mutate a tuple...
1512 class S(str):
1521 pass
1531 t = (S("ab"),)
1541 d = new.code(argcount, nlocals, stacksize, flags, codestring,
1551 constants, t, varnames, filename, name,
1561 firstlineno, lnotab)
1571 self.assertTrue(type(t[0]) is S, "eek, tuple changed under us!")
158n/a
1591def test_main():
1601 test_support.run_unittest(NewTest)
161n/a
1621if __name__ == "__main__":
1630 test_main()