popenX() misunderstanding on win32

Jon Nicoll jkn at nicorp.f9.co.uk
Tue May 28 10:08:00 EDT 2002


[repost - my previous attempt didn't seem to get through]

Following the useful comments from Steve Holden et. al., I made some
progress. I'll post a snippet to (a) show what works (so far) and (b)
to invite further comment.

I improved things by adding an fflush() to my program's printf()
statement.

--
/* upper.c - turn lower-case standard input into upper case, crudely
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char *argv[])
{
	int i;
	static char io[2048];

	while (1)
	{
		fgets(io, 2047,stdin);
		for (i = 0; i < strlen(io); i++)
		{
			if (islower(io[i]))
				io[i] = toupper(io[i]);
		}
		printf("%s\n", io);
		fflush(stdout);    /* this is the line I added */
	}

	return 1;
}

---
PythonWin 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)] on
win32.
Portions Copyright 1994-2001 Mark Hammond (mhammond at skippinet.com.au)
- see 'Help/About PythonWin' for further copyright information.
>
>>> import os
>>> po, pi = os.popen2("d:\\temp\\upper.exe", 't')
>>> po.write("junk\n")
>>> pi.readline()
'JUNK\n'                # good! ;-)
>>> pi.readline()
'\n'                    # OK

>>> pi.readline()	# don't do this! - will block


--

I do take the point about this all being rather fragile though. Any
suggestions?

    Thanks & Regards
    Jon N



More information about the Python-list mailing list