473,785 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.isat ty():
# we are in interactive mode or we don't have a tty-like
# device, so we call the default hook
sys.__excepthoo k__(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 1637
Jon Perez <jb********@yah oo.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\sit ecustomize.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=trace back.print_exce ption,
## debug=pywin.deb ugger.pm):
debug=pdb.pm):
if not __debug__ \
or not sys \
or hasattr(sys, 'ps1') \
or not sys.stderr.isat ty() \
or not sys.stdin.isatt y() \
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\sit ecustomize.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.p y
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\sit ecustomize.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.p y
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.p y 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.p y 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\sit ecustomize.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.p y
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
1164
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 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...
6
7591
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. Additional information: Unverifiable assembly 'FrameGrabber' failed policy check. The FrameGrabber assembly is a C++ managed DLL.
9
2320
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. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER // EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
9
2210
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 AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledException Application.Run(Main)
2
1324
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 to disable a dialog of my liking. Now if I run the application outside of the IDE I recieve the standard "Unhandled exception" dialog provided by .NET giving me the ability to continue or quit. I wasn't actually aware that exception handling...
2
2892
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) I receive the following error: "The type initializer for "System.Xml.Schema.Validator" threw an exception." I wrote a small console app that contains the problem -- I've just attached the default class which gets run. Output outside the...
1
2860
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 following error: "The type initializer for "System.Xml.Schema.Validator" threw an exception.". When running from the debugger (debug or release) no exception is thrown. I wrote a small console app that replicates the problem -- I've just attached...
0
1120
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 debug. When I click the debug button I receive a messagebox that states "An exception 'Unhandled Win32 Exception' has occurred in <my product>.exe However, a debugger that cannot handle the exception type is already attached to the process." ...
6
3395
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 queries data passed to it. I don't want to handle or supress exceptions at that point in the application, but rather ignore them so the debugger handles them in the correct place, further up the call stack. In java I could normally add a "throws...
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8986
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.