[python-win32] embedding IPython in a windows dll

mani sabri mani.sabri at gmail.com
Tue Mar 11 17:25:28 CET 2008



>-----Original Message-----
>From: python-win32-bounces at python.org [mailto:python-win32- 
>bounces at python.org] On Behalf Of Tim Roberts mani sabri wrote:
>> I want to run a Python shell from a DllMain() function.
>
>It isn't safe to do this much processing in a DllMain.  There is a 
>thing called the "loader lock" that is held while DllMain is called, 
>and it can prevent you from loading other DLLs.  You might try spinning 
>off a thread to do your real work.
>
>

Wow thanks,I'll difinitly use a thread. Do have any link to a sample code or
some keywords for google?

>> 	freopen("CONOUT$","w",stdout);
>> 	freopen("CONIN$","w",stdin);
>> 	freopen("CONERR$","w",stderr);
>>
>
>Do you really think "w" is correct for all three of those?
>
>> 	FILE* afile = fopen("CONOUT$", "w+");
>> 	FILE* bfile = fopen("CONIN$", "w+");
>
>Again, do you really think you want "w+ for both of these?  How much 
>writing will you do to stdin?
>
>> 	PySys_SetObject("stdin", PyFile_FromFile(bfile, "CONIN$","wb", 
>> fclose));
>
>Again, you're probably going to want to read from stdin, not write to 
>it.

Thanks a lot. I'm sure I'll never forget that.

>  And you don't really need to do the fopen yourself:
>
>    PyFileObject* xout = PyFile_FromString( "CONOUT$", "wb" );
>    PyFileObject* xin = PyFile_FromString( "CONIN$", "rb" );
>    PySys_SetObject( "stdout", xout );
>    PySys_SetObject( "__stdout__", xout );
>    PySys_SetObject( "stdin", xin );
>    PySys_SetObject( "__stdin__", xin );

And this, I was not able to compile it[1] until I changed it a little
bit[2].

[1] It was 6 errors, first and seconed line of the kind:
expertsample.cpp(85) : error C2440: 'initializing' : cannot convert from
'PyObject *' to 'PyFileObject *'
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast

And the remaining four of the kind:
expertsample.cpp(87) : error C2664: 'PySys_SetObject' : cannot convert
parameter 2 from 'PyFileObject *' to 'PyObject *'
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast

[2] So I changed the code to:

PyFileObject* xout = (PyFileObject*)PyFile_FromString("CONOUT$","wb");
PyFileObject* xin = (PyFileObject*)PyFile_FromString( "CONIN$", "rb" );
PySys_SetObject( "stdout", (PyObject*)xout ); PySys_SetObject( "__stdout__",
(PyObject*)xout ); PySys_SetObject( "stdin", (PyObject*)xin );
PySys_SetObject( "__stdin__", (PyObject*)xin );

But still no output after a PyRun_SimpleString("print 'Hello from
python'\n");

>--
>Tim Roberts, timr at probo.com
>Providenza & Boekelheide, Inc.

Best regards,
Mani



More information about the python-win32 mailing list