Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_compile.TestSpecifics Class Reference

List of all members.

Public Member Functions

def test_no_ending_newline
def test_empty
def test_other_newlines
def test_debug_assignment
def test_argument_handling
def test_syntax_error
def test_none_keyword_arg
def test_duplicate_global_local
def test_exec_with_general_mapping_for_locals
def test_extended_arg
def test_argument_order
def test_float_literals
def test_indentation
def test_leading_newlines
def test_literals_with_leading_zeroes
def test_unary_minus
def test_32_63_bit_values
def test_sequence_unpacking_error
def test_none_assignment
def test_import
def test_for_distinct_code_objects
def test_lambda_doc

Public Attributes

 results

Detailed Description

Definition at line 6 of file test_compile.py.


Member Function Documentation

Definition at line 199 of file test_compile.py.

00199 
00200         def test_32_63_bit_values(self):
00201             a = +4294967296  # 1 << 32
00202             b = -4294967296  # 1 << 32
00203             c = +281474976710656  # 1 << 48
00204             d = -281474976710656  # 1 << 48
00205             e = +4611686018427387904  # 1 << 62
00206             f = -4611686018427387904  # 1 << 62
00207             g = +9223372036854775807  # 1 << 63 - 1
00208             h = -9223372036854775807  # 1 << 63 - 1
00209 
00210             for variable in self.test_32_63_bit_values.__code__.co_consts:
00211                 if variable is not None:
00212                     self.assertIsInstance(variable, int)

Here is the call graph for this function:

Definition at line 29 of file test_compile.py.

00029 
00030     def test_argument_handling(self):
00031         # detect duplicate positional and keyword arguments
00032         self.assertRaises(SyntaxError, eval, 'lambda a,a:0')
00033         self.assertRaises(SyntaxError, eval, 'lambda a,a=1:0')
00034         self.assertRaises(SyntaxError, eval, 'lambda a=1,a=1:0')
00035         self.assertRaises(SyntaxError, exec, 'def f(a, a): pass')
00036         self.assertRaises(SyntaxError, exec, 'def f(a = 0, a = 1): pass')
00037         self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1')

Here is the call graph for this function:

Definition at line 118 of file test_compile.py.

00118 
00119     def test_argument_order(self):
00120         self.assertRaises(SyntaxError, exec, 'def f(a=1, b): pass')

Here is the call graph for this function:

Definition at line 21 of file test_compile.py.

00021 
00022     def test_debug_assignment(self):
00023         # catch assignments to __debug__
00024         self.assertRaises(SyntaxError, compile, '__debug__ = 1', '?', 'single')
00025         import builtins
00026         prev = builtins.__debug__
00027         setattr(builtins, '__debug__', 'sure')
00028         setattr(builtins, '__debug__', prev)

Here is the call graph for this function:

Definition at line 44 of file test_compile.py.

00044 
00045     def test_duplicate_global_local(self):
00046         self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1')

Here is the call graph for this function:

Definition at line 12 of file test_compile.py.

00012 
00013     def test_empty(self):
00014         compile("", "<test>", "exec")

Here is the call graph for this function:

Definition at line 47 of file test_compile.py.

00047 
00048     def test_exec_with_general_mapping_for_locals(self):
00049 
00050         class M:
00051             "Test mapping interface versus possible calls from eval()."
00052             def __getitem__(self, key):
00053                 if key == 'a':
00054                     return 12
00055                 raise KeyError
00056             def __setitem__(self, key, value):
00057                 self.results = (key, value)
00058             def keys(self):
00059                 return list('xyz')
00060 
00061         m = M()
00062         g = globals()
00063         exec('z = a', g, m)
00064         self.assertEqual(m.results, ('z', 12))
00065         try:
00066             exec('z = b', g, m)
00067         except NameError:
00068             pass
00069         else:
00070             self.fail('Did not detect a KeyError')
00071         exec('z = dir()', g, m)
00072         self.assertEqual(m.results, ('z', list('xyz')))
00073         exec('z = globals()', g, m)
00074         self.assertEqual(m.results, ('z', g))
00075         exec('z = locals()', g, m)
00076         self.assertEqual(m.results, ('z', m))
00077         self.assertRaises(TypeError, exec, 'z = b', m)
00078 
00079         class A:
00080             "Non-mapping"
00081             pass
00082         m = A()
00083         self.assertRaises(TypeError, exec, 'z = a', g, m)
00084 
00085         # Verify that dict subclasses work as well
00086         class D(dict):
00087             def __getitem__(self, key):
00088                 if key == 'a':
00089                     return 12
00090                 return dict.__getitem__(self, key)
00091         d = D()
00092         exec('z = a', g, d)
00093         self.assertEqual(d['z'], 12)

Here is the call graph for this function:

Definition at line 94 of file test_compile.py.

