starting PythonWin from script called by embedded interpreter

Mike Haspert mike_haspert at credence.com
Thu Jul 12 16:24:43 EDT 2001


Hi all:
First I've got to say it's been fun "learning by lurking." But now
I've learned enough to get in over my head, and I'd sure appreciate
some advice.

I'm investigating the pain vs profit of using python as a
customization layer inside an MFC project: An MFC project embeds the
python interpreter, and calls python scripts which optionally use SWIG
wrapped C or C++ extensions.

I quite like Mark Hammond's PythonWin but hit problems when I tried to
use it to debug a function called by PyEval_CallObject(), but _only_
in an MFC app.

THE SETUP:
I'm using python 2.1, VC++6.0, and NT4.0 sp6.
I've embedded the python interpreter into 6 testcase projects, debug
and release builds of a console app, a console app supporting MFC, and
an MFC app. Each app does the same thing: It calls a function in a
python module with the following sequence. (error checking and
incref/decref'ing elided)

Py_Initialize();
PyObject *mod = PyImport_ImportModule("testmodule");
PyObject *func = PyObject_GetAttrString(mod, "f1");
PyObject *args = Py_BuildValue("(i)", 7))	//just a test value
PyObject *rvals = PyEval_CallObject(func,args);
//do stuff with rval
Py_Finalize();

THE PROBLEM:
When I run my MFC app testcase or my "console app supporting MFC"
testcase, PyEval_CallObject() returns 0. Doing prints inside the
script shows that "import pywin.debugger" succeeds and the line
"pywin.debugger.set_trace()" doesn't.

Yow. 
Can I make the interpreter spit out more info on what happened? Can I
make PythonWin spit out info on what happened?
Does anyone recognize this because they went through it already?
Any advice on debugging embedded Python?
Any caveats about embedding Python in an MFC project?

MORE DETAILS:
All testcases work if I don't try to start the debugger from inside
the pyscript. (debug build projects use python21_d.dll)

I'm following the example in the debugger tutorial and adding the
following lines in the function I've called with PyEval_CallObject()
in order to start the debugger.
	import pywin.debugger
	pywin.debugger.set_trace()

SOME SUCCESS:
Release build Console app testcase successfully starts the debugger:
 
I can step through my C++ code. When PyEval_CallObject()executes,
PythonWin starts and I can step through the script. Wonderful. When
the script finishes, I can continue stepping through my C++ code
again.  Wonderful again.   This is what I want to be able to
accomplish in an MFC project.

(BTW, in case you came to a full stop reading about stepping through a
release build, you can alter release build settings to allow it.
Details in MSDN's "Turn on Generation of Debug Information for the
Release Build")

Debug build testcases fail, but I expected that:
PythonWin doesn't work when my C++ project is a debug build. I expect
that I'll have to get the source for the windows extensions and
pythonwin and do a debug build to remedy this.



More information about the Python-list mailing list