blob: c9f2835e3cf81796edf92611d1ff9f89dc78fcd8 [file] [log] [blame]
Antoine Pitrouab4a6912014-05-23 11:46:03 +02001import math
Raymond Hettinger8a99b502003-06-23 13:36:57 +00002import unittest
Raymond Hettinger8a99b502003-06-23 13:36:57 +00003import sys
Georg Brandlfc8eef32008-03-28 12:11:56 +00004import _ast
Raymond Hettinger8a99b502003-06-23 13:36:57 +00005from test import test_support
Serhiy Storchaka5d7d26c2015-11-14 15:14:29 +02006from test import script_helper
7import os
8import tempfile
Florent Xiclunaaf617192010-03-21 11:03:21 +00009import textwrap
Peter Schneider-Kampfdee0f02000-07-25 22:15:45 +000010
Raymond Hettinger8a99b502003-06-23 13:36:57 +000011class TestSpecifics(unittest.TestCase):
Jeremy Hylton778e2652001-11-09 19:50:08 +000012
Benjamin Petersone36199b2009-11-12 23:39:44 +000013 def test_no_ending_newline(self):
14 compile("hi", "<test>", "exec")
15 compile("hi\r", "<test>", "exec")
16
17 def test_empty(self):
18 compile("", "<test>", "exec")
19
20 def test_other_newlines(self):
21 compile("\r\n", "<test>", "exec")
22 compile("\r", "<test>", "exec")
23 compile("hi\r\nstuff\r\ndef f():\n pass\r", "<test>", "exec")
24 compile("this_is\rreally_old_mac\rdef f():\n pass", "<test>", "exec")
25
Raymond Hettinger8a99b502003-06-23 13:36:57 +000026 def test_debug_assignment(self):
27 # catch assignments to __debug__
28 self.assertRaises(SyntaxError, compile, '__debug__ = 1', '?', 'single')
29 import __builtin__
30 prev = __builtin__.__debug__
31 setattr(__builtin__, '__debug__', 'sure')
32 setattr(__builtin__, '__debug__', prev)
Jeremy Hylton778e2652001-11-09 19:50:08 +000033
Raymond Hettinger8a99b502003-06-23 13:36:57 +000034 def test_argument_handling(self):
35 # detect duplicate positional and keyword arguments
36 self.assertRaises(SyntaxError, eval, 'lambda a,a:0')
37 self.assertRaises(SyntaxError, eval, 'lambda a,a=1:0')
38 self.assertRaises(SyntaxError, eval, 'lambda a=1,a=1:0')
39 try:
40 exec 'def f(a, a): pass'
41 self.fail("duplicate arguments")
42 except SyntaxError:
43 pass
44 try:
45 exec 'def f(a = 0, a = 1): pass'
46 self.fail("duplicate keyword arguments")
47 except SyntaxError:
48 pass
49 try:
50 exec 'def f(a): global a; a = 1'
51 self.fail("variable is global and local")
52 except SyntaxError:
53 pass
Jeremy Hylton778e2652001-11-09 19:50:08 +000054
Raymond Hettinger8a99b502003-06-23 13:36:57 +000055 def test_syntax_error(self):
56 self.assertRaises(SyntaxError, compile, "1+*3", "filename", "exec")
Peter Schneider-Kampfdee0f02000-07-25 22:15:45 +000057
Georg Brandle06cf452007-06-07 13:23:24 +000058 def test_none_keyword_arg(self):
59 self.assertRaises(SyntaxError, compile, "f(None=1)", "<string>", "exec")
60
Raymond Hettinger8a99b502003-06-23 13:36:57 +000061 def test_duplicate_global_local(self):
62 try:
63 exec 'def f(a): global a; a = 1'
64 self.fail("variable is global and local")
65 except SyntaxError:
66 pass
Peter Schneider-Kampfdee0f02000-07-25 22:15:45 +000067
Mark Dickinson16587972012-11-25 13:25:57 +000068 def test_exec_functional_style(self):
69 # Exec'ing a tuple of length 2 works.
70 g = {'b': 2}
71 exec("a = b + 1", g)
72 self.assertEqual(g['a'], 3)
73
74 # As does exec'ing a tuple of length 3.
75 l = {'b': 3}
76 g = {'b': 5, 'c': 7}
77 exec("a = b + c", g, l)
78 self.assertNotIn('a', g)
79 self.assertEqual(l['a'], 10)
80
81 # Tuples not of length 2 or 3 are invalid.
82 with self.assertRaises(TypeError):
83 exec("a = b + 1",)
84
85 with self.assertRaises(TypeError):
86 exec("a = b + 1", {}, {}, {})
87
88 # Can't mix and match the two calling forms.
89 g = {'a': 3, 'b': 4}
90 l = {}
91 with self.assertRaises(TypeError):
92 exec("a = b + 1", g) in g
93 with self.assertRaises(TypeError):
94 exec("a = b + 1", g, l) in g, l
95
Robert Jordensaf09c772014-07-29 17:24:24 +020096 def test_nested_qualified_exec(self):
97 # Can use qualified exec in nested functions.
98 code = ["""
99def g():
100 def f():
101 if True:
102 exec "" in {}, {}
103 """, """
104def g():
105 def f():
106 if True:
107 exec("", {}, {})
Benjamin Peterson4f09e612014-08-09 19:39:50 -0700108 """, """
109def g():
110 def f():
111 if True:
112 exec("", {})
Robert Jordensaf09c772014-07-29 17:24:24 +0200113 """]
114 for c in code:
115 compile(c, "<code>", "exec")
116
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000117 def test_exec_with_general_mapping_for_locals(self):
118
119 class M:
120 "Test mapping interface versus possible calls from eval()."
121 def __getitem__(self, key):
122 if key == 'a':
123 return 12
124 raise KeyError
125 def __setitem__(self, key, value):
126 self.results = (key, value)
127 def keys(self):
128 return list('xyz')
129
130 m = M()
131 g = globals()
132 exec 'z = a' in g, m
133 self.assertEqual(m.results, ('z', 12))
134 try:
135 exec 'z = b' in g, m
136 except NameError:
137 pass
138 else:
139 self.fail('Did not detect a KeyError')
140 exec 'z = dir()' in g, m
141 self.assertEqual(m.results, ('z', list('xyz')))
142 exec 'z = globals()' in g, m
143 self.assertEqual(m.results, ('z', g))
144 exec 'z = locals()' in g, m
145 self.assertEqual(m.results, ('z', m))
146 try:
147 exec 'z = b' in m
148 except TypeError:
149 pass
150 else:
151 self.fail('Did not validate globals as a real dict')
152
153 class A:
154 "Non-mapping"
155 pass
156 m = A()
157 try:
158 exec 'z = a' in g, m
159 except TypeError:
160 pass
161 else:
162 self.fail('Did not validate locals as a mapping')
163
164 # Verify that dict subclasses work as well
165 class D(dict):
166 def __getitem__(self, key):
167 if key == 'a':
168 return 12
169 return dict.__getitem__(self, key)
170 d = D()
171 exec 'z = a' in g, d
172 self.assertEqual(d['z'], 12)
173
Neal Norwitz6ab080c2005-10-24 00:08:10 +0000174 def test_extended_arg(self):
175 longexpr = 'x = x or ' + '-x' * 2500
176 code = '''
177def f(x):
178 %s
179 %s
180 %s
181 %s
182 %s
183 %s
184 %s
185 %s
186 %s
187 %s
188 # the expressions above have no effect, x == argument
189 while x:
190 x -= 1
191 # EXTENDED_ARG/JUMP_ABSOLUTE here
192 return x
193''' % ((longexpr,)*10)
194 exec code
195 self.assertEqual(f(5), 0)
196
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000197 def test_complex_args(self):
Senthil Kumarance8e33a2010-01-08 19:04:16 +0000198
Florent Xiclunaaf617192010-03-21 11:03:21 +0000199 with test_support.check_py3k_warnings(
200 ("tuple parameter unpacking has been removed", SyntaxWarning)):
201 exec textwrap.dedent('''
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000202 def comp_args((a, b)):
203 return a,b
204 self.assertEqual(comp_args((1, 2)), (1, 2))
Thomas Heller6b17abf2002-07-09 09:23:27 +0000205
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000206 def comp_args((a, b)=(3, 4)):
207 return a, b
208 self.assertEqual(comp_args((1, 2)), (1, 2))
209 self.assertEqual(comp_args(), (3, 4))
Jeremy Hylton047e2c92001-01-19 03:25:56 +0000210
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000211 def comp_args(a, (b, c)):
212 return a, b, c
213 self.assertEqual(comp_args(1, (2, 3)), (1, 2, 3))
Jeremy Hylton121b6eb2001-02-19 23:53:42 +0000214
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000215 def comp_args(a=2, (b, c)=(3, 4)):
216 return a, b, c
217 self.assertEqual(comp_args(1, (2, 3)), (1, 2, 3))
218 self.assertEqual(comp_args(), (2, 3, 4))
Florent Xiclunaaf617192010-03-21 11:03:21 +0000219 ''')
Jeremy Hylton121b6eb2001-02-19 23:53:42 +0000220
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000221 def test_argument_order(self):
222 try:
223 exec 'def f(a=1, (b, c)): pass'
224 self.fail("non-default args after default")
225 except SyntaxError:
226 pass
Jeremy Hylton121b6eb2001-02-19 23:53:42 +0000227
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000228 def test_float_literals(self):
229 # testing bad float literals
230 self.assertRaises(SyntaxError, eval, "2e")
231 self.assertRaises(SyntaxError, eval, "2.0e+")
232 self.assertRaises(SyntaxError, eval, "1e-")
233 self.assertRaises(SyntaxError, eval, "3-4e/21")
Jeremy Hylton121b6eb2001-02-19 23:53:42 +0000234
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000235 def test_indentation(self):
236 # testing compile() of indented block w/o trailing newline"
237 s = """
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000238if 1:
239 if 2:
240 pass"""
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000241 compile(s, "<string>", "exec")
242
Neal Norwitzed657552006-07-10 00:04:44 +0000243 # This test is probably specific to CPython and may not generalize
244 # to other implementations. We are trying to ensure that when
245 # the first line of code starts after 256, correct line numbers
246 # in tracebacks are still produced.
247 def test_leading_newlines(self):
248 s256 = "".join(["\n"] * 256 + ["spam"])
249 co = compile(s256, 'fn', 'exec')
250 self.assertEqual(co.co_firstlineno, 257)
251 self.assertEqual(co.co_lnotab, '')
252
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000253 def test_literals_with_leading_zeroes(self):
254 for arg in ["077787", "0xj", "0x.", "0e", "090000000000000",
Eric Smith9ff19b52008-03-17 17:32:20 +0000255 "080000000000000", "000000000000009", "000000000000008",
256 "0b42", "0BADCAFE", "0o123456789", "0b1.1", "0o4.2",
Amaury Forgeot d'Arc52167212008-04-24 18:07:05 +0000257 "0b101j2", "0o153j2", "0b100e1", "0o777e1", "0o8", "0o78"]:
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000258 self.assertRaises(SyntaxError, eval, arg)
259
260 self.assertEqual(eval("0777"), 511)
261 self.assertEqual(eval("0777L"), 511)
262 self.assertEqual(eval("000777"), 511)
263 self.assertEqual(eval("0xff"), 255)
264 self.assertEqual(eval("0xffL"), 255)
265 self.assertEqual(eval("0XfF"), 255)
266 self.assertEqual(eval("0777."), 777)
267 self.assertEqual(eval("0777.0"), 777)
268 self.assertEqual(eval("000000000000000000000000000000000000000000000000000777e0"), 777)
269 self.assertEqual(eval("0777e1"), 7770)
270 self.assertEqual(eval("0e0"), 0)
271 self.assertEqual(eval("0000E-012"), 0)
272 self.assertEqual(eval("09.5"), 9.5)
273 self.assertEqual(eval("0777j"), 777j)
274 self.assertEqual(eval("00j"), 0j)
275 self.assertEqual(eval("00.0"), 0)
276 self.assertEqual(eval("0e3"), 0)
277 self.assertEqual(eval("090000000000000."), 90000000000000.)
278 self.assertEqual(eval("090000000000000.0000000000000000000000"), 90000000000000.)
279 self.assertEqual(eval("090000000000000e0"), 90000000000000.)
280 self.assertEqual(eval("090000000000000e-0"), 90000000000000.)
281 self.assertEqual(eval("090000000000000j"), 90000000000000j)
282 self.assertEqual(eval("000000000000007"), 7)
283 self.assertEqual(eval("000000000000008."), 8.)
284 self.assertEqual(eval("000000000000009."), 9.)
Eric Smith9ff19b52008-03-17 17:32:20 +0000285 self.assertEqual(eval("0b101010"), 42)
286 self.assertEqual(eval("-0b000000000010"), -2)
287 self.assertEqual(eval("0o777"), 511)
288 self.assertEqual(eval("-0o0000010"), -8)
Mark Dickinson64b7e502008-07-16 09:40:03 +0000289 self.assertEqual(eval("020000000000.0"), 20000000000.0)
290 self.assertEqual(eval("037777777777e0"), 37777777777.0)
291 self.assertEqual(eval("01000000000000000000000.0"),
292 1000000000000000000000.0)
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000293
294 def test_unary_minus(self):
295 # Verify treatment of unary minus on negative numbers SF bug #660455
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000296 if sys.maxint == 2147483647:
297 # 32-bit machine
298 all_one_bits = '0xffffffff'
299 self.assertEqual(eval(all_one_bits), 4294967295L)
300 self.assertEqual(eval("-" + all_one_bits), -4294967295L)
301 elif sys.maxint == 9223372036854775807:
302 # 64-bit machine
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000303 all_one_bits = '0xffffffffffffffff'
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000304 self.assertEqual(eval(all_one_bits), 18446744073709551615L)
305 self.assertEqual(eval("-" + all_one_bits), -18446744073709551615L)
306 else:
307 self.fail("How many bits *does* this machine have???")
Ezio Melotti24b07bc2011-03-15 18:55:01 +0200308 # Verify treatment of constant folding on -(sys.maxint+1)
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +0000309 # i.e. -2147483648 on 32 bit platforms. Should return int, not long.
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000310 self.assertIsInstance(eval("%s" % (-sys.maxint - 1)), int)
311 self.assertIsInstance(eval("%s" % (-sys.maxint - 2)), long)
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000312
Neal Norwitz28746ab2006-07-09 22:14:42 +0000313 if sys.maxint == 9223372036854775807:
314 def test_32_63_bit_values(self):
315 a = +4294967296 # 1 << 32
316 b = -4294967296 # 1 << 32
317 c = +281474976710656 # 1 << 48
318 d = -281474976710656 # 1 << 48
319 e = +4611686018427387904 # 1 << 62
320 f = -4611686018427387904 # 1 << 62
321 g = +9223372036854775807 # 1 << 63 - 1
322 h = -9223372036854775807 # 1 << 63 - 1
323
324 for variable in self.test_32_63_bit_values.func_code.co_consts:
325 if variable is not None:
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000326 self.assertIsInstance(variable, int)
Neal Norwitz28746ab2006-07-09 22:14:42 +0000327
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000328 def test_sequence_unpacking_error(self):
329 # Verify sequence packing/unpacking with "or". SF bug #757818
330 i,j = (1, -1) or (-1, 1)
331 self.assertEqual(i, 1)
332 self.assertEqual(j, -1)
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000333
Raymond Hettinger11a70c72004-07-17 21:46:25 +0000334 def test_none_assignment(self):
335 stmts = [
336 'None = 0',
337 'None += 0',
338 '__builtins__.None = 0',
339 'def None(): pass',
340 'class None: pass',
341 '(a, None) = 0, 0',
342 'for None in range(10): pass',
343 'def f(None): pass',
Benjamin Peterson565e1b62009-06-13 03:46:30 +0000344 'import None',
345 'import x as None',
346 'from x import None',
347 'from x import y as None'
Raymond Hettinger11a70c72004-07-17 21:46:25 +0000348 ]
349 for stmt in stmts:
350 stmt += "\n"
351 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single')
352 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
Benjamin Peterson565e1b62009-06-13 03:46:30 +0000353 # This is ok.
354 compile("from None import x", "tmp", "exec")
Benjamin Petersond1f5a592009-06-13 13:06:21 +0000355 compile("from x import None as y", "tmp", "exec")
356 compile("import None as x", "tmp", "exec")
Tim Petersd507dab2001-08-30 20:51:59 +0000357
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000358 def test_import(self):
359 succeed = [
360 'import sys',
361 'import os, sys',
Neal Norwitzfb48afa2006-07-08 05:31:37 +0000362 'import os as bar',
363 'import os.path as bar',
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000364 'from __future__ import nested_scopes, generators',
365 'from __future__ import (nested_scopes,\ngenerators)',
366 'from __future__ import (nested_scopes,\ngenerators,)',
367 'from sys import stdin, stderr, stdout',
368 'from sys import (stdin, stderr,\nstdout)',
369 'from sys import (stdin, stderr,\nstdout,)',
370 'from sys import (stdin\n, stderr, stdout)',
371 'from sys import (stdin\n, stderr, stdout,)',
372 'from sys import stdin as si, stdout as so, stderr as se',
373 'from sys import (stdin as si, stdout as so, stderr as se)',
374 'from sys import (stdin as si, stdout as so, stderr as se,)',
375 ]
376 fail = [
377 'import (os, sys)',
378 'import (os), (sys)',
379 'import ((os), (sys))',
380 'import (sys',
381 'import sys)',
382 'import (os,)',
Neal Norwitzfb48afa2006-07-08 05:31:37 +0000383 'import os As bar',
384 'import os.path a bar',
Georg Brandl9575fb22006-07-08 12:15:27 +0000385 'from sys import stdin As stdout',
386 'from sys import stdin a stdout',
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000387 'from (sys) import stdin',
388 'from __future__ import (nested_scopes',
389 'from __future__ import nested_scopes)',
390 'from __future__ import nested_scopes,\ngenerators',
391 'from sys import (stdin',
392 'from sys import stdin)',
393 'from sys import stdin, stdout,\nstderr',
394 'from sys import stdin si',
395 'from sys import stdin,'
396 'from sys import (*)',
397 'from sys import (stdin,, stdout, stderr)',
398 'from sys import (stdin, stdout),',
399 ]
400 for stmt in succeed:
401 compile(stmt, 'tmp', 'exec')
402 for stmt in fail:
403 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
404
Raymond Hettinger9047c8f2004-10-24 00:10:06 +0000405 def test_for_distinct_code_objects(self):
406 # SF bug 1048870
407 def f():
408 f1 = lambda x=1: x
409 f2 = lambda x=2: x
410 return f1, f2
411 f1, f2 = f()
412 self.assertNotEqual(id(f1.func_code), id(f2.func_code))
413
Benjamin Peterson0dee9c12010-03-17 20:41:42 +0000414 def test_lambda_doc(self):
415 l = lambda: "foo"
416 self.assertIsNone(l.__doc__)
417
Serhiy Storchaka3eb554f2014-09-05 10:22:05 +0300418 @test_support.requires_unicode
419 def test_encoding(self):
420 code = b'# -*- coding: badencoding -*-\npass\n'
421 self.assertRaises(SyntaxError, compile, code, 'tmp', 'exec')
Neal Norwitze98ccf62006-03-23 05:39:47 +0000422 code = u"# -*- coding: utf-8 -*-\npass\n"
423 self.assertRaises(SyntaxError, compile, code, "tmp", "exec")
Serhiy Storchaka3eb554f2014-09-05 10:22:05 +0300424 code = 'u"\xc2\xa4"\n'
425 self.assertEqual(eval(code), u'\xc2\xa4')
426 code = u'u"\xc2\xa4"\n'
427 self.assertEqual(eval(code), u'\xc2\xa4')
428 code = '# -*- coding: latin1 -*-\nu"\xc2\xa4"\n'
429 self.assertEqual(eval(code), u'\xc2\xa4')
430 code = '# -*- coding: utf-8 -*-\nu"\xc2\xa4"\n'
431 self.assertEqual(eval(code), u'\xa4')
432 code = '# -*- coding: iso8859-15 -*-\nu"\xc2\xa4"\n'
433 self.assertEqual(eval(code), test_support.u(r'\xc2\u20ac'))
434 code = 'u"""\\\n# -*- coding: utf-8 -*-\n\xc2\xa4"""\n'
435 self.assertEqual(eval(code), u'# -*- coding: utf-8 -*-\n\xc2\xa4')
Neal Norwitze98ccf62006-03-23 05:39:47 +0000436
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000437 def test_subscripts(self):
438 # SF bug 1448804
439 # Class to make testing subscript results easy
440 class str_map(object):
441 def __init__(self):
442 self.data = {}
443 def __getitem__(self, key):
444 return self.data[str(key)]
445 def __setitem__(self, key, value):
446 self.data[str(key)] = value
447 def __delitem__(self, key):
448 del self.data[str(key)]
449 def __contains__(self, key):
450 return str(key) in self.data
451 d = str_map()
452 # Index
453 d[1] = 1
454 self.assertEqual(d[1], 1)
455 d[1] += 1
456 self.assertEqual(d[1], 2)
457 del d[1]
Ezio Melottiaa980582010-01-23 23:04:36 +0000458 self.assertNotIn(1, d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000459 # Tuple of indices
460 d[1, 1] = 1
461 self.assertEqual(d[1, 1], 1)
462 d[1, 1] += 1
463 self.assertEqual(d[1, 1], 2)
464 del d[1, 1]
Ezio Melottiaa980582010-01-23 23:04:36 +0000465 self.assertNotIn((1, 1), d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000466 # Simple slice
467 d[1:2] = 1
468 self.assertEqual(d[1:2], 1)
469 d[1:2] += 1
470 self.assertEqual(d[1:2], 2)
471 del d[1:2]
Ezio Melottiaa980582010-01-23 23:04:36 +0000472 self.assertNotIn(slice(1, 2), d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000473 # Tuple of simple slices
474 d[1:2, 1:2] = 1
475 self.assertEqual(d[1:2, 1:2], 1)
476 d[1:2, 1:2] += 1
477 self.assertEqual(d[1:2, 1:2], 2)
478 del d[1:2, 1:2]
Ezio Melottiaa980582010-01-23 23:04:36 +0000479 self.assertNotIn((slice(1, 2), slice(1, 2)), d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000480 # Extended slice
481 d[1:2:3] = 1
482 self.assertEqual(d[1:2:3], 1)
483 d[1:2:3] += 1
484 self.assertEqual(d[1:2:3], 2)
485 del d[1:2:3]
Ezio Melottiaa980582010-01-23 23:04:36 +0000486 self.assertNotIn(slice(1, 2, 3), d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000487 # Tuple of extended slices
488 d[1:2:3, 1:2:3] = 1
489 self.assertEqual(d[1:2:3, 1:2:3], 1)
490 d[1:2:3, 1:2:3] += 1
491 self.assertEqual(d[1:2:3, 1:2:3], 2)
492 del d[1:2:3, 1:2:3]
Ezio Melottiaa980582010-01-23 23:04:36 +0000493 self.assertNotIn((slice(1, 2, 3), slice(1, 2, 3)), d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000494 # Ellipsis
495 d[...] = 1
496 self.assertEqual(d[...], 1)
497 d[...] += 1
498 self.assertEqual(d[...], 2)
499 del d[...]
Ezio Melottiaa980582010-01-23 23:04:36 +0000500 self.assertNotIn(Ellipsis, d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000501 # Tuple of Ellipses
502 d[..., ...] = 1
503 self.assertEqual(d[..., ...], 1)
504 d[..., ...] += 1
505 self.assertEqual(d[..., ...], 2)
506 del d[..., ...]
Ezio Melottiaa980582010-01-23 23:04:36 +0000507 self.assertNotIn((Ellipsis, Ellipsis), d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000508
Jeremy Hylton37075c52007-02-27 01:01:59 +0000509 def test_mangling(self):
510 class A:
511 def f():
512 __mangled = 1
513 __not_mangled__ = 2
514 import __mangled_mod
515 import __package__.module
516
Ezio Melottiaa980582010-01-23 23:04:36 +0000517 self.assertIn("_A__mangled", A.f.func_code.co_varnames)
518 self.assertIn("__not_mangled__", A.f.func_code.co_varnames)
519 self.assertIn("_A__mangled_mod", A.f.func_code.co_varnames)
520 self.assertIn("__package__", A.f.func_code.co_varnames)
Jeremy Hylton37075c52007-02-27 01:01:59 +0000521
Georg Brandlfc8eef32008-03-28 12:11:56 +0000522 def test_compile_ast(self):
523 fname = __file__
524 if fname.lower().endswith(('pyc', 'pyo')):
525 fname = fname[:-1]
526 with open(fname, 'r') as f:
527 fcontents = f.read()
528 sample_code = [
529 ['<assign>', 'x = 5'],
530 ['<print1>', 'print 1'],
531 ['<printv>', 'print v'],
532 ['<printTrue>', 'print True'],
533 ['<printList>', 'print []'],
534 ['<ifblock>', """if True:\n pass\n"""],
535 ['<forblock>', """for n in [1, 2, 3]:\n print n\n"""],
536 ['<deffunc>', """def foo():\n pass\nfoo()\n"""],
537 [fname, fcontents],
538 ]
539
540 for fname, code in sample_code:
541 co1 = compile(code, '%s1' % fname, 'exec')
542 ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000543 self.assertTrue(type(ast) == _ast.Module)
Georg Brandlfc8eef32008-03-28 12:11:56 +0000544 co2 = compile(ast, '%s3' % fname, 'exec')
545 self.assertEqual(co1, co2)
Georg Brandlf2bfd542008-03-29 13:24:23 +0000546 # the code object's filename comes from the second compilation step
547 self.assertEqual(co2.co_filename, '%s3' % fname)
548
549 # raise exception when node type doesn't match with compile mode
550 co1 = compile('print 1', '<string>', 'exec', _ast.PyCF_ONLY_AST)
551 self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')
552
553 # raise exception when node type is no start node
554 self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')
555
556 # raise exception when node has invalid children
557 ast = _ast.Module()
558 ast.body = [_ast.BoolOp()]
559 self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
Georg Brandlfc8eef32008-03-28 12:11:56 +0000560
Serhiy Storchaka5d7d26c2015-11-14 15:14:29 +0200561 def test_yet_more_evil_still_undecodable(self):
562 # Issue #25388
563 src = b"#\x00\n#\xfd\n"
564 tmpd = tempfile.mkdtemp()
565 try:
566 fn = os.path.join(tmpd, "bad.py")
567 with open(fn, "wb") as fp:
568 fp.write(src)
569 rc, out, err = script_helper.assert_python_failure(fn)
570 finally:
571 test_support.rmtree(tmpd)
572 self.assertIn(b"Non-ASCII", err)
573
Serhiy Storchaka61565602015-11-20 21:56:21 +0200574 def test_null_terminated(self):
575 # The source code is null-terminated internally, but bytes-like
576 # objects are accepted, which could be not terminated.
577 with self.assertRaisesRegexp(TypeError, "without null bytes"):
578 compile(u"123\x00", "<dummy>", "eval")
Serhiy Storchaka43e90072015-11-29 20:13:56 +0200579 with test_support.check_py3k_warnings():
580 with self.assertRaisesRegexp(TypeError, "without null bytes"):
581 compile(buffer("123\x00"), "<dummy>", "eval")
582 code = compile(buffer("123\x00", 1, 2), "<dummy>", "eval")
583 self.assertEqual(eval(code), 23)
584 code = compile(buffer("1234", 1, 2), "<dummy>", "eval")
585 self.assertEqual(eval(code), 23)
586 code = compile(buffer("$23$", 1, 2), "<dummy>", "eval")
587 self.assertEqual(eval(code), 23)
Georg Brandlfc8eef32008-03-28 12:11:56 +0000588
Antoine Pitrouab4a6912014-05-23 11:46:03 +0200589class TestStackSize(unittest.TestCase):
590 # These tests check that the computed stack size for a code object
591 # stays within reasonable bounds (see issue #21523 for an example
592 # dysfunction).
593 N = 100
594
595 def check_stack_size(self, code):
596 # To assert that the alleged stack size is not O(N), we
597 # check that it is smaller than log(N).
598 if isinstance(code, str):
599 code = compile(code, "<foo>", "single")
600 max_size = math.ceil(math.log(len(code.co_code)))
601 self.assertLessEqual(code.co_stacksize, max_size)
602
603 def test_and(self):
604 self.check_stack_size("x and " * self.N + "x")
605
606 def test_or(self):
607 self.check_stack_size("x or " * self.N + "x")
608
609 def test_and_or(self):
610 self.check_stack_size("x and x or " * self.N + "x")
611
612 def test_chained_comparison(self):
613 self.check_stack_size("x < " * self.N + "x")
614
615 def test_if_else(self):
616 self.check_stack_size("x if x else " * self.N + "x")
617
618 def test_binop(self):
619 self.check_stack_size("x + " * self.N + "x")
620
621 def test_func_and(self):
622 code = "def f(x):\n"
623 code += " x and x\n" * self.N
624 self.check_stack_size(code)
625
626
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000627def test_main():
Antoine Pitrouab4a6912014-05-23 11:46:03 +0200628 test_support.run_unittest(__name__)
Tim Petersd507dab2001-08-30 20:51:59 +0000629
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000630if __name__ == "__main__":
Antoine Pitrouab4a6912014-05-23 11:46:03 +0200631 unittest.main()