Running in Release or Debug version of the python interpreter?

R

Raphael Zulliger

Hi

I have to check wheter a .py script is run within the debug or the
release version of an embedded python interpreter (in short, wheter
python24_d.dll or python24.dll is in use).

long version: I'm using ctypes to load my own dll. There exists 2
version of this dll - a debug and a release version (where one is linked
against debug C runtime dll and the other to release C runtime dll). Now
I have to change the name of the dll I want to be loaded by ctypes...
But how can I find out which of the dll I have to load?!

Thanks in advance!
Raphael Zulliger
 
F

Fredrik Lundh

Raphael Zulliger wrote:

long version: I'm using ctypes to load my own dll. There exists 2
version of this dll - a debug and a release version (where one is linked
against debug C runtime dll and the other to release C runtime dll). Now
I have to change the name of the dll I want to be loaded by ctypes...
But how can I find out which of the dll I have to load?!

calling GetModuleFileName (in kernel32.dll) on sys.dllhandle should give
you the name of the current Python DLL. something like this should work:

from ctypes import *
MAX_PATH = 500
filename = c_buffer(MAX_PATH)
windll.kernel32.GetModuleFileNameA(sys.dllhandle, filename, MAX_PATH)
filename = filename.value

</F>
 
T

Thomas Heller

Raphael said:
Hi

I have to check wheter a .py script is run within the debug or the
release version of an embedded python interpreter (in short, wheter
python24_d.dll or python24.dll is in use).

long version: I'm using ctypes to load my own dll. There exists 2
version of this dll - a debug and a release version (where one is linked
against debug C runtime dll and the other to release C runtime dll). Now
I have to change the name of the dll I want to be loaded by ctypes...
But how can I find out which of the dll I have to load?!

Thanks in advance!
Raphael Zulliger

You could use imp.get_suffixes(). In a release build the returned list
will contain these entries
('.pyd', 'rb', 3), ('.dll', 'rb', 3)
in a debug build that will be
('_d.pyd', 'rb', 3), ('_d.dll', 'rb', 3)

Another way would be to look at the filename of any binary extension
module. _ctypes.__file__, for example: '.....\\_ctypes.pyd' vs.
'....\\_ctypes_d.pyd'.

Thomas
 
R

Raphael Zulliger

Thanks for your answers!
I prefer the proposal of Thomas Heller by using a small helper function
like this:

def IsDebugVersionRunning():
import imp
for suffix in imp.get_suffixes():
if suffix[0] == '_d.pyd':
return True
return False

This works well for me. The
_ctypes.__file__
trick didn't work for me - I guess I've done something wrong...

One more question: Under linux this doesn't seam to work - as there is
always returned '.so' (not '_d.so') at least in my tests. Are there no
debug/release version issues like on windows? doesn't import the
python2.4-dbg (debian) binary different modules than python2.4?

Raphael Zulliger
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top