[Python-checkins] CVS: python/dist/src/Python pythonrun.c,2.121,2.122

Tim Peters tim_one@users.sourceforge.net
Sat, 10 Feb 2001 20:35:41 -0800


Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv16427/python/dist/src/Python

Modified Files:
	pythonrun.c 
Log Message:
Ugly fix for SF bug 131239 (-x flag busted).
Bug was introduced by tricks played to make .pyc files executable
via cmdline arg.  Then again, -x worked via a trick to begin with.
If anyone can think of a portable way to test -x, be my guest!


Index: pythonrun.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/pythonrun.c,v
retrieving revision 2.121
retrieving revision 2.122
diff -C2 -r2.121 -r2.122
*** pythonrun.c	2001/02/02 18:19:15	2.121
--- pythonrun.c	2001/02/11 04:35:39	2.122
***************
*** 579,586 ****
  		unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
  		unsigned char buf[2];
! 		if (fread(buf, 1, 2, fp) == 2
! 		    && ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
! 			return 1;
! 		fseek(fp, 0, SEEK_SET);
  	}
  	return 0;
--- 579,597 ----
  		unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
  		unsigned char buf[2];
! 		/* Mess:  In case of -x, the stream is NOT at its start now,
! 		   and ungetc() was used to push back the first newline,
! 		   which makes the current stream position formally undefined
! 		   until that newline is read back.  So first we getc(), so
! 		   that ftell() is well-defined.
! 		*/
! 		const int maybepushedback = getc(fp);
! 		const long currentpos = ftell(fp);
! 		int ispyc = 0;
! 		rewind(fp);
! 		ispyc = fread(buf, 1, 2, fp) == 2 &&
! 		        ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic;
! 		fseek(fp, currentpos, SEEK_SET);
! 		ungetc(maybepushedback, fp);
! 		return ispyc;
  	}
  	return 0;