Embedded Python 3 porting issues when passing FILE* to PyRun_SimpleFile() in Windows mixed-compiler environment

Deron Meranda deron.meranda at gmail.com
Fri Oct 5 00:13:17 EDT 2012


Hi.  I'm trying to convert a large C application that embeds Python so it works with Python 3, and am running into an new API limitation I can't solve.  I have a report from Windows users that there is a crashing problem occurring with calls to PyRun_SimpleFile (and similar functions).  This appears to happen when people are using a Python DLL compiled with one compiler (e.g., MicroSoft's) and the application with another compiler (e.g., MinGW/gcc).

The basic problem is that C type (FILE*) is not defined by the Windows OS, but instead is specific to the compiler and its associated runtime-environment. So therefore it is not safe to pass a FILE* across DLL boundaries.  However many of the Python C API functions only accept a FILE*.

Under Python 2 a work-around was used by calling the PyFile_FromString() then PyFile_AsFile(), to effectively get the underlying fopen() to occur inside the Python DLL so that an incompatible FILE* is not passed across the DLL boundary.

However many of those PyFile functions, including PyFile_FromFile(), were removed from the Python 3 API.  So how can one safely call PyRun_SimpleFile() in Python 3 in Windows where different compilers could be used?

Thanks
-- 
Deron Meranda
http://deron.meranda.us/



More information about the Python-list mailing list