[ python-Feature Requests-1324176 ] fix for ms stdio tables

SourceForge.net noreply at sourceforge.net
Wed Nov 9 04:12:23 CET 2005


Feature Requests item #1324176, was opened at 2005-10-11 22:46
Message generated for change (Comment added) made by kotk
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1324176&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Windows
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Vambola Kotkas (kotk)
Assigned to: Nobody/Anonymous (nobody)
Summary: fix for ms stdio tables 

Initial Comment:
Hopefully its right tracker for such request.
I am mainly C++ developer but recently it become 
desirable to embed python as python24.dll into my 
existing app to make some of features scriptable. I don't 
want it to have any visible UI or to deal with GUI or the 
like.
However ms has made it so that each dll has its own rtl 
ioinfo table that is loaded at program start. In other 
words .. some other dll or exe itself cannot simply 
redirect python24.dll stdin stdout stderr runtime. It 
probably  has to be done from within python for these 
redirections to really take effect. I think it is actually very 
simple to fix that dll problem on C level by making some 
callable thing into python24.dll that does nothing but 
fixes dlls ioinfo table to contain real stdio for the 
process. Example:

#include <windows.h>
#include <io.h>

void Py_FixMSstdioPITA(void)
{
	/*fix stdin*/
	HANDLE hReal = GetStdHandle
(STD_INPUT_HANDLE);
	HANDLE hKnown = _get_osfhandle(_fileno
(stdin));
	int Number;
	if (hReal != hKnown && hReal != 
INVALID_HANDLE_VALUE)
	{
		Number = _open_osfhandle 
(hReal, _O_BINARY|_O_RDONLY);
		_dup2(Number,_fileno(stdin));
	}
	/*fix stdout*/
	hReal = GetStdHandle
(STD_OUTPUT_HANDLE);
	hKnown = _get_osfhandle(_fileno(stdout));
	if (hReal != hKnown && hReal != 
INVALID_HANDLE_VALUE)
	{
		Number = _open_osfhandle 
(hReal, _O_BINARY|_O_WRONLY);
		_dup2(Number,_fileno(stdout));
	}
	/*fix stderr*/
	hReal = GetStdHandle
(STD_ERROR_HANDLE);
	hKnown = _get_osfhandle(_fileno(stderr));
	if (hReal != hKnown && hReal != 
INVALID_HANDLE_VALUE)
	{
		Number = _open_osfhandle 
(hReal, _O_BINARY|_O_WRONLY);
		_dup2(Number,_fileno(stderr));
	}
}

I want just call it from outside after any io redirection 
done in my code ... so python has his stdio all right and 
done. 
If something like this is already implemented there in 
similar or some other way then sorry didnt find it. If its 
agains general python ideology of some sort then sorry 
didnt know of it. Let me know please.
Best wishes,
Vambola Kotkas

----------------------------------------------------------------------

>Comment By: Vambola Kotkas (kotk)
Date: 2005-11-09 05:12

Message:
Logged In: YES 
user_id=698026

Right, python24.dll is compiled with /MD option. I was testing 
it under debug version and that is compiled with /MDd option. 
When i change the compiler option /MDd to /MD in my apps 
debug version then the function works outside too.
Thanks

----------------------------------------------------------------------

Comment By: Vambola Kotkas (kotk)
Date: 2005-11-09 02:32

Message:
Logged In: YES 
user_id=698026

Yes, first i did use that function in my application but it 
surprizingly did not help.  Googling about python IO 
redirection indicated that some others have had similar 
difficulties. So i debugged around and noticed that the very 
table that my above function was meant to fix  (possibly right 
that it was named __pioinfo ... MS maps windows handles to 
CRT FILE pointers there) had various locations. Maybe it was 
because compiler /M option differed between the module 
where i did IO redirection and with what python24.dll was 
compiled. Like... say msvcr71d.dll was loaded for it but 
msvcr71.dll loaded for python24.dll? :-/  I check it over again 
when i get time.
Thanks.

----------------------------------------------------------------------

Comment By: Vambola Kotkas (kotk)
Date: 2005-11-09 02:32

Message:
Logged In: YES 
user_id=698026

Thank you.

I've moved this feature request to PEP 42, "Feature Requests".


----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2005-11-08 01:36

Message:
Logged In: YES 
user_id=21627

I don't understand the remark "has its own rtl ioinfo
table". ioinfo is a type - what table are you referring to?

If you are talking about __pioinfo: this certainly isn't
defined per DLL. Instead, each copy of the MS C runtime has
one copy. Python (in 2.4) uses msvcr71.dll. So as long as
you also link against msvcr71.dll, you can modify the CRT
that Python uses without modifying python24.dll.

IOW, you can use your function in your application, and be done.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1324176&group_id=5470


More information about the Python-bugs-list mailing list