Patch #1429539: pdb now correctly initializes the __main__ module for

the debugged script, which means that imports from __main__ work
correctly now.
This commit is contained in:
Georg Brandl 2007-03-13 21:58:44 +00:00
parent bad6f1904b
commit b6ae6aa8ac
2 changed files with 20 additions and 9 deletions

View File

@ -1147,11 +1147,17 @@ see no sign that the breakpoint was reached.
return None return None
def _runscript(self, filename): def _runscript(self, filename):
# Start with fresh empty copy of globals and locals and tell the script # The script has to run in __main__ namespace (or imports from
# that it's being run as __main__ to avoid scripts being able to access # __main__ will break).
# the pdb.py namespace. #
globals_ = {"__name__" : "__main__"} # So we clear up the __main__ and set several special variables
locals_ = globals_ # (this gets rid of pdb's globals and cleans old variables on restarts).
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 # When bdb sets tracing, a number of call and line events happens
# BEFORE debugger even reaches user's code (and the exact sequence of # BEFORE debugger even reaches user's code (and the exact sequence of
@ -1162,7 +1168,7 @@ see no sign that the breakpoint was reached.
self.mainpyfile = self.canonic(filename) self.mainpyfile = self.canonic(filename)
self._user_requested_quit = 0 self._user_requested_quit = 0
statement = 'execfile( "%s")' % filename statement = 'execfile( "%s")' % filename
self.run(statement, globals=globals_, locals=locals_) self.run(statement)
# Simplified interface # Simplified interface
@ -1259,5 +1265,6 @@ def main():
# When invoked as main program, invoke the debugger on a script # When invoked as main program, invoke the debugger on a script
if __name__=='__main__': if __name__ == '__main__':
main() import pdb
pdb.main()

View File

@ -170,6 +170,10 @@ Core and builtins
Library Library
------- -------
- Patch #1429539: pdb now correctly initializes the __main__ module for
the debugged script, which means that imports from __main__ work
correctly now.
- The nonobvious commands.getstatus() function is now deprecated. - The nonobvious commands.getstatus() function is now deprecated.
- Patch #1393667: pdb now has a "run" command which restarts the debugged - Patch #1393667: pdb now has a "run" command which restarts the debugged