Python Extensions with Borland's Free Compiler Tools

Ng Pheng Siong ngps at madcap.dyndns.org
Tue Jun 13 10:50:45 EDT 2000


Hi,

Here's a followup to Gordon Williams's document 'Creating Python 
extensions using Borland's free compiler', available at the following
URLs:

    http://www.deja.com/=dnc/getdoc.xp?AN=619698470 
    http://www.cyberus.ca/~g_will/pyExtenDL.shtml


I've found a problem with the above in my environment. Using Gordon's 
example, if I change example_add() to simply return Py_None (after 
Py_INCREF()'ing it, of course), instead of a Python integer, I get a crash.


Py_None is declared thusly in Python's Include/object.h:

  extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */
  #define Py_None (&_Py_NoneStruct)


And in Python's config.h, DL_IMPORT is defined thusly:

  #ifdef _M_IX86 (this for MS C compiler)
  [ other stuff ... ]
  #define DL_IMPORT(RTYPE) __declspec(dllimport) RTYPE
  #endif
  
  #ifdef __BORLANDC__
  [ other stuff ... ]
  #define DL_IMPORT(RTYPE) RTYPE __import
  #endif


DL_IMPORT appears to be defined incompatibly. Compiling an extension with 
BC and linking it with Python's MSVC-built core generates a runtime crash 
when Py_None is [de]referenced.


Here's how to work around it:

1. Add the following to Python's config.h, after the Windows 
compiler-specific stuff:

  #ifdef MSC_CORE_BC_EXT
  #define DL_IMPORT(RTYPE) __declspec(dllimport) RTYPE
  #define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
  #endif

  /* End of compilers - finish up */

Note the above comment line already exists. 


2. When compiling an extension, add "-DMSC_CORE_BC_EXT" to CFLAGS.

MSC_CORE_BC_EXT informs the compiler it is compiling an extension with 
BC for linkage with MSVC-built Python core.


I've used the above technique with M2Crypto, where (in the latest snapshot,
to be posted) the SWIG-generated extension is compiled by BC and then linked 
with MSVC-built Python and OpenSSL DLLs. It seems to work. In fact, at one 
troublesome spot where the MSVC-built package crashes, the BC-built one works! 
Go figure...


Cheers.

-- 
Ng Pheng Siong <ngps at post1.com> * http://www.post1.com/home/ngps




More information about the Python-list mailing list