This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: fix for ms stdio tables
Type: enhancement Stage:
Components: Windows Versions:
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: kotk, loewis
Priority: normal Keywords:

Created on 2005-10-11 19:46 by kotk, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg54632 - (view) Author: Vambola Kotkas (kotk) Date: 2005-10-11 19:46
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
msg54633 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2005-11-07 23:36
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.
msg54634 - (view) Author: Vambola Kotkas (kotk) Date: 2005-11-09 00:32
Logged In: YES 
user_id=698026

Thank you.

I've moved this feature request to PEP 42, "Feature Requests".
msg54635 - (view) Author: Vambola Kotkas (kotk) Date: 2005-11-09 00:32
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.
msg54636 - (view) Author: Vambola Kotkas (kotk) Date: 2005-11-09 03:12
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
msg54637 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2005-11-09 05:28
Logged In: YES 
user_id=21627

It normally shouldn't happen that you get two copies of
msvcr71.dll. To analyse this problem, you should look at
your application in depends.exe; if it shows different
versions of the CRT, you lose. In your case, it would have
shown both msvcr71.dll and msvcr71d.dll, each having its own
notion of stdio.

Never ever mix different CRT versions. It will not only not
work; it may even crash.

Closing this as "works for me".
History
Date User Action Args
2022-04-11 14:56:13adminsetgithub: 42468
2005-10-11 19:46:55kotkcreate