|
1 | 1 | import dis
|
2 | 2 | from itertools import combinations, product
|
| 3 | +import sys |
3 | 4 | import textwrap
|
4 | 5 | import unittest
|
5 | 6 |
|
@@ -682,5 +683,184 @@ def test_bpo_45773_pop_jump_if_false(self):
|
682 | 683 | compile("while True or not spam: pass", "<test>", "exec")
|
683 | 684 |
|
684 | 685 |
|
| 686 | +class TestMarkingVariablesAsUnKnown(BytecodeTestCase): |
| 687 | + |
| 688 | + def setUp(self): |
| 689 | + self.addCleanup(sys.settrace, sys.gettrace()) |
| 690 | + sys.settrace(None) |
| 691 | + |
| 692 | + def test_load_fast_known_simple(self): |
| 693 | + def f(): |
| 694 | + x = 1 |
| 695 | + y = x + x |
| 696 | + self.assertInBytecode(f, 'LOAD_FAST') |
| 697 | + |
| 698 | + def test_load_fast_unknown_simple(self): |
| 699 | + def f(): |
| 700 | + if condition(): |
| 701 | + x = 1 |
| 702 | + print(x) |
| 703 | + self.assertInBytecode(f, 'LOAD_FAST_CHECK') |
| 704 | + self.assertNotInBytecode(f, 'LOAD_FAST') |
| 705 | + |
| 706 | + def test_load_fast_unknown_because_del(self): |
| 707 | + def f(): |
| 708 | + x = 1 |
| 709 | + del x |
| 710 | + print(x) |
| 711 | + self.assertInBytecode(f, 'LOAD_FAST_CHECK') |
| 712 | + self.assertNotInBytecode(f, 'LOAD_FAST') |
| 713 | + |
| 714 | + def test_load_fast_known_because_parameter(self): |
| 715 | + def f1(x): |
| 716 | + print(x) |
| 717 | + self.assertInBytecode(f1, 'LOAD_FAST') |
| 718 | + self.assertNotInBytecode(f1, 'LOAD_FAST_CHECK') |
| 719 | + |
| 720 | + def f2(*, x): |
| 721 | + print(x) |
| 722 | + self.assertInBytecode(f2, 'LOAD_FAST') |
| 723 | + self.assertNotInBytecode(f2, 'LOAD_FAST_CHECK') |
| 724 | + |
| 725 | + def f3(*args): |
| 726 | + print(args) |
| 727 | + self.assertInBytecode(f3, 'LOAD_FAST') |
| 728 | + self.assertNotInBytecode(f3, 'LOAD_FAST_CHECK') |
| 729 | + |
| 730 | + def f4(**kwargs): |
| 731 | + print(kwargs) |
| 732 | + self.assertInBytecode(f4, 'LOAD_FAST') |
| 733 | + self.assertNotInBytecode(f4, 'LOAD_FAST_CHECK') |
| 734 | + |
| 735 | + def f5(x=0): |
| 736 | + print(x) |
| 737 | + self.assertInBytecode(f5, 'LOAD_FAST') |
| 738 | + self.assertNotInBytecode(f5, 'LOAD_FAST_CHECK') |
| 739 | + |
| 740 | + def test_load_fast_known_because_already_loaded(self): |
| 741 | + def f(): |
| 742 | + if condition(): |
| 743 | + x = 1 |
| 744 | + print(x) |
| 745 | + print(x) |
| 746 | + self.assertInBytecode(f, 'LOAD_FAST_CHECK') |
| 747 | + self.assertInBytecode(f, 'LOAD_FAST') |
| 748 | + |
| 749 | + def test_load_fast_known_multiple_branches(self): |
| 750 | + def f(): |
| 751 | + if condition(): |
| 752 | + x = 1 |
| 753 | + else: |
| 754 | + x = 2 |
| 755 | + print(x) |
| 756 | + self.assertInBytecode(f, 'LOAD_FAST') |
| 757 | + self.assertNotInBytecode(f, 'LOAD_FAST_CHECK') |
| 758 | + |
| 759 | + def test_load_fast_unknown_after_error(self): |
| 760 | + def f(): |
| 761 | + try: |
| 762 | + res = 1 / 0 |
| 763 | + except ZeroDivisionError: |
| 764 | + pass |
| 765 | + return res |
| 766 | + # LOAD_FAST (known) still occurs in the no-exception branch. |
| 767 | + # Assert that it doesn't occur in the LOAD_FAST_CHECK branch. |
| 768 | + self.assertInBytecode(f, 'LOAD_FAST_CHECK') |
| 769 | + |
| 770 | + def test_load_fast_unknown_after_error_2(self): |
| 771 | + def f(): |
| 772 | + try: |
| 773 | + 1 / 0 |
| 774 | + except: |
| 775 | + print(a, b, c, d, e, f, g) |
| 776 | + a = b = c = d = e = f = g = 1 |
| 777 | + self.assertInBytecode(f, 'LOAD_FAST_CHECK') |
| 778 | + self.assertNotInBytecode(f, 'LOAD_FAST') |
| 779 | + |
| 780 | + def test_setting_lineno_adds_check(self): |
| 781 | + code = textwrap.dedent("""\ |
| 782 | + def f(): |
| 783 | + x = 2 |
| 784 | + L = 3 |
| 785 | + L = 4 |
| 786 | + for i in range(55): |
| 787 | + x + 6 |
| 788 | + del x |
| 789 | + L = 8 |
| 790 | + L = 9 |
| 791 | + L = 10 |
| 792 | + """) |
| 793 | + ns = {} |
| 794 | + exec(code, ns) |
| 795 | + f = ns['f'] |
| 796 | + self.assertInBytecode(f, "LOAD_FAST") |
| 797 | + def trace(frame, event, arg): |
| 798 | + if event == 'line' and frame.f_lineno == 9: |
| 799 | + frame.f_lineno = 2 |
| 800 | + sys.settrace(None) |
| 801 | + return None |
| 802 | + return trace |
| 803 | + sys.settrace(trace) |
| 804 | + f() |
| 805 | + self.assertNotInBytecode(f, "LOAD_FAST") |
| 806 | + |
| 807 | + def make_function_with_no_checks(self): |
| 808 | + code = textwrap.dedent("""\ |
| 809 | + def f(): |
| 810 | + x = 2 |
| 811 | + L = 3 |
| 812 | + L = 4 |
| 813 | + L = 5 |
| 814 | + if not L: |
| 815 | + x + 7 |
| 816 | + y = 2 |
| 817 | + """) |
| 818 | + ns = {} |
| 819 | + exec(code, ns) |
| 820 | + f = ns['f'] |
| 821 | + self.assertInBytecode(f, "LOAD_FAST") |
| 822 | + self.assertNotInBytecode(f, "LOAD_FAST_CHECK") |
| 823 | + return f |
| 824 | + |
| 825 | + def test_deleting_local_adds_check(self): |
| 826 | + f = self.make_function_with_no_checks() |
| 827 | + def trace(frame, event, arg): |
| 828 | + if event == 'line' and frame.f_lineno == 4: |
| 829 | + del frame.f_locals["x"] |
| 830 | + sys.settrace(None) |
| 831 | + return None |
| 832 | + return trace |
| 833 | + sys.settrace(trace) |
| 834 | + f() |
| 835 | + self.assertNotInBytecode(f, "LOAD_FAST") |
| 836 | + self.assertInBytecode(f, "LOAD_FAST_CHECK") |
| 837 | + |
| 838 | + def test_modifying_local_does_not_add_check(self): |
| 839 | + f = self.make_function_with_no_checks() |
| 840 | + def trace(frame, event, arg): |
| 841 | + if event == 'line' and frame.f_lineno == 4: |
| 842 | + frame.f_locals["x"] = 42 |
| 843 | + sys.settrace(None) |
| 844 | + return None |
| 845 | + return trace |
| 846 | + sys.settrace(trace) |
| 847 | + f() |
| 848 | + self.assertInBytecode(f, "LOAD_FAST") |
| 849 | + self.assertNotInBytecode(f, "LOAD_FAST_CHECK") |
| 850 | + |
| 851 | + def test_initializing_local_does_not_add_check(self): |
| 852 | + f = self.make_function_with_no_checks() |
| 853 | + def trace(frame, event, arg): |
| 854 | + if event == 'line' and frame.f_lineno == 4: |
| 855 | + frame.f_locals["y"] = 42 |
| 856 | + sys.settrace(None) |
| 857 | + return None |
| 858 | + return trace |
| 859 | + sys.settrace(trace) |
| 860 | + f() |
| 861 | + self.assertInBytecode(f, "LOAD_FAST") |
| 862 | + self.assertNotInBytecode(f, "LOAD_FAST_CHECK") |
| 863 | + |
| 864 | + |
685 | 865 | if __name__ == "__main__":
|
686 | 866 | unittest.main()
|
0 commit comments