00094 
00095     def test_extended_arg(self):
00096         longexpr = 'x = x or ' + '-x' * 2500
00097         g = {}
00098         code = '''
00099 def f(x):
00100     %s
00101     %s
00102     %s
00103     %s
00104     %s
00105     %s
00106     %s
00107     %s
00108     %s
00109     %s
00110     # the expressions above have no effect, x == argument
00111     while x:
00112         x -= 1
00113         # EXTENDED_ARG/JUMP_ABSOLUTE here
00114     return x
00115 ''' % ((longexpr,)*10)
00116         exec(code, g)
00117         self.assertEqual(g['f'](5), 0)

Here is the call graph for this function:

Definition at line 121 of file test_compile.py.

00121 
00122     def test_float_literals(self):
00123         # testing bad float literals
00124         self.assertRaises(SyntaxError, eval, "2e")
00125         self.assertRaises(SyntaxError, eval, "2.0e+")
00126         self.assertRaises(SyntaxError, eval, "1e-")
00127         self.assertRaises(SyntaxError, eval, "3-4e/21")

Here is the call graph for this function:

Definition at line 286 of file test_compile.py.

00286 
00287     def test_for_distinct_code_objects(self):
00288         # SF bug 1048870
00289         def f():
00290             f1 = lambda x=1: x
00291             f2 = lambda x=2: x
00292             return f1, f2
00293         f1, f2 = f()
00294         self.assertNotEqual(id(f1.__code__), id(f2.__code__))

Here is the call graph for this function:

Definition at line 239 of file test_compile.py.

00239 
00240     def test_import(self):
00241         succeed = [
00242             'import sys',
00243             'import os, sys',
00244             'import os as bar',
00245             'import os.path as bar',
00246             'from __future__ import nested_scopes, generators',
00247             'from __future__ import (nested_scopes,\ngenerators)',
00248             'from __future__ import (nested_scopes,\ngenerators,)',
00249             'from sys import stdin, stderr, stdout',
00250             'from sys import (stdin, stderr,\nstdout)',
00251             'from sys import (stdin, stderr,\nstdout,)',
00252             'from sys import (stdin\n, stderr, stdout)',
00253             'from sys import (stdin\n, stderr, stdout,)',
00254             'from sys import stdin as si, stdout as so, stderr as se',
00255             'from sys import (stdin as si, stdout as so, stderr as se)',
00256             'from sys import (stdin as si, stdout as so, stderr as se,)',
00257             ]
00258         fail = [
00259             'import (os, sys)',
00260             'import (os), (sys)',
00261             'import ((os), (sys))',
00262             'import (sys',
00263             'import sys)',
00264             'import (os,)',
00265             'import os As bar',
00266             'import os.path a bar',
00267             'from sys import stdin As stdout',
00268             'from sys import stdin a stdout',
00269             'from (sys) import stdin',
00270             'from __future__ import (nested_scopes',
00271             'from __future__ import nested_scopes)',
00272             'from __future__ import nested_scopes,\ngenerators',
00273             'from sys import (stdin',
00274             'from sys import stdin)',
00275             'from sys import stdin, stdout,\nstderr',
00276             'from sys import stdin si',
00277             'from sys import stdin,'
00278             'from sys import (*)',
00279             'from sys import (stdin,, stdout, stderr)',
00280             'from sys import (stdin, stdout),',
00281             ]
00282         for stmt in succeed:
00283             compile(stmt, 'tmp', 'exec')
00284         for stmt in fail:
00285             self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')

Here is the call graph for this function:

Definition at line 128 of file test_compile.py.

00128 
00129     def test_indentation(self):
00130         # testing compile() of indented block w/o trailing newline"
00131         s = """
00132 if 1:
00133     if 2:
00134         pass"""
00135         compile(s, "<string>", "exec")

Here is the call graph for this function:

Definition at line 295 of file test_compile.py.

00295 
00296     def test_lambda_doc(self):
00297         l = lambda: "foo"
00298         self.assertIsNone(l.__doc__)

Here is the call graph for this function:

Definition at line 140 of file test_compile.py.

00140 
00141     def test_leading_newlines(self):
00142         s256 = "".join(["\n"] * 256 + ["spam"])
00143         co = compile(s256, 'fn', 'exec')
00144         self.assertEqual(co.co_firstlineno, 257)
00145         self.assertEqual(co.co_lnotab, bytes())

Here is the call graph for this function:

Definition at line 146 of file test_compile.py.

