473,408 Members | 2,399 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,408 software developers and data experts.

"assert" annoyance

So I have some assert statements in my code to verify the absence of
some "impossible" conditions. They were useful in debugging and of
course I left them in place for "real" runs of the program. Umpteen
hours into a run, an assertion failed, and of course since failure
was "impossible", I didn't catch the exception so the whole program
crashed. I don't know what I'd have done with the exception anyway,
since it would have had to be caught at an outer scope where the
data I cared about was no longer around, or else I'd have had to
predict in advance what I needed to examine and pass that as a
an arg to the assert statement.

What I really want is for any assertion failure, anywhere in the
program, to trap to the debugger WITHOUT blowing out of the scope
where the failure happened, so I can examine the local frame. That
just seems natural, but I don't see an obvious way to do it. Am I
missing something? I guess I could replace all the assertions with
function calls that launch pdb, but why bother having an assert
statement?
Jun 22 '07 #1
10 1296
On Jun 22, 1:31 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
What I really want is for any assertion failure, anywhere in the
program, to trap to the debugger WITHOUT blowing out of the scope
where the failure happened, so I can examine the local frame. That
just seems natural, but I don't see an obvious way to do it.
You could run the entire program through pdb:
----
#!/usr/bin/env python -m pdb

print "Hello!"
assert False
print "...world!"
----

Jun 22 '07 #2
On 6/21/07, Miles <se*********@gmail.comwrote:
On Jun 22, 1:31 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
What I really want is for any assertion failure, anywhere in the
program, to trap to the debugger WITHOUT blowing out of the scope
where the failure happened, so I can examine the local frame. That
just seems natural, but I don't see an obvious way to do it.

You could run the entire program through pdb:
----
#!/usr/bin/env python -m pdb

print "Hello!"
assert False
print "...world!"
----
You can only pass one argument to a command that you invoke with the
shebang sequence, so this won't work the way you wrote it.

--
Evan Klitzke <ev**@yelp.com>
Jun 22 '07 #3
On Jun 22, 2:45 am, "Evan Klitzke" <e...@yelp.comwrote:
On 6/21/07, Miles <semantic...@gmail.comwrote:
On Jun 22, 1:31 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
What I really want is for any assertion failure, anywhere in the
program, to trap to the debugger WITHOUT blowing out of the scope
where the failure happened, so I can examine the local frame. That
just seems natural, but I don't see an obvious way to do it.
You could run the entire program through pdb:
----
#!/usr/bin/env python -m pdb
print "Hello!"
assert False
print "...world!"
----

You can only pass one argument to a command that you invoke with the
shebang sequence, so this won't work the way you wrote it.

--
Evan Klitzke <e...@yelp.com>
It actually does work on my system (OS X); I didn't realize it wasn't
portable.

Jun 22 '07 #4
Paul Rubin schrieb:
So I have some assert statements in my code to verify the absence of
some "impossible" conditions. They were useful in debugging and of
course I left them in place for "real" runs of the program. Umpteen
hours into a run, an assertion failed, and of course since failure
was "impossible", I didn't catch the exception so the whole program
crashed. I don't know what I'd have done with the exception anyway,
since it would have had to be caught at an outer scope where the
data I cared about was no longer around, or else I'd have had to
predict in advance what I needed to examine and pass that as a
an arg to the assert statement.

What I really want is for any assertion failure, anywhere in the
program, to trap to the debugger WITHOUT blowing out of the scope
where the failure happened, so I can examine the local frame. That
just seems natural, but I don't see an obvious way to do it. Am I
missing something? I guess I could replace all the assertions with
function calls that launch pdb, but why bother having an assert
statement?
http://aspn.activestate.com/ASPN/Coo...n/Recipe/65287

Thomas

Jun 22 '07 #5
On 6/22/07, Miles <se*********@gmail.comwrote:
On Jun 22, 2:45 am, "Evan Klitzke" <e...@yelp.comwrote:
On 6/21/07, Miles <semantic...@gmail.comwrote:
On Jun 22, 1:31 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
What I really want is for any assertion failure, anywhere in the
program, to trap to the debugger WITHOUT blowing out of the scope
where the failure happened, so I can examine the local frame. That
just seems natural, but I don't see an obvious way to do it.
You could run the entire program through pdb:
----
#!/usr/bin/env python -m pdb
print "Hello!"
assert False
print "...world!"
----
You can only pass one argument to a command that you invoke with the
shebang sequence, so this won't work the way you wrote it.

--
Evan Klitzke <e...@yelp.com>

It actually does work on my system (OS X); I didn't realize it wasn't
portable.
This sort of surprised me (in a good way), since I just took the one
argument rule for granted. It's always bugged me that I couldn't do,
say, #!/usr/bin/env python -O. So it's nice to see that OS X splits
the arguments, even if this isn't completely portable! I did some
research on this and it looks like a few other Unices do it too. If
anyone is interested, there's a table documenting the behavior of
different systems at
http://www.in-ulm.de/~mascheck/various/shebang/#results

