[Tutor] Using python *.py files like DLL...

Sebastien Auclair sxa@fluent.com
Mon Dec 16 16:50:02 2002


This is a multi-part message in MIME format.

------=_NextPart_000_0044_01C2A523.1F5D1EB0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi  !

We need to find a correct strategy for loading PYTHON modules as defined =
in *.py files. (From a C++ program)

We are porting a C++ software from Linux to Win2000. Our software can =
load modules implemented in either DLL (.so on Linux) or
PYTHON files. (*.py).

The way the doc is made, it is very hard to findout how to do that. It =
is even harder to find how to do it so that it doesn't crash !

The problem is that we just discovered that our strategy for loading =
python files doesn't work under Win2000.
We were using this call sequence :
///////////////
    FILE* fd =3D fopen(fname,"r"); // fname... for instance =
"moduleABC.py"
    struct _node* nd =3D PyParser_SimpleParseFile( fd, fname, =
Py_file_input );
    PyObject* code =3D (PyObject*)PyNode_Compile( nd, fname );
    PyObject* module =3D PyImport_ExecCodeModule( =
const_cast<char*>(name),
code );
    Py_INCREF( module );
///////////////

This is the equivalent of "LoadLibrary" for dll cases.

This code works fine under Linux but there's maybe a bug with the WIN32 =
version of PYTHON.
PyParser_SimpleParseFile causes a :
    "Unhandled exception in TOTO.exe (NTDLL.DLL): 0xC0000005: ACCESS =
VIOLATION."

In all cases, we need an alternative !!! How can we create PyObject from =
PYTHON source contained in a regular *.py file ?

We would at least need to find a way to debug Python C/API. Just =
printing the PyErr_Print call is one hell of a chalange ! (In fact, we =
haven't found a way to do that on WIN2000).

SETUP:
    WIN2000
    PYTHON 2.2.2
    VC++ 6.0
    debug and release versions produces the same result.

Thanks in advance !
________________________________
Seb.

=20

------=_NextPart_000_0044_01C2A523.1F5D1EB0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.3315.2870" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial size=3D2>Hi&nbsp; !<BR><BR>We need to find a =
correct=20
strategy for loading PYTHON modules as defined in *.py files. (From a =
C++=20
program)<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><FONT face=3DArial size=3D2>We are =
porting a C++=20
software from Linux to Win2000. Our software can load modules =
implemented in=20
either DLL (.so on Linux) or<BR>PYTHON files. =
(*.py).</FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><BR>The way the doc is made, it is very =
hard to=20
findout how to do that. It is even&nbsp;harder to find how to do it so =
that it=20
doesn't crash !</FONT></DIV>
<DIV><FONT face=3DArial size=3D2><BR>The problem is that we just =
discovered that our=20
strategy for loading python files doesn't work under Win2000.<BR>We were =
using=20
this call sequence :<BR>///////////////<BR>&nbsp;&nbsp;&nbsp; FILE* fd =
=3D=20
fopen(fname,"r"); // fname... for instance =
"moduleABC.py"<BR>&nbsp;&nbsp;&nbsp;=20
struct _node* nd =3D PyParser_SimpleParseFile( fd, fname, Py_file_input=20
);<BR>&nbsp;&nbsp;&nbsp; PyObject* code =3D (PyObject*)PyNode_Compile( =
nd, fname=20
);<BR>&nbsp;&nbsp;&nbsp; PyObject* module =3D PyImport_ExecCodeModule(=20
const_cast&lt;char*&gt;(name),<BR>code );<BR>&nbsp;&nbsp;&nbsp; =
Py_INCREF(=20
module );<BR>///////////////</FONT></DIV>
<DIV><FONT face=3DArial size=3D2><BR>This is the equivalent of =
"LoadLibrary" for dll=20
cases.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2><BR>This code works fine under Linux =
but there's=20
maybe a bug with the WIN32 version of =
PYTHON.<BR>PyParser_SimpleParseFile causes=20
a :<BR>&nbsp;&nbsp;&nbsp; "Unhandled exception in TOTO.exe (NTDLL.DLL):=20
0xC0000005: ACCESS VIOLATION."<BR><FONT face=3DArial size=3D2><BR>In all =
cases, we=20
need an alternative !!! How can we create PyObject from PYTHON source =
contained=20
in a regular *.py file ?<BR></FONT><BR>We would at least need to find a =
way to=20
debug Python C/API. Just printing the PyErr_Print call is one hell of a =
chalange=20
! (In fact, we haven't found a way to do that on =
WIN2000).<BR></FONT><FONT=20
face=3DArial size=3D2><FONT face=3DArial size=3D2></FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><FONT face=3DArial=20
size=3D2>SETUP:<BR>&nbsp;&nbsp;&nbsp; WIN2000<BR>&nbsp;&nbsp;&nbsp; =
PYTHON=20
2.2.2<BR>&nbsp;&nbsp;&nbsp; VC++ 6.0<BR>&nbsp;&nbsp;&nbsp; debug and =
release=20
versions produces the same result.</FONT></DIV>
<DIV>
<DIV></FONT><FONT face=3DArial size=3D2><BR>Thanks in advance=20
!<BR>________________________________<BR>Seb.<BR></DIV></DIV></FONT>
<DIV><FONT face=3DArial =
size=3D2></FONT>&nbsp;</DIV></FONT></DIV></BODY></HTML>

------=_NextPart_000_0044_01C2A523.1F5D1EB0--