Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(7)

Unified Diff: Lib/test/test_ast.py

Issue 15452: Improve the security model for logging listener()
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/logging/config.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/test/test_ast.py Thu Oct 11 00:11:26 2012 -0700
+++ b/Lib/test/test_ast.py Thu Oct 11 16:47:49 2012 +0100
@@ -526,6 +526,173 @@
compile(mod, 'test', 'exec')
self.assertIn("invalid integer value: None", str(cm.exception))
+ def test_literal_eval_issue4907(self):
+ self.assertEqual(ast.literal_eval('2j'), 2j)
+ self.assertEqual(ast.literal_eval('10 + 2j'), 10 + 2j)
+ self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j)
+
+ def test_bad_integer(self):
+ # issue13436: Bad error message with invalid numeric values
+ body = [ast.ImportFrom(module='time',
+ names=[ast.alias(name='sleep')],
+ level=None,
+ lineno=None, col_offset=None)]
+ mod = ast.Module(body)
+ with self.assertRaises(ValueError) as cm:
+ compile(mod, 'test', 'exec')
+ self.assertIn("invalid integer value: None", str(cm.exception))
+
+ def test_lookup_eval_no_context(self):
+ compute = lambda s: ast.lookup_eval(s, {}, False)
+ compute_import = lambda s: ast.lookup_eval(s, {}, True)
+
+ # Do the same tests as for literal_eval
+
+ self.assertEqual(compute('[1, 2, 3]'), [1, 2, 3])
+ self.assertEqual(compute('{"foo": 42}'), {"foo": 42})
+ self.assertEqual(compute('(True, False, None)'), (True, False, None))
+ self.assertEqual(compute('{1, 2, 3}'), {1, 2, 3})
+ self.assertEqual(compute('b"hi"'), b"hi")
+ self.assertRaises(ValueError, compute, 'foo()')
+ self.assertEqual(compute('-6'), -6)
+ self.assertEqual(compute('-6j+3'), 3-6j)
+ self.assertEqual(compute('3.25'), 3.25)
+ self.assertEqual(compute('2j'), 2j)
+ self.assertEqual(compute('10 + 2j'), 10 + 2j)
+ self.assertEqual(compute('1.5 - 2j'), 1.5 - 2j)
+
+ # Add some for builtin and imported names
+ self.assertEqual(compute_import('ast'), ast)
+ self.assertEqual(compute_import('None'), None)
+ self.assertEqual(compute_import('True'), True)
+ self.assertEqual(compute_import('False'), False)
+ self.assertIs(compute_import('...'), Ellipsis)
+
+ self.assertRaises(ValueError, compute, 'ast')
+
+ def test_lookup_eval_with_context(self):
+ context = {
+ 'v1': [1, 2, 3],
+ 'v2': {"foo": 42},
+ 'v3': (True, False, None),
+ 'v4': {1, 2, 3},
+ 'v5': b"hi",
+ 'v6': 3 - 6j,
+ 'v7': 3.25,
+ 'v8': {1, 3, 5},
+ 'v9': 1.0,
+ 'v10': 1 + 2j,
+ 'v11': (None, True, False),
+ 'v12': b' there',
+ 'v13': 'ahoy ',
+ 'v14': 'matey',
+ 'v15': [4, 5, 6],
+ 'v16': 0x2222,
+ 'v17': 0x1111,
+ 'v18': 2,
+ 'v19': 4,
+ 'v20': False,
+ 'v21': True,
+ 'None': 'not really None',
+ 'Ellipsis': 'not really Ellipsis',
+ }
+ compute = lambda s: ast.lookup_eval(s, context, False)
+ compute_import = lambda s: ast.lookup_eval(s, context, True)
+
+ # Test name lookup
+ self.assertEqual(compute('v1'), [1, 2, 3])
+ self.assertEqual(compute('None'), None)
+ self.assertEqual(compute('False'), False)
+ self.assertEqual(compute('True'), True)
+ self.assertIs(compute('...'), Ellipsis)
+
+ # Test addition operator
+ self.assertEqual(compute('v1 + v15'), [1, 2, 3, 4, 5, 6])
+ self.assertEqual(compute('v3 + v11'), (True, False, None,
+ None, True, False))
+ self.assertEqual(compute('v7 + v9'), 4.25)
+ self.assertEqual(compute('v6 + v10'), 4 - 4j)
+ self.assertEqual(compute('v5 + v12'), b'hi there')
+ self.assertEqual(compute('v13 + v14'), 'ahoy matey')
+
+ # Test subtraction operator
+ self.assertEqual(compute('v7 - v9'), 2.25)
+ self.assertEqual(compute('v6 - v10'), 2 - 8j)
+ self.assertEqual(compute('v4 - v8'), {2})
+
+ # Test multiplication operator
+ self.assertEqual(compute('v18 * v19'), 8)
+ self.assertEqual(compute('v5 * v18'), b'hihi')
+ self.assertEqual(compute('v1 * v18'), [1, 2, 3, 1, 2, 3])
+
+ # Test division operator
+ self.assertEqual(compute('v19 / v18'), 2)
+ self.assertEqual(compute('v17 / v18'), 2184.5)
+ self.assertEqual(compute('v17 // v18'), 2184)
+
+ # Test modulo operator
+ self.assertEqual(compute('v17 % v19'), 1)
+ self.assertEqual(compute('"%d" % v19'), '4')
+
+ # Test power operator
+ self.assertEqual(compute('v18 ** v19'), 16)
+ self.assertEqual(compute('v7 ** v18'), 10.5625)
+
+ # Test bitwise operators
+ self.assertEqual(compute('v16 | v17'), 0x3333)
+ self.assertEqual(compute('v4 | v8'), {1, 2, 3, 5})
+ self.assertEqual(compute('v16 & v17'), 0)
+ self.assertEqual(compute('v4 & v8'), {1, 3})
+ self.assertEqual(compute('v16 ^ v17'), 0x3333)
+ self.assertEqual(compute('v4 ^ v8'), {2, 5})
+ self.assertEqual(compute('v16 << v18'), 0x2222 << 2)
+ self.assertEqual(compute('v16 >> v18'), 0x2222 >> 2)
+
+ # Test relational operators
+ self.assertEqual(compute('v18 == v18'), True)
+ self.assertEqual(compute('v18 == v19'), False)
+ self.assertEqual(compute('v18 != v18'), False)
+ self.assertEqual(compute('v18 != v19'), True)
+ self.assertEqual(compute('v18 < v19'), True)
+ self.assertEqual(compute('v18 <= v19'), True)
+ self.assertEqual(compute('v19 <= v19'), True)
+ self.assertEqual(compute('v19 > v18'), True)
+ self.assertEqual(compute('v19 >= v18'), True)
+ self.assertEqual(compute('v19 >= v19'), True)
+
+ # Test membership operators
+ self.assertEqual(compute('v18 in v1'), True)
+ self.assertEqual(compute('v19 in v1'), False)
+ self.assertEqual(compute('v18 not in v1'), False)
+ self.assertEqual(compute('v19 not in v1'), True)
+ self.assertEqual(compute('v18 in v4'), True)
+
+ # Test unary operators
+ self.assertEqual(compute('-v18'), -2)
+ self.assertEqual(compute('+v19'), 4)
+ self.assertEqual(compute('not v20'), True)
+ self.assertEqual(compute('~v18'), -3)
+
+ # Test attribute access
+ self.assertIs(compute_import('ast.parse'), ast.parse)
+
+ # Test array indexing and slicing
+ self.assertEqual(compute('v1[v18]'), 3)
+ self.assertEqual(compute('v1[::-1]'), [3, 2, 1])
+ self.assertEqual(compute('"abc"[::-1]'), "cba")
+
+ # Test logical operators
+ self.assertEqual(compute('v20 or v21'), True)
+ self.assertEqual(compute('v20 and v21'), False)
+ self.assertEqual(compute('v21 or v20'), True)
+ self.assertEqual(compute('v21 and v20'), False)
+
+ # Error cases
+ self.assertRaises(TypeError, compute, 'v4 + v8') # can't add sets
+ self.assertRaises(ValueError, compute, 'v4 + ') # bad syntax
+ # function calls are not supported ...
+ self.assertRaises(ValueError, compute_import, 'cgi.print_directory()')
+ self.assertRaises(ValueError, compute, 'v99') # unknown name
class ASTValidatorTests(unittest.TestCase):
« no previous file with comments | « Lib/logging/config.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+