How to pop the interpreter's stack?

kj no.email at please.post
Sun Dec 26 14:09:34 EST 2010


In <mailman.280.1293287106.6505.python-list at python Robert Kern <robert.kern at gmail.com> writes:

>Except that the *caller* never gets the traceback (unless if it deliberately 
>inspects the stack for some metaprogramming reason). It gets the exception, and 
>that is the same no matter what you do. The developer/user gets the traceback, 
>and those implementation details *are* often important to them.


Just look at what Python shows you if you pass the wrong number of
arguments to a function:

>>> def spam(x, y, z): pass
... 
>>> spam(1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: spam() takes exactly 3 arguments (2 given)


That's it.  The traceback stops at the point of the error.  Python
doesn't show you all the underlying C-coded machinery that went
into detecting the error and emitting the error message.  *No one*
needs this information at this point.  All I'm saying is that I
want to do the same thing with my argument validation code as Python
does with its argument validation code: keep it out of sight.  When
my argument validation code fires an exception ***there's no bug
in **my** code***.  It's doing exactly what it's supposed to do.
Therefore, there's no need for me to debug anything, and certainly
no need for me to inspect the traceback all the way to the exception.
The bug is in the code that called my function with the wrong
arguments.  The developer of that code has no more use for seeing
the traceback all the way to where my code raises the exception
than I have for seeing the traceback of Python's underlying C code
when I get an error like the one shown above.

~kj



More information about the Python-list mailing list