473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

dropping into the debugger on an exception

(sorry for the duplicate post, just wanted to make the subject
line clearer)

How do you set up pdb such that you will automatically
get dropped into its prompt if an unanticipated exception
occurs in a script you are using?

ASPN Python cookbook gives you the following method
which you can add to your script and hook into sys.excepthook.
But is there a way to do it without adding stuff to your
script? (It's okay if this means having to invoke the script
from within pdb, but #1, I don't know how to get it stay inside
pdb in the case of an /unanticipated/ exception. And #2, I
don't know how to pass [the equivalent of] command-line
arguments to a script invoked from within pdb.)

def info(type, value, tb):
if hasattr(sys, 'ps1') or not sys.stderr.isatty():
# we are in interactive mode or we don't have a tty-like
# device, so we call the default hook
sys.__excepthook__(type, value, tb)
else:
import traceback, pdb
# we are NOT in interactive mode, print the exception...
traceback.print_exception(type, value, tb)
print
# ...then start the debugger in post-mortem mode.
pdb.pm()

sys.excepthook = info
Jul 18 '05 #1
8 1618
Jon Perez <jb********@yahoo.com> writes:
(sorry for the duplicate post, just wanted to make the subject
line clearer)

How do you set up pdb such that you will automatically
get dropped into its prompt if an unanticipated exception
occurs in a script you are using?

ASPN Python cookbook gives you the following method
which you can add to your script and hook into sys.excepthook.
But is there a way to do it without adding stuff to your
script? (It's okay if this means having to invoke the script
from within pdb, but #1, I don't know how to get it stay inside
pdb in the case of an /unanticipated/ exception. And #2, I
don't know how to pass [the equivalent of] command-line
arguments to a script invoked from within pdb.)

[...]

Is the description in the cookbook unclear? You do *not* have to add
anything to your script - save the code as a file
C:\Python23\sitecustomize.py and everything will work. And you don't
have to start the script from within pdb. The sitecustomize module is
automatically imported when Python starts - if it is found.

I'm appending the version I currently use, maybe it has some
improvements not mentioned in the online version.

Thomas

"""
# code snippet, to be included in 'sitecustomize.py'
import sys
import traceback, pdb
##import pywin.debugger

def info(type, value, tb,
excepthook=sys.__excepthook__,
print_exc=traceback.print_exception,
## debug=pywin.debugger.pm):
debug=pdb.pm):
if not __debug__ \
or not sys \
or hasattr(sys, 'ps1') \
or not sys.stderr.isatty() \
or not sys.stdin.isatty() \
or not hasattr(sys, "last_traceback"):
# call the default hook if one of the following conditions
# is satisfied:
# - Python is running with the -O flag (__debug__ == 0)
# - sys is None (python is shutting down, and the sys module
# already has been cleared)
# - we are in interactive mode (sys has a 'ps1' attribute)
# - sys.stderr is not a tty: we are not connected to a terminal
# - no last_traceback attribute in sys (SyntaxError)
excepthook(type, value, tb)
else:
# print the exception...
print_exc(type, value, tb)
# ...then start the debugger in post-mortem mode.
debug()

sys.excepthook = info
"""
Jul 18 '05 #2
Thomas Heller wrote:
Is the description in the cookbook unclear? You do *not* have to add
anything to your script - save the code as a file
C:\Python23\sitecustomize.py and everything will work. And you don't
have to start the script from within pdb. The sitecustomize module is
automatically imported when Python starts - if it is found.


Yes, I'm aware of this. I don't want to add this to sitecustomize.py
because I don't want this happening all the time.

I was hoping to for a different strategy. Would it be possible
to start up pdb(), call a script from there, give it the equivalent
of command line arguments, do a cont (or whatever is required) and
then have it /stay/ within pdb if an unanticipated exception occurs
(as opposed to having to set up a breakpoint)?
Jul 18 '05 #3
Jon Perez wrote:
Thomas Heller wrote:
Is the description in the cookbook unclear? You do *not* have to add
anything to your script - save the code as a file
C:\Python23\sitecustomize.py and everything will work. And you don't
have to start the script from within pdb. The sitecustomize module is
automatically imported when Python starts - if it is found.


Yes, I'm aware of this. I don't want to add this to sitecustomize.py
because I don't want this happening all the time.


Just to be clear: you don't want this to happen all the time,
you want it to happen only with a particular script, yet you
don't want to modify that script at all?

Would it be sufficient to have a local sitecustomize.py file
in the directory where the script is, or do you have other
scripts in that folder which you don't want to get the same
treatment?

What about a wrapper which you invoke instead of the script,
which sets this up and then runs the real script using
"import" and an appropriate direct call?

