File: test_breakpoint.py

package info (click to toggle)
pypy3 7.3.11%2Bdfsg-2%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 201,024 kB
  • sloc: python: 1,950,308; ansic: 517,580; sh: 21,417; asm: 14,419; cpp: 4,263; makefile: 4,228; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 11; awk: 4
file content (164 lines) | stat: -rw-r--r-- 5,504 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import pytest

class AppTestBreakpoint:
    def setup_class(cls):
        cls.w_import_mock_pdb = cls.space.appexec([], """():
            import sys
            from contextlib import contextmanager
            from types import ModuleType

            @contextmanager
            def w_import_mock_pdb():
                try:
                    mock_pdb = ModuleType('pdb')
                    mock_pdb.set_trace = None
                    sys.modules['pdb'] = mock_pdb
                    yield mock_pdb
                finally:
                    del sys.modules['pdb']

            return w_import_mock_pdb
        """)

        cls.w_mock_function = cls.space.appexec([], """():
            from contextlib import contextmanager

            class MockedCallable:
                def __init__(self):
                    self.called = 0

                def __call__(self, *args, **kwargs):
                    self.called += 1
                    self.last_call = (args, kwargs)

            @contextmanager
            def mock_function(scope, attr, delete=False):
                old = getattr(scope, attr)
                try:
                    if not delete:
                        new = MockedCallable()
                        setattr(scope, attr, new)
                        yield new
                    else:
                        delattr(scope, attr)
                        yield
                finally:
                    setattr(scope, attr, old)

            return mock_function
        """)


    def test_default(self):
        import sys
        assert sys.breakpointhook is sys.__breakpointhook__

        import os
        assert 'PYTHONBREAKPOINT' not in os.environ

        with self.import_mock_pdb() as pdb:
            with self.mock_function(pdb, 'set_trace') as mocked:
                breakpoint()
                assert mocked.called == 1


    def test_args_kwargs(self):
        import sys
        with self.import_mock_pdb() as pdb:
            with self.mock_function(pdb, 'set_trace') as mocked:
                breakpoint(1, 2, 3, x=4, y=5)
                assert mocked.called == 1
                assert mocked.last_call == ((1, 2, 3), {'x': 4, 'y': 5})


    def test_breakpointhook(self):
        import sys
        with self.import_mock_pdb() as pdb:
            with self.mock_function(sys, 'breakpointhook') as mocked, \
                self.mock_function(pdb, 'set_trace') as mocked_pdb:
                breakpoint()
                assert mocked.called == 1
                assert mocked_pdb.called == 0

            with self.mock_function(pdb, 'set_trace') as mocked_pdb:
                breakpoint()
                assert mocked_pdb.called == 1


    def test_breakpointhook_lost(self):
        import sys
        with self.import_mock_pdb() as pdb:
            with self.mock_function(sys, 'breakpointhook', delete=True):
                with raises(RuntimeError) as excinfo:
                    breakpoint()
                assert str(excinfo.value) == "lost sys.breakpointhook"


    @pytest.mark.dont_track_allocations('putenv intentionally keeps strings alive')
    def test_env_default(self):
        import os
        try:
            os.environ['PYTHONBREAKPOINT'] = ""

            import sys
            with self.import_mock_pdb() as pdb:
                with self.mock_function(pdb, 'set_trace') as mocked:
                    breakpoint()
                    assert mocked.called == 1
        finally:
            del os.environ['PYTHONBREAKPOINT']


    @pytest.mark.dont_track_allocations('putenv intentionally keeps strings alive')
    def test_env_disable(self):
        import os
        try:
            os.environ['PYTHONBREAKPOINT'] = "0"

            import sys
            with self.import_mock_pdb() as pdb:
                with self.mock_function(pdb, 'set_trace') as mocked:
                    breakpoint()
                    assert mocked.called == 0

                with self.mock_function(sys, 'breakpointhook') as mocked:
                    breakpoint()
                    assert mocked.called == 1
        finally:
            del os.environ['PYTHONBREAKPOINT']


    @pytest.mark.dont_track_allocations('putenv intentionally keeps strings alive')
    def test_env_other(self):
        import os
        try:
            os.environ['PYTHONBREAKPOINT'] = 'sys.exit'

            import sys
            with self.import_mock_pdb() as pdb:
                with self.mock_function(sys, 'exit') as mocked, \
                     self.mock_function(pdb, 'set_trace') as mocked_pdb:
                    breakpoint()
                    assert mocked.called == 1
                    assert mocked_pdb.called == 0
        finally:
            del os.environ['PYTHONBREAKPOINT']


    @pytest.mark.dont_track_allocations('putenv intentionally keeps strings alive')
    def test_env_nonexistent(self):
        import os
        import warnings
        try:
            os.environ['PYTHONBREAKPOINT'] = 'blah.bleh'

            import sys
            with self.import_mock_pdb() as pdb:
                with self.mock_function(pdb, 'set_trace') as mocked_pdb, \
                     warnings.catch_warnings(record=True) as w:
                    breakpoint()
                    assert mocked_pdb.called == 0
                    assert len(w) == 1
                    assert str(w[-1].message) == 'Ignoring unimportable $PYTHONBREAKPOINT: "blah.bleh"'
        finally:
            del os.environ['PYTHONBREAKPOINT']