Pop return from stack?

bvdp bob at mellowood.ca
Sat Aug 14 19:05:05 EDT 2010


Assuming I have a module 'foo.py' with something like this:

def error(s):
    print "Error", s
    sys.exit(1)

def func(s):
    ... do some processing
    ... call error() if bad .. go to system exit.
    ...  more processing

and then I write a new program, test.py, which:

import foo

def myerror(s):
    print "new error message"

foo.error = myerror

a = foo.func(..)

Now, if an error is encountered myerror() is called. Fine. But
execution resumes in func(). Not exactly what I wanted.

I can "fix" this simply by wrapping the call to foo.func() in a try/
expect and have myerror() raise an exception. This appears to work,
but I'm hesitant to use this out of fear that I'm building up some
kind of stack overflow or something which will bite me later.

Is there a better way? Simplest for an old assembler guy like me would
be pop a return address off the stack ... but python isn't
assembler :)

I don't want to change stuff in the foo.py module since it's part of
an existing program. But, if I must, I suppose I could. I'd prefer to
just short-circuit this if possible.

Thanks.



More information about the Python-list mailing list