Skip to content

Commit 33c84e6

Browse files
authored
Merge branch 'main' into warnings-as-error-2
2 parents 7123093 + 61b35f7 commit 33c84e6

File tree

3 files changed

+55
-48
lines changed

3 files changed

+55
-48
lines changed

Doc/faq/programming.rst

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,28 +1906,30 @@ In the standard library code, you will see several common patterns for
19061906
correctly using identity tests:
19071907

19081908
1) As recommended by :pep:`8`, an identity test is the preferred way to check
1909-
for ``None``. This reads like plain English in code and avoids confusion with
1910-
other objects that may have boolean values that evaluate to false.
1909+
for ``None``. This reads like plain English in code and avoids confusion
1910+
with other objects that may have boolean values that evaluate to false.
19111911

19121912
2) Detecting optional arguments can be tricky when ``None`` is a valid input
1913-
value. In those situations, you can create a singleton sentinel object
1914-
guaranteed to be distinct from other objects. For example, here is how
1915-
to implement a method that behaves like :meth:`dict.pop`::
1913+
value. In those situations, you can create a singleton sentinel object
1914+
guaranteed to be distinct from other objects. For example, here is how
1915+
to implement a method that behaves like :meth:`dict.pop`:
19161916

1917-
_sentinel = object()
1917+
.. code-block:: python
19181918
1919-
def pop(self, key, default=_sentinel):
1920-
if key in self:
1921-
value = self[key]
1922-
del self[key]
1923-
return value
1924-
if default is _sentinel:
1925-
raise KeyError(key)
1926-
return default
1919+
_sentinel = object()
1920+
1921+
def pop(self, key, default=_sentinel):
1922+
if key in self:
1923+
value = self[key]
1924+
del self[key]
1925+
return value
1926+
if default is _sentinel:
1927+
raise KeyError(key)
1928+
return default
19271929
19281930
3) Container implementations sometimes need to augment equality tests with
1929-
identity tests. This prevents the code from being confused by objects such as
1930-
``float('NaN')`` that are not equal to themselves.
1931+
identity tests. This prevents the code from being confused by objects
1932+
such as ``float('NaN')`` that are not equal to themselves.
19311933

19321934
For example, here is the implementation of
19331935
:meth:`!collections.abc.Sequence.__contains__`::

Lib/bdb.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import os
66
import weakref
7+
from contextlib import contextmanager
78
from inspect import CO_GENERATOR, CO_COROUTINE, CO_ASYNC_GENERATOR
89

910
__all__ = ["BdbQuit", "Bdb", "Breakpoint"]
@@ -65,6 +66,12 @@ def reset(self):
6566
self.botframe = None
6667
self._set_stopinfo(None, None)
6768

69+
@contextmanager
70+
def set_enterframe(self, frame):
71+
self.enterframe = frame
72+
yield
73+
self.enterframe = None
74+
6875
def trace_dispatch(self, frame, event, arg):
6976
"""Dispatch a trace function for debugged frames based on the event.
7077
@@ -90,28 +97,27 @@ def trace_dispatch(self, frame, event, arg):
9097
The arg parameter depends on the previous event.
9198
"""
9299

93-
self.enterframe = frame
94-
95-
if self.quitting:
96-
return # None
97-
if event == 'line':
98-
return self.dispatch_line(frame)
99-
if event == 'call':
100-
return self.dispatch_call(frame, arg)
101-
if event == 'return':
102-
return self.dispatch_return(frame, arg)
103-
if event == 'exception':
104-
return self.dispatch_exception(frame, arg)
105-
if event == 'c_call':
106-
return self.trace_dispatch
107-
if event == 'c_exception':
108-
return self.trace_dispatch
109-
if event == 'c_return':
100+
with self.set_enterframe(frame):
101+
if self.quitting:
102+
return # None
103+
if event == 'line':
104+
return self.dispatch_line(frame)
105+
if event == 'call':
106+
return self.dispatch_call(frame, arg)
107+
if event == 'return':
108+
return self.dispatch_return(frame, arg)
109+
if event == 'exception':
110+
return self.dispatch_exception(frame, arg)
111+
if event == 'c_call':
112+
return self.trace_dispatch
113+
if event == 'c_exception':
114+
return self.trace_dispatch
115+
if event == 'c_return':
116+
return self.trace_dispatch
117+
if event == 'opcode':
118+
return self.dispatch_opcode(frame, arg)
119+
print('bdb.Bdb.dispatch: unknown debugging event:', repr(event))
110120
return self.trace_dispatch
111-
if event == 'opcode':
112-
return self.dispatch_opcode(frame, arg)
113-
print('bdb.Bdb.dispatch: unknown debugging event:', repr(event))
114-
return self.trace_dispatch
115121

116122
def dispatch_line(self, frame):
117123
"""Invoke user function and return trace function for line event.
@@ -395,16 +401,15 @@ def set_trace(self, frame=None):
395401
if frame is None:
396402
frame = sys._getframe().f_back
397403
self.reset()
398-
self.enterframe = frame
399-
while frame:
400-
frame.f_trace = self.trace_dispatch
401-
self.botframe = frame
402-
self.frame_trace_lines_opcodes[frame] = (frame.f_trace_lines, frame.f_trace_opcodes)
403-
# We need f_trace_lines == True for the debugger to work
404-
frame.f_trace_lines = True
405-
frame = frame.f_back
406-
self.set_stepinstr()
407-
self.enterframe = None
404+
with self.set_enterframe(frame):
405+
while frame:
406+
frame.f_trace = self.trace_dispatch
407+
self.botframe = frame
408+
self.frame_trace_lines_opcodes[frame] = (frame.f_trace_lines, frame.f_trace_opcodes)
409+
# We need f_trace_lines == True for the debugger to work
410+
frame.f_trace_lines = True
411+
frame = frame.f_back
412+
self.set_stepinstr()
408413
sys.settrace(self.trace_dispatch)
409414

410415
def set_continue(self):
@@ -424,7 +429,6 @@ def set_continue(self):
424429
for frame, (trace_lines, trace_opcodes) in self.frame_trace_lines_opcodes.items():
425430
frame.f_trace_lines, frame.f_trace_opcodes = trace_lines, trace_opcodes
426431
self.frame_trace_lines_opcodes = {}
427-
self.enterframe = None
428432

429433
def set_quit(self):
430434
"""Set quitting attribute to True.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Release the enter frame reference within :mod:`bdb` callback

0 commit comments

Comments
 (0)