unhandled exception question

J

joshusdog

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top