Maybe I should start using a Mac ;-)

--
Evan Klitzke <ev**@yelp.com>
Jun 22 '07 #6
Paul Rubin <http://ph****@NOSPAM.invalidwrites:
So I have some assert statements in my code to verify the absence of
some "impossible" conditions. They were useful in debugging and of
course I left them in place for "real" runs of the program. Umpteen
hours into a run, an assertion failed, and of course since failure
was "impossible", I didn't catch the exception so the whole program
crashed.
This is exactly the sort of check which is best done as unit
tests. The program has no 'assert' cruft in it, and the tests can be
as comprehensive as needed without having any impact on the actual
running program.

--
\ Legionnaire: "We have their leader captive!" C泡r: "Is he |
`\ bound?" Legionnaire: "Of his health I know not, sir." -- The |
_o__) Goon Show, _The Histories Of Pliny The Elder_ |
Ben Finney
Jun 22 '07 #7
Ben Finney <bi****************@benfinney.id.auwrites:
So I have some assert statements in my code to verify the absence of
some "impossible" conditions. They were useful in debugging and of
course I left them in place for "real" runs of the program. Umpteen
hours into a run, an assertion failed, and of course since failure
was "impossible", I didn't catch the exception so the whole program
crashed.

This is exactly the sort of check which is best done as unit
tests. The program has no 'assert' cruft in it, and the tests can be
as comprehensive as needed without having any impact on the actual
running program.
I don't understand what you're trying to say here. The code was
tested before I ran it with real data. The asserts triggered because
there was a real problem, so removing them would not have been the
right thing to do. On the other hand, the code had already been
tested without discovering the problem. The problem could not have
been found without running the program for hours, not something one
would normally do in a unit test, and also it involved an unexpected
error from a remote program (a 3GB process had run out of memory).
Unit tests are not a magic wand that discover every problem that a
program could possibly have.
Jun 22 '07 #8
Thomas Heller <th*****@ctypes.orgwrites:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/65287
Thanks! This looks very useful.
Jun 22 '07 #9
In article <7x***************@ruckus.brouhaha.com>,
Paul Rubin <http://ph****@NOSPAM.invalidwrote:
>
What I really want is for any assertion failure, anywhere in the
program, to trap to the debugger WITHOUT blowing out of the scope
where the failure happened, so I can examine the local frame. That
just seems natural, but I don't see an obvious way to do it. Am I
missing something? I guess I could replace all the assertions with
function calls that launch pdb, but why bother having an assert
statement?
I tend to run my programs with a -i option:

python -i foo.py

Then if an exception occurs, I can do:

import pdb
pdm.pm()

and have a debug session at the point where the exception was raised.
The "-i" shouldn't interfere with sys.argv since it is before the python
file.

Another option would be for your program to do something like:

try:
# put your code here
except:
import sys
import pdb
pdb.post_mortem(sys.exc_info()[2])
If an exception is raised, the debugger will be started using the
appropriate traceback. (You don't need to import sys and pdb in the
except block if they have already been imported.)
Dave
Jun 23 '07 #10
On Jun 22, 5:05 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
Unit tests are not a magic wand that discover every problem that a
program could possibly have.

+1 QOTW

Michele Simionato

Jun 23 '07 #11

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

Similar topics

8
by: Arvid Andersson | last post by:
Hello, I need to convert a string to a number, but the string can contain +,-,* and / as well as parenthesis. For example, if I have the string "30/(6+9)" I would like a function that returned the...
3
by: Thomas Guettler | last post by:
Hi, Python 2.3.3 (#1, Feb 5 2005, 16:22:10) on linux2 >>> assert 0, "foo" Traceback (most recent call last): File "<stdin>", line 1, in ? AssertionError: foo >>> assert(0, "foo") >>>
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
3
by: janzon | last post by:
Hi! Sorry for the bad subject line... Here's what I mean. Suppose we deal with C++ standard integers lists (the type is indifferent). We have a function f, declared as list<intf(int); Now...
6
by: Joseph Turian | last post by:
I've been using assert liberally throughout my code. Then, upon compiling with -NDEBUG, I found that my program had different output. Why? Because -NDEBUG disables assert, but I had (at least) one...
12
by: filia&sofia | last post by:
For example I would like to dynamically allocate memory for linked list (or complex struct etc.) that has not maximum number of elements. All that I can remember is that I have to have allocated...
14
by: Anna | last post by:
I try to put 8 int bit for example 10100010 into one character of type char(1 octet) with no hope . Could anyone propose a simple way to do it? Thank you very much.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
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.