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

Side by Side Diff: Lib/test/test_ast.py

Issue 15452: Improve the security model for logging listener()
Patch Set: Created 7 years, 3 months ago
Left:
Right:
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 unified diff | Download patch
« no previous file with comments | « Lib/logging/config.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import os 1 import os
2 import sys 2 import sys
3 import unittest 3 import unittest
4 import ast 4 import ast
5 import weakref 5 import weakref
6 6
7 from test import support 7 from test import support
8 8
9 def to_tuple(t): 9 def to_tuple(t):
10 if t is None or isinstance(t, (str, int, complex)): 10 if t is None or isinstance(t, (str, int, complex)):
(...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 # issue13436: Bad error message with invalid numeric values 519 # issue13436: Bad error message with invalid numeric values
520 body = [ast.ImportFrom(module='time', 520 body = [ast.ImportFrom(module='time',
521 names=[ast.alias(name='sleep')], 521 names=[ast.alias(name='sleep')],
522 level=None, 522 level=None,
523 lineno=None, col_offset=None)] 523 lineno=None, col_offset=None)]
524 mod = ast.Module(body) 524 mod = ast.Module(body)
525 with self.assertRaises(ValueError) as cm: 525 with self.assertRaises(ValueError) as cm:
526 compile(mod, 'test', 'exec') 526 compile(mod, 'test', 'exec')
527 self.assertIn("invalid integer value: None", str(cm.exception)) 527 self.assertIn("invalid integer value: None", str(cm.exception))
528 528
529 def test_literal_eval_issue4907(self):
530 self.assertEqual(ast.literal_eval('2j'), 2j)
531 self.assertEqual(ast.literal_eval('10 + 2j'), 10 + 2j)
532 self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j)
533
534 def test_bad_integer(self):
535 # issue13436: Bad error message with invalid numeric values
536 body = [ast.ImportFrom(module='time',
537 names=[ast.alias(name='sleep')],
538 level=None,
539 lineno=None, col_offset=None)]
540 mod = ast.Module(body)
541 with self.assertRaises(ValueError) as cm:
542 compile(mod, 'test', 'exec')
543 self.assertIn("invalid integer value: None", str(cm.exception))
544
545 def test_lookup_eval_no_context(self):
546 compute = lambda s: ast.lookup_eval(s, {}, False)
547 compute_import = lambda s: ast.lookup_eval(s, {}, True)
548
549 # Do the same tests as for literal_eval
550
551 self.assertEqual(compute('[1, 2, 3]'), [1, 2, 3])
552 self.assertEqual(compute('{"foo": 42}'), {"foo": 42})
553 self.assertEqual(compute('(True, False, None)'), (True, False, None))
554 self.assertEqual(compute('{1, 2, 3}'), {1, 2, 3})
555 self.assertEqual(compute('b"hi"'), b"hi")
556 self.assertRaises(ValueError, compute, 'foo()')
557 self.assertEqual(compute('-6'), -6)
558 self.assertEqual(compute('-6j+3'), 3-6j)
559 self.assertEqual(compute('3.25'), 3.25)
560 self.assertEqual(compute('2j'), 2j)
561 self.assertEqual(compute('10 + 2j'), 10 + 2j)
562 self.assertEqual(compute('1.5 - 2j'), 1.5 - 2j)
563
564 # Add some for builtin and imported names
565 self.assertEqual(compute_import('ast'), ast)
566 self.assertEqual(compute_import('None'), None)
567 self.assertEqual(compute_import('True'), True)
568 self.assertEqual(compute_import('False'), False)
569 self.assertIs(compute_import('...'), Ellipsis)
570
571 self.assertRaises(ValueError, compute, 'ast')
572
573 def test_lookup_eval_with_context(self):
574 context = {
575 'v1': [1, 2, 3],
576 'v2': {"foo": 42},
577 'v3': (True, False, None),
578 'v4': {1, 2, 3},
579 'v5': b"hi",
580 'v6': 3 - 6j,
581 'v7': 3.25,
582 'v8': {1, 3, 5},
583 'v9': 1.0,
584 'v10': 1 + 2j,
585 'v11': (None, True, False),
586 'v12': b' there',
587 'v13': 'ahoy ',
588 'v14': 'matey',
589 'v15': [4, 5, 6],
590 'v16': 0x2222,
591 'v17': 0x1111,
592 'v18': 2,
593 'v19': 4,
594 'v20': False,
595 'v21': True,
596 'None': 'not really None',
597 'Ellipsis': 'not really Ellipsis',
598 }
599 compute = lambda s: ast.lookup_eval(s, context, False)
600 compute_import = lambda s: ast.lookup_eval(s, context, True)
601
602 # Test name lookup
603 self.assertEqual(compute('v1'), [1, 2, 3])
604 self.assertEqual(compute('None'), None)
605 self.assertEqual(compute('False'), False)
606 self.assertEqual(compute('True'), True)
607 self.assertIs(compute('...'), Ellipsis)
608
609 # Test addition operator
610 self.assertEqual(compute('v1 + v15'), [1, 2, 3, 4, 5, 6])
611 self.assertEqual(compute('v3 + v11'), (True, False, None,
612 None, True, False))
613 self.assertEqual(compute('v7 + v9'), 4.25)
614 self.assertEqual(compute('v6 + v10'), 4 - 4j)
615 self.assertEqual(compute('v5 + v12'), b'hi there')
616 self.assertEqual(compute('v13 + v14'), 'ahoy matey')
617
618 # Test subtraction operator
619 self.assertEqual(compute('v7 - v9'), 2.25)
620 self.assertEqual(compute('v6 - v10'), 2 - 8j)
621 self.assertEqual(compute('v4 - v8'), {2})
622
623 # Test multiplication operator
624 self.assertEqual(compute('v18 * v19'), 8)
625 self.assertEqual(compute('v5 * v18'), b'hihi')
626 self.assertEqual(compute('v1 * v18'), [1, 2, 3, 1, 2, 3])
627
628 # Test division operator
629 self.assertEqual(compute('v19 / v18'), 2)
630 self.assertEqual(compute('v17 / v18'), 2184.5)
631 self.assertEqual(compute('v17 // v18'), 2184)
632
633 # Test modulo operator
634 self.assertEqual(compute('v17 % v19'), 1)
635 self.assertEqual(compute('"%d" % v19'), '4')
636
637 # Test power operator
638 self.assertEqual(compute('v18 ** v19'), 16)
639 self.assertEqual(compute('v7 ** v18'), 10.5625)
640
641 # Test bitwise operators
642 self.assertEqual(compute('v16 | v17'), 0x3333)
643 self.assertEqual(compute('v4 | v8'), {1, 2, 3, 5})
644 self.assertEqual(compute('v16 & v17'), 0)
645 self.assertEqual(compute('v4 & v8'), {1, 3})
646 self.assertEqual(compute('v16 ^ v17'), 0x3333)
647 self.assertEqual(compute('v4 ^ v8'), {2, 5})
648 self.assertEqual(compute('v16 << v18'), 0x2222 << 2)
649 self.assertEqual(compute('v16 >> v18'), 0x2222 >> 2)
650
651 # Test relational operators
652 self.assertEqual(compute('v18 == v18'), True)
653 self.assertEqual(compute('v18 == v19'), False)
654 self.assertEqual(compute('v18 != v18'), False)
655 self.assertEqual(compute('v18 != v19'), True)
656 self.assertEqual(compute('v18 < v19'), True)
657 self.assertEqual(compute('v18 <= v19'), True)
658 self.assertEqual(compute('v19 <= v19'), True)
659 self.assertEqual(compute('v19 > v18'), True)
660 self.assertEqual(compute('v19 >= v18'), True)
661 self.assertEqual(compute('v19 >= v19'), True)
662
663 # Test membership operators
664 self.assertEqual(compute('v18 in v1'), True)
665 self.assertEqual(compute('v19 in v1'), False)
666 self.assertEqual(compute('v18 not in v1'), False)
667 self.assertEqual(compute('v19 not in v1'), True)
668 self.assertEqual(compute('v18 in v4'), True)
669
670 # Test unary operators
671 self.assertEqual(compute('-v18'), -2)
672 self.assertEqual(compute('+v19'), 4)
673 self.assertEqual(compute('not v20'), True)
674 self.assertEqual(compute('~v18'), -3)
675
676 # Test attribute access
677 self.assertIs(compute_import('ast.parse'), ast.parse)
678
679 # Test array indexing and slicing
680 self.assertEqual(compute('v1[v18]'), 3)
681 self.assertEqual(compute('v1[::-1]'), [3, 2, 1])
682 self.assertEqual(compute('"abc"[::-1]'), "cba")
683
684 # Test logical operators
685 self.assertEqual(compute('v20 or v21'), True)
686 self.assertEqual(compute('v20 and v21'), False)
687 self.assertEqual(compute('v21 or v20'), True)
688 self.assertEqual(compute('v21 and v20'), False)
689
690 # Error cases
691 self.assertRaises(TypeError, compute, 'v4 + v8') # can't add sets
692 self.assertRaises(ValueError, compute, 'v4 + ') # bad syntax
693 # function calls are not supported ...
694 self.assertRaises(ValueError, compute_import, 'cgi.print_directory()')
695 self.assertRaises(ValueError, compute, 'v99') # unknown name
529 696
530 class ASTValidatorTests(unittest.TestCase): 697 class ASTValidatorTests(unittest.TestCase):
531 698
532 def mod(self, mod, msg=None, mode="exec", *, exc=ValueError): 699 def mod(self, mod, msg=None, mode="exec", *, exc=ValueError):
533 mod.lineno = mod.col_offset = 0 700 mod.lineno = mod.col_offset = 0
534 ast.fix_missing_locations(mod) 701 ast.fix_missing_locations(mod)
535 with self.assertRaises(exc) as cm: 702 with self.assertRaises(exc) as cm:
536 compile(mod, "<test>", mode) 703 compile(mod, "<test>", mode)
537 if msg is not None: 704 if msg is not None:
538 self.assertIn(msg, str(cm.exception)) 705 self.assertIn(msg, str(cm.exception))
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
1013 ('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Lo ad',))), 1180 ('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Lo ad',))),
1014 ('Expression', ('Name', (1, 0), 'v', ('Load',))), 1181 ('Expression', ('Name', (1, 0), 'v', ('Load',))),
1015 ('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))), 1182 ('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
1016 ('Expression', ('List', (1, 0), [], ('Load',))), 1183 ('Expression', ('List', (1, 0), [], ('Load',))),
1017 ('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num' , (1, 4), 3)], ('Load',))), 1184 ('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num' , (1, 4), 3)], ('Load',))),
1018 ('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num' , (1, 5), 3)], ('Load',))), 1185 ('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num' , (1, 5), 3)], ('Load',))),
1019 ('Expression', ('Tuple', (1, 0), [], ('Load',))), 1186 ('Expression', ('Tuple', (1, 0), [], ('Load',))),
1020 ('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('At tribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load ',)), 'd', ('Load',)), [('Subscript', (1, 8), ('Attribute', (1, 8), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1 , 14), 2), None), ('Load',))], [], None, None)), 1187 ('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('At tribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load ',)), 'd', ('Load',)), [('Subscript', (1, 8), ('Attribute', (1, 8), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1 , 14), 2), None), ('Load',))], [], None, None)),
1021 ] 1188 ]
1022 main() 1189 main()
OLDNEW
« 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+