[Swig-user] How to receive a FILE* from Python under MinGW?

Carl Douglas carl.douglas at gmail.com
Tue Mar 20 23:08:32 EDT 2007


Hi John,

I had exactly this problem... use the PyFile_AsFile function.  Below
is a code snippet from my project:

		// Thank you:  http://www.ragestorm.net/tutorial?id=21#8
		PyObject* PyFileObject = PyFile_FromString(ofn.lpstrFile, "r");

		if (PyFileObject == NULL)
		{
			PyErr_Print();
			PyErr_Clear();
			return false;
		}

		// Because microsoft C runtimes are not binary compatible, we can't
		// just call fopen to get a FILE * and pass that FILE * to another application
		// or library (Python25.dll in this case) that uses a different version
		// of the C runtime that this DLL uses. Using PyFile_AsFile is a
work-around...

		if (PyRun_SimpleFile(PyFile_AsFile(PyFileObject), ofn.lpstrFile) == -1)
		{
			PyErr_Print();
			PyErr_Clear();
		}

		Py_DECREF(PyFileObject);

For the full source of my SWIG/Python project, poke around here:

http://l3dtpython.googlecode.com/svn/trunk/cdPython/

Hope that helps.

On 3/21/07, John Pye <john.pye at student.unsw.edu.au> wrote:
> Hi all
>
> There is fairly well-known problem on MinGW with passing FILE pointers
> to Python. The problem seems to be that Python depends on a different
> version of msvcrt*.dll to that which MinGW depends on, and these
> different C runtimes provide different FILE implementations.
>
> The upshot of that seems to be that file objects created in Python via
> the 'file(...)' command can't safely be used in a SWIG module. It can be
> done perfectly (using SWIG typemap and PyFile_AsFile) on Linux but it
> fails with a segfault on Windows.
>
> What is the best possible workaround for this? Surely someone must have
> found a satisfactory solution to this problem?
>
> Cheeers
> JP
>
> --
> John Pye
> Department of Mechanical and Manufacturing Engineering
> University of New South Wales, Sydney, Australia
> http://pye.dyndns.org/
>
>
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share your
> opinions on IT & business topics through brief surveys-and earn cash
> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
> _______________________________________________
> Swig-user mailing list
> Swig-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/swig-user
>



More information about the Python-list mailing list