Debuggin a ptyhon extension

S

Steve Menard

I am writing a python extension module in VC++ on windows. At some
point, my extension crashes.

Now I want to debug exactly why. I built the python projects in VC++,
and put the resulting python_d.exe in my ptyon home directory, and the
python23_d.dll in the windows/system32 directory. I also tried to put
the python23_d.dll in python's home directory, to the same result.

Problem is, running my scripts/extension with python_d.exe, it always
complains it can't find win32api. Yes, I am using win32API to preload a
dll, and can't really get rid of it.

To try and solve this, I tried replacing the "stock" python.dll with my
python_d, so at least VC++'s debugger would see the debug info. but I
got the same result, of win32api not being found ...

What is the best way to go about this? My extension will require a fair
amount of native code and interacting with the Python code, so being
able to trace into python would be invaluable.

Are my problems caused because python 2.3.3 (and possible win32all) were
build using VC.net?

I am grateful for any help you guys can provide.


Steve Menard
(e-mail address removed)
 
R

Roger Upole

It's probably looking for the debug version of win32api
(win32api_d.pyd, which will also require a debug build of
Pywintypes). You can grab the pywin32 source and
roll your own debug binaries, or alternately you might
be able to get away with creating a release build of Python
that contains debug info.
hth
Roger
 
T

Thomas Heller

Roger Upole said:
It's probably looking for the debug version of win32api
(win32api_d.pyd, which will also require a debug build of Pywintypes).
You can grab the pywin32 source and roll your own debug binaries, or
alternately you might be able to get away with creating a release
build of Python that contains debug info.

If Steve only uses win32api to call LoadLibrary it is probably easier to
use ctypes for that. Of course he needs a debug version of ctypes, but
that should build out of the box from the sources, if he has VC.

No, python 2.3 and pywin32 (formerly called win32all) are built using
MSVC 6.

Thomas
 
M

Mike Rovner

Steve said:
I am writing a python extension module in VC++ on windows. At some
point, my extension crashes.

Welcome to the club. :)
Now I want to debug exactly why. I built the python projects in VC++,
and put the resulting python_d.exe in my ptyon home directory, and the
python23_d.dll in the windows/system32 directory. I also tried to put
the python23_d.dll in python's home directory, to the same result.

Problem is, running my scripts/extension with python_d.exe, it always
complains it can't find win32api. Yes, I am using win32API to preload
a dll, and can't really get rid of it.

To try and solve this, I tried replacing the "stock" python.dll with
my python_d, so at least VC++'s debugger would see the debug info.
but I got the same result, of win32api not being found ...

"Everyone is getting lost to the measure of his abilities" (c)
You choosed the hard way.
What is the best way to go about this? My extension will require a
fair amount of native code and interacting with the Python code, so
being able to trace into python would be invaluable.

Here what I did:

- Compile you _extension_ in debug mode. Name the output "myext.pyd".

- Call it with simple test script from dos (hopefully cmd) prompt: python
testmyext.py
import myext
stop=raw_input('connect now')
myext.run_crash_func()

-As script puses and prompt for input switch to VS and debug attach to
running process.
(Menu Tools/Debug Processes...; select your running python.exe and press
Attach)
Set appropriate breakpoints in your ext code. Continue.

-Switch to script and press Enter.

-Get breaked in C++ and enjoy debugging.
Are my problems caused because python 2.3.3 (and possible win32all)
were build using VC.net?

I use to debug my extensions with stock python (2.2.x & 2.3.x - both
compiled with MSVC6).
I use MSVC7.1. However I don't use FILE* between extension boundaries. No
problems so far.
I am grateful for any help you guys can provide.

Hope this helps,
Mike

PS. If anybody is interested, there is a way to debug extension not leaving
VS at all:
In the extension solution properties set Debugging:
-Command (extenal program) python.exe
-Command Arguments: testmyext.py (or whatever your calling script is)
and just hit Run to debug your code.
 
S

Steve Menard

Thomas said:
If Steve only uses win32api to call LoadLibrary it is probably easier to
use ctypes for that. Of course he needs a debug version of ctypes, but
that should build out of the box from the sources, if he has VC.




No, python 2.3 and pywin32 (formerly called win32all) are built using
MSVC 6.

Thomas

thanks for your help guys. While I didnt manage to get pywin32 working
with python_d, I wrote my own little library to do the loadLibrary, and
that works.

Thanks for the ctype hint though, I didnt know about it. Is it portable
across platforms?

Steve Menard
 
T

Thomas Heller

Steve Menard said:
thanks for your help guys. While I didnt manage to get pywin32 working
with python_d, I wrote my own little library to do the loadLibrary,
and that works.

Thanks for the ctype hint though, I didnt know about it. Is it
portable across platforms?

Yep. And it's called ctypes ;-)

The windows version is stable/late-beta, non-windows is probably alpha.

<http://starship.python.net/crew/theller/ctypes>

Let me add some further comments. IMO it pays off (if you have MSVC6)
to get your whole stuff working in debug mode. At least pywin32 (if you
use it), python, and your own stuff. And ctypes, of course.

And it's not that complicated anymore. Here's what I do:

- Install stock Python 2.3.3, from the python.org installer.

- Download the Python sources, or grab them with CVS (the
release-maint23 branch, if you use Python 2.3). Make a Python Debug
build with VC 6, from the .dsw file.

- In the PCBuild directory is a brand new pydebug.iss file (you have to
get this with CVS or ViewCVS), it can be used to create a windows
installer which installs the debug binaries alongside the release
binaries. Use Inno Setup by double clicking the iss file to create the
installer, and run it.

- Grab the pywin32 source from CVS, and build it with distutils by
entering 'python setup_win32all.py build' in the command prompt. This
builds everything in release mode, which takes quite some time. Build
in debug mode with 'python setup_win32all.py build -g'. Now create a
windows installer by 'python setup_win32all.py bdist_wininst'.

The installer is created in the dist subdirectory, an exe file. Run the
exe file to install the stuff (will install both the release and debug
build).

Thomas
 
J

Jerry Gamache

- Grab the pywin32 source from CVS, and build it with distutils by
entering 'python setup_win32all.py build' in the command prompt. This
builds everything in release mode, which takes quite some time. Build
in debug mode with 'python setup_win32all.py build -g'. Now create a
windows installer by 'python setup_win32all.py bdist_wininst'.

I found out that setting the following environment variables before
building PyWin32 in release mode forces the generation of PDB files and
allows some limited debugging in MSDEV. This will help you get call stacks
and trace through the code, but variable values might become difficult to
inspect due to code optimizations:

set CL=/Zi
set LINK=/DEBUG

The install part of the code understands PDB files and will copy them to
the proper locations when they are present.

This is not necessary when building in debug mode.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top