Skip to content

Commit

Permalink
Fix (unreleased) regression with missing start attributes
Browse files Browse the repository at this point in the history
This is not exactly the same as before (which was only done via the
module's `set_trace`), and therefore still gets updated in
`Pdb.set_trace()` (after initially in `__init__`).
  • Loading branch information
blueyed committed May 25, 2019
1 parent f2179d8 commit 8a3018c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 27 deletions.
61 changes: 34 additions & 27 deletions pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import re
import signal
from collections import OrderedDict
from fancycompleter import Completer, ConfigurableClass, Color

import fancycompleter
import six
from fancycompleter import Color, Completer, ConfigurableClass

__author__ = 'Antonio Cuni <anto.cuni@gmail.com>'
__url__ = 'http://github.com/antocuni/pdb'
Expand Down Expand Up @@ -170,6 +172,37 @@ def __repr__(self):
undefined = Undefined()


class PdbMeta(type):
def __call__(cls, *args, **kwargs):
"""Reuse an existing instance with ``pdb.set_trace()``."""
use_global_pdb = kwargs.get("use_global_pdb", True)
global_pdb = getattr(local, "GLOBAL_PDB", None)

calling_frame = sys._getframe().f_back
called_for_set_trace = (
calling_frame.f_code.co_name == "set_trace"
and calling_frame.f_back
and "set_trace" in calling_frame.f_back.f_code.co_names)

if use_global_pdb and global_pdb and called_for_set_trace:
if hasattr(global_pdb, "botframe"):
# Do not stop while tracing is active (in _set_stopinfo).
# But skip it with instances that have not called set_trace
# before.
global_pdb.set_continue()
global_pdb._skip_init = True
return global_pdb

obj = cls.__new__(cls)
if called_for_set_trace:
kwargs.setdefault("start_filename", calling_frame.f_code.co_filename)
kwargs.setdefault("start_lineno", calling_frame.f_lineno)
obj.__init__(*args, **kwargs)
local.GLOBAL_PDB = obj
return obj


@six.add_metaclass(PdbMeta)
class Pdb(pdb.Pdb, ConfigurableClass, object):

DefaultConfig = DefaultConfig
Expand Down Expand Up @@ -205,32 +238,6 @@ def __init__(self, *args, **kwds):
self.hidden_frames = []
self.stdout = self.ensure_file_can_write_unicode(self.stdout)

def __new__(cls, *args, **kwargs):
"""Reuse an existing instance with ``pdb.set_trace()``."""
use_global_pdb = kwargs.get("use_global_pdb", True)
global_pdb = getattr(local, "GLOBAL_PDB", None)
if use_global_pdb and global_pdb:
called_for_set_trace = False
frame = sys._getframe()
while frame.f_back:
frame = frame.f_back
if (frame.f_code.co_name == "set_trace"
and frame.f_back
and "set_trace" in frame.f_back.f_code.co_names):
called_for_set_trace = True
break
if called_for_set_trace:
if hasattr(global_pdb, "botframe"):
# Do not stop while tracing is active (in _set_stopinfo).
# But skip it with instances that have not called set_trace
# before.
global_pdb.set_continue()
global_pdb._skip_init = True
return global_pdb
ret = super(Pdb, cls).__new__(cls)
local.GLOBAL_PDB = ret
return ret

def ensure_file_can_write_unicode(self, f):
# Wrap with an encoder, but only if not already wrapped
if (not hasattr(f, 'stream')
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def run(self):
"fancycompleter>=0.8",
"wmctrl",
"pygments",
"six",
],
extras_require={
'funcsigs': ["funcsigs"],
Expand Down
24 changes: 24 additions & 0 deletions testing/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3595,3 +3595,27 @@ def fn():
5 frames hidden .*
# c
""")


def test_config_gets_start_filename():
def fn():
setup_lineno = set_trace.__code__.co_firstlineno + 8
set_trace_lineno = sys._getframe().f_lineno + 8

class MyConfig(ConfigTest):
def setup(self, pdb):
print("config_setup")
assert pdb.start_filename == __file__
assert pdb.start_lineno == setup_lineno

set_trace(Config=MyConfig)

assert pdb.local.GLOBAL_PDB.start_lineno == set_trace_lineno

check(fn, r"""
config_setup
[NUM] > .*fn()
-> assert pdb.local.GLOBAL_PDB.start_lineno == set_trace_lineno
5 frames hidden .*
# c
""")

0 comments on commit 8a3018c

Please sign in to comment.