-Peter
Jul 18 '05 #4
Peter Hansen wrote:
Just to be clear: you don't want this to happen all the time,
you want it to happen only with a particular script, yet you
don't want to modify that script at all?
Correct. I don't even want it to happen all of the time
with that particular script (because I may eventually
deploy it for others to use who may be confused by
being dropped into pdb). I can easily disable it by not
assigning info() to sys.excepthook. But like I said, preferably,
I don't even want to add anything to the script source code.
Would it be sufficient to have a local sitecustomize.py file
in the directory where the script is, or do you have other
scripts in that folder which you don't want to get the same
treatment? What about a wrapper which you invoke instead of the script,
which sets this up and then runs the real script using
"import" and an appropriate direct call?


These are both acceptable to a certain extent, and frankly,
if these were the alternatives, I will just stick to putting
info() in any script I intend to debug this way.

I was just wondering if there wasn't a cleaner/more intuitive
way which would be to just invoke the script from within pdb
and have pdb do the right thing (i.e. stay within itself) upon
an exception.

Can't one prevent pdb from exiting if an (unanticipated)
exception occurs in a script (or function) it invokes?

And secondly, how do you pass command-line arguments to a
script invoked from within pdb? I'm not even sure how to
invoke a script proper like you would from a command line, all
the examples I see invoke specific functions within a script.

Not being able to accomplish simple things like this in a
straightforward manner is what's keeping me from wholeheartedly
embracing pdb.
Jul 18 '05 #5
Peter Hansen <pe***@engcorp.com> writes:
Jon Perez wrote:
Thomas Heller wrote:
Is the description in the cookbook unclear? You do *not* have to add
anything to your script - save the code as a file
C:\Python23\sitecustomize.py and everything will work. And you don't
have to start the script from within pdb. The sitecustomize module is
automatically imported when Python starts - if it is found.

Yes, I'm aware of this. I don't want to add this to sitecustomize.py
because I don't want this happening all the time.


Just to be clear: you don't want this to happen all the time,
you want it to happen only with a particular script, yet you
don't want to modify that script at all?


I also don't understand *why* he wants to have it that way.

There's also one (hidden?) feature in the code I posted: The excepthook
only calls the debugger when __debug__ is true, so you can also avoid
the debugger when the python is started with the -O flag.

Thomas
Jul 18 '05 #6
Thanks everyone, for taking the time out to answer...

Hopefully pdb operation can one day be as simple and
straightforward as with other debuggers. Run pdb,
invoke a script from it, and have it remain within pdb
when an exception happens.
Jul 18 '05 #7
Jon Perez wrote:
Thanks everyone, for taking the time out to answer...

Hopefully pdb operation can one day be as simple and
straightforward as with other debuggers. Run pdb,
invoke a script from it, and have it remain within pdb
when an exception happens.


Jon, I think rpdb does that,
http://rpdb.digitalpeers.com/

Jul 18 '05 #8
ziaran wrote:
Jon, I think rpdb does that,
http://rpdb.digitalpeers.com/


Excellent!

Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jon Perez | last post by:
How do you set up pdb such that you will automatically get dropped into its prompt if an unanticipated exception occurs in a script you are using? ASPN Python cookbook gives you the following...
6
by: Ken Varn | last post by:
I am trying to remote debug a C# application but the debugger is reporting the following exception: An unhandled exception of type 'System.IO.FileLoadException' occurred in Unknown Module....
9
by: RalphTheExpert | last post by:
I'm getting different behavior if my code is running under the debugger or not. I have modified Winmain to look like this: // Copyright (C) 2002 Microsoft Corporation // All rights reserved....
9
by: Claudio Di Flumeri | last post by:
Hello all, I've added a global exception handler to my application in this way: Sub Main() AddHandler Application.ThreadException, AddressOf ThreadException AddHandler...
2
by: Nak | last post by:
Hi there, I'm just curious as to something. I have just added an exception handler at the entry point to my application, within the IDE any unhandled exceptions fallback to this and enable me...
2
by: Chris Stiefeling | last post by:
Hi, I am experiencing a strange problem. I am reading and writing xml files via XmlDocument and XmlTextWriter. In the debugger everything works fine but outside the debugger (debug or release)...
1
by: Chris Stiefeling | last post by:
Hi, I am experiencing a strange problem. I am reading and writing xml files via XmlDocument and XmlTextWriter. In the debugger everything works fine but outside the debugger I receive the...
0
by: Jeff Beem | last post by:
I have a problem where visual studio doesn't break into the vs.net debugger when an exception is encountered. Instead I get the standard windows exception message where it allows one to cancel or...
6
by: Phillip Taylor | last post by:
I don't like the way every time I write a bad sql query the debugger takes me to the database access code. It's extremely stable and reliable and the exceptions being thrown are because of SQL...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.