472,117 Members | 2,626 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

unhandled exception question

I'm working on a test application that embeds the Python interpreter.
I have the following problem...

I've created my own interactive interpreter loop. Essentially, it
reads the command from the prompt and calls the following C code:

PyObject* pMainModule = PyImport_AddModule("__main__");
PyObject* pMainDictionary = PyModule_GetDict(pMainModule);
PyObject* pObj = PyRun_String(pCommandText, Py_single_input,
pMainDictionary, pMainDictionary);

where pCommandText is the text entered by the user.

I also have the following Python functions defined:

def Boo():
raise Exception()

def Foo():
try:
MyModule.SomeFunction()
except Exception:
print "goodbye"

MyModule.SomeFunction() is defined in C. All it does is call Boo()
with the following code:

PyObject* pMainModule = PyImport_AddModule("__main__");
PyObject* pMainDictionary = PyModule_GetDict(pMainModule);
PyObject* pObj = PyRun_String("Boo()", Py_single_input,
pMainDictionary, pMainDictionary);

If it's at all relevent, I'm using Boost.Python to embed MyModule.

Now here's the problem: when I type "Foo()" at the prompt, it calls
Foo(), which calls MyModule.SomeFunction(), which calls Boo(), which
raises the exception. However, the exception doesn't get handled,
despite the try-except statement in Foo(). Even stranger, if I
manually enter the entire multi-line try-except statement at the
prompt (instead of simply calling Foo()), the exception is handled
properly and "goodbye" is printed to the screen.

What's going on here? Why is exception properly handled in the second
case, but not the first?

May 25 '07 #1
0 956

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by terry | last post: by
reply views Thread by Colmeister | last post: by
5 posts views Thread by Simon Tamman {Uchiha Jax} | last post: by
reply views Thread by Mike S | last post: by
reply views Thread by leo001 | last post: by

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.