Back to index

python3.2  3.2.2
Classes | Functions | Variables
test.test_compile Namespace Reference

Classes

class  TestSpecifics

Functions

def test_subscripts
 def test_unicode_encoding(self): code = "# -*- coding: utf-8 -*-\npass\n" self.assertRaises(SyntaxError, compile, code, "tmp", "exec")
def test_annotation_limit
def test_mangling
def test_compile_ast
def test_main

Variables

 data

Function Documentation

Definition at line 375 of file test_compile.py.

00375 
00376     def test_annotation_limit(self):
00377         # 16 bits are available for # of annotations, but only 8 bits are
00378         # available for the parameter count, hence 255
00379         # is the max. Ensure the result of too many annotations is a
00380         # SyntaxError.
00381         s = "def f(%s): pass"
00382         s %= ', '.join('a%d:%d' % (i,i) for i in range(256))
00383         self.assertRaises(SyntaxError, compile, s, '?', 'exec')
00384         # Test that the max # of annotations compiles.
00385         s = "def f(%s): pass"
00386         s %= ', '.join('a%d:%d' % (i,i) for i in range(255))
00387         compile(s, '?', 'exec')

Here is the call graph for this function:

Definition at line 401 of file test_compile.py.

00401 
00402     def test_compile_ast(self):
00403         fname = __file__
00404         if fname.lower().endswith(('pyc', 'pyo')):
00405             fname = fname[:-1]
00406         with open(fname, 'r') as f:
00407             fcontents = f.read()
00408         sample_code = [
00409             ['<assign>', 'x = 5'],
00410             ['<ifblock>', """if True:\n    pass\n"""],
00411             ['<forblock>', """for n in [1, 2, 3]:\n    print(n)\n"""],
00412             ['<deffunc>', """def foo():\n    pass\nfoo()\n"""],
00413             [fname, fcontents],
00414         ]
00415 
00416         for fname, code in sample_code:
00417             co1 = compile(code, '%s1' % fname, 'exec')
00418             ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
00419             self.assertTrue(type(ast) == _ast.Module)
00420             co2 = compile(ast, '%s3' % fname, 'exec')
00421             self.assertEqual(co1, co2)
00422             # the code object's filename comes from the second compilation step
00423             self.assertEqual(co2.co_filename, '%s3' % fname)
00424 
00425         # raise exception when node type doesn't match with compile mode
00426         co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST)
00427         self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')
00428 
00429         # raise exception when node type is no start node
00430         self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')
00431 
00432         # raise exception when node has invalid children
00433         ast = _ast.Module()
00434         ast.body = [_ast.BoolOp()]
00435         self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
00436 

Here is the call graph for this function:

Definition at line 437 of file test_compile.py.

00437 
00438 def test_main():
00439     support.run_unittest(TestSpecifics)

Definition at line 388 of file test_compile.py.

00388 
00389     def test_mangling(self):
00390         class A:
00391             def f():
00392                 __mangled = 1
00393                 __not_mangled__ = 2
00394                 import __mangled_mod
00395                 import __package__.module
00396 
00397         self.assertIn("_A__mangled", A.f.__code__.co_varnames)
00398         self.assertIn("__not_mangled__", A.f.__code__.co_varnames)
00399         self.assertIn("_A__mangled_mod", A.f.__code__.co_varnames)
00400         self.assertIn("__package__", A.f.__code__.co_varnames)

def test_unicode_encoding(self): code = "# -*- coding: utf-8 -*-\npass\n" self.assertRaises(SyntaxError, compile, code, "tmp", "exec")

Definition at line 303 of file test_compile.py.

00303 
00304     def test_subscripts(self):
00305         # SF bug 1448804
00306         # Class to make testing subscript results easy
00307         class str_map(object):
00308             def __init__(self):
00309                 self.data = {}
00310             def __getitem__(self, key):
00311                 return self.data[str(key)]
00312             def __setitem__(self, key, value):
00313                 self.data[str(key)] = value
00314             def __delitem__(self, key):
00315                 del self.data[str(key)]
00316             def __contains__(self, key):
00317                 return str(key) in self.data
00318         d = str_map()
00319         # Index
00320         d[1] = 1
00321         self.assertEqual(d[1], 1)
00322         d[1] += 1
00323         self.assertEqual(d[1], 2)
00324         del d[1]
00325         self.assertNotIn(1, d)
00326         # Tuple of indices
00327         d[1, 1] = 1
00328         self.assertEqual(d[1, 1], 1)
00329         d[1, 1] += 1
00330         self.assertEqual(d[1, 1], 2)
00331         del d[1, 1]
00332         self.assertNotIn((1, 1), d)
00333         # Simple slice
00334         d[1:2] = 1
00335         self.assertEqual(d[1:2], 1)
00336         d[1:2] += 1
00337         self.assertEqual(d[1:2], 2)
00338         del d[1:2]
00339         self.assertNotIn(slice(1, 2), d)
00340         # Tuple of simple slices
00341         d[1:2, 1:2] = 1
00342         self.assertEqual(d[1:2, 1:2], 1)
00343         d[1:2, 1:2] += 1
00344         self.assertEqual(d[1:2, 1:2], 2)
00345         del d[1:2, 1:2]
00346         self.assertNotIn((slice(1, 2), slice(1, 2)), d)
00347         # Extended slice
00348         d[1:2:3] = 1
00349         self.assertEqual(d[1:2:3], 1)
00350         d[1:2:3] += 1
00351         self.assertEqual(d[1:2:3], 2)
00352         del d[1:2:3]
00353         self.assertNotIn(slice(1, 2, 3), d)
00354         # Tuple of extended slices
00355         d[1:2:3, 1:2:3] = 1
00356         self.assertEqual(d[1:2:3, 1:2:3], 1)
00357         d[1:2:3, 1:2:3] += 1
00358         self.assertEqual(d[1:2:3, 1:2:3], 2)
00359         del d[1:2:3, 1:2:3]
00360         self.assertNotIn((slice(1, 2, 3), slice(1, 2, 3)), d)
00361         # Ellipsis
00362         d[...] = 1
00363         self.assertEqual(d[...], 1)
00364         d[...] += 1
00365         self.assertEqual(d[...], 2)
00366         del d[...]
00367         self.assertNotIn(Ellipsis, d)
00368         # Tuple of Ellipses
00369         d[..., ...] = 1
00370         self.assertEqual(d[..., ...], 1)
00371         d[..., ...] += 1
00372         self.assertEqual(d[..., ...], 2)
00373         del d[..., ...]
00374         self.assertNotIn((Ellipsis, Ellipsis), d)

Here is the call graph for this function:


Variable Documentation

Definition at line 308 of file test_compile.py.