00146 
00147     def test_literals_with_leading_zeroes(self):
00148         for arg in ["077787", "0xj", "0x.", "0e",  "090000000000000",
00149                     "080000000000000", "000000000000009", "000000000000008",
00150                     "0b42", "0BADCAFE", "0o123456789", "0b1.1", "0o4.2",
00151                     "0b101j2", "0o153j2", "0b100e1", "0o777e1", "0777",
00152                     "000777", "000000000000007"]:
00153             self.assertRaises(SyntaxError, eval, arg)
00154 
00155         self.assertEqual(eval("0xff"), 255)
00156         self.assertEqual(eval("0777."), 777)
00157         self.assertEqual(eval("0777.0"), 777)
00158         self.assertEqual(eval("000000000000000000000000000000000000000000000000000777e0"), 777)
00159         self.assertEqual(eval("0777e1"), 7770)
00160         self.assertEqual(eval("0e0"), 0)
00161         self.assertEqual(eval("0000e-012"), 0)
00162         self.assertEqual(eval("09.5"), 9.5)
00163         self.assertEqual(eval("0777j"), 777j)
00164         self.assertEqual(eval("000"), 0)
00165         self.assertEqual(eval("00j"), 0j)
00166         self.assertEqual(eval("00.0"), 0)
00167         self.assertEqual(eval("0e3"), 0)
00168         self.assertEqual(eval("090000000000000."), 90000000000000.)
00169         self.assertEqual(eval("090000000000000.0000000000000000000000"), 90000000000000.)
00170         self.assertEqual(eval("090000000000000e0"), 90000000000000.)
00171         self.assertEqual(eval("090000000000000e-0"), 90000000000000.)
00172         self.assertEqual(eval("090000000000000j"), 90000000000000j)
00173         self.assertEqual(eval("000000000000008."), 8.)
00174         self.assertEqual(eval("000000000000009."), 9.)
00175         self.assertEqual(eval("0b101010"), 42)
00176         self.assertEqual(eval("-0b000000000010"), -2)
00177         self.assertEqual(eval("0o777"), 511)
00178         self.assertEqual(eval("-0o0000010"), -8)

Here is the call graph for this function:

Definition at line 8 of file test_compile.py.

00008 
00009     def test_no_ending_newline(self):
00010         compile("hi", "<test>", "exec")
00011         compile("hi\r", "<test>", "exec")

Here is the call graph for this function:

Definition at line 219 of file test_compile.py.

00219 
00220     def test_none_assignment(self):
00221         stmts = [
00222             'None = 0',
00223             'None += 0',
00224             '__builtins__.None = 0',
00225             'def None(): pass',
00226             'class None: pass',
00227             '(a, None) = 0, 0',
00228             'for None in range(10): pass',
00229             'def f(None): pass',
00230             'import None',
00231             'import x as None',
00232             'from x import None',
00233             'from x import y as None'
00234         ]
00235         for stmt in stmts:
00236             stmt += "\n"
00237             self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single')
00238             self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')

Here is the call graph for this function:

Definition at line 41 of file test_compile.py.

00041 
00042     def test_none_keyword_arg(self):
00043         self.assertRaises(SyntaxError, compile, "f(None=1)", "<string>", "exec")

Here is the call graph for this function:

Definition at line 15 of file test_compile.py.

00015 
00016     def test_other_newlines(self):
00017         compile("\r\n", "<test>", "exec")
00018         compile("\r", "<test>", "exec")
00019         compile("hi\r\nstuff\r\ndef f():\n    pass\r", "<test>", "exec")
00020         compile("this_is\rreally_old_mac\rdef f():\n    pass", "<test>", "exec")

Here is the call graph for this function:

Definition at line 213 of file test_compile.py.

00213 
00214     def test_sequence_unpacking_error(self):
00215         # Verify sequence packing/unpacking with "or".  SF bug #757818
00216         i,j = (1, -1) or (-1, 1)
00217         self.assertEqual(i, 1)
00218         self.assertEqual(j, -1)

Here is the call graph for this function:

Definition at line 38 of file test_compile.py.

00038 
00039     def test_syntax_error(self):
00040         self.assertRaises(SyntaxError, compile, "1+*3", "filename", "exec")

Here is the call graph for this function:

Definition at line 179 of file test_compile.py.

00179 
00180     def test_unary_minus(self):
00181         # Verify treatment of unary minus on negative numbers SF bug #660455
00182         if sys.maxsize == 2147483647:
00183             # 32-bit machine
00184             all_one_bits = '0xffffffff'
00185             self.assertEqual(eval(all_one_bits), 4294967295)
00186             self.assertEqual(eval("-" + all_one_bits), -4294967295)
00187         elif sys.maxsize == 9223372036854775807:
00188             # 64-bit machine
00189             all_one_bits = '0xffffffffffffffff'
00190             self.assertEqual(eval(all_one_bits), 18446744073709551615)
00191             self.assertEqual(eval("-" + all_one_bits), -18446744073709551615)
00192         else:
00193             self.fail("How many bits *does* this machine have???")
00194         # Verify treatment of constant folding on -(sys.maxsize+1)
00195         # i.e. -2147483648 on 32 bit platforms.  Should return int, not long.
00196         self.assertIsInstance(eval("%s" % (-sys.maxsize - 1)), int)
00197         self.assertIsInstance(eval("%s" % (-sys.maxsize - 2)), int)

Here is the call graph for this function:


Member Data Documentation

Definition at line 56 of file test_compile.py.


The documentation for this class was generated from the following file: