From 11242616d7ff96e364d98d72d907ddb2b9e9d973 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Sun, 24 Mar 2019 12:45:08 -0500 Subject: [PATCH 1/2] Use existing __main__ namespace to execute scripts (closes GH-331) --- pudb/debugger.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/pudb/debugger.py b/pudb/debugger.py index 2a16822..4e7931b 100644 --- a/pudb/debugger.py +++ b/pudb/debugger.py @@ -433,11 +433,19 @@ class Debugger(bdb.Bdb): self.interaction(frame, exc_tuple) def _runscript(self, filename): - # Start with fresh empty copy of globals and locals and tell the script - # that it's being run as __main__ to avoid scripts being able to access - # the debugger's namespace. - globals_ = {"__name__": "__main__", "__file__": filename} - locals_ = globals_ + # Provide separation from current __main__, which is likely + # pudb.__main__ run. Preserving its namespace is not important, and + # having the script share it ensures that, e.g., pickle can find + # types defined there: + # https://github.com/inducer/pudb/issues/331 + + import __main__ + __main__.__dict__.clear() + __main__.__dict__.update({ + "__name__" : "__main__", + "__file__" : filename, + "__builtins__": __builtins__, + }) # When bdb sets tracing, a number of call and line events happens # BEFORE debugger even reaches user's code (and the exact sequence of @@ -456,7 +464,8 @@ class Debugger(bdb.Bdb): from pudb import set_interrupt_handler set_interrupt_handler() - self.run(statement, globals=globals_, locals=locals_) + # Implicitly runs in the namespace of __main__. + self.run(statement) # }}} -- GitLab From 64f58294d5d0d86a41a215394c3c6781dba1383f Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Sun, 24 Mar 2019 12:47:10 -0500 Subject: [PATCH 2/2] Placate flake8 --- pudb/debugger.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pudb/debugger.py b/pudb/debugger.py index 4e7931b..59ac490 100644 --- a/pudb/debugger.py +++ b/pudb/debugger.py @@ -442,8 +442,8 @@ class Debugger(bdb.Bdb): import __main__ __main__.__dict__.clear() __main__.__dict__.update({ - "__name__" : "__main__", - "__file__" : filename, + "__name__": "__main__", + "__file__": filename, "__builtins__": __builtins__, }) -- GitLab