Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e87656

Browse files
committed
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!
1 parent 497314e commit 3e87656

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

Python/pythonrun.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,21 @@ maybe_pyc_file(FILE *fp, char* filename, char* ext, int closeit)
578578
be read as they are on disk. */
579579
unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
580580
unsigned char buf[2];
581-
if (fread(buf, 1, 2, fp) == 2
582-
&& ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
583-
return 1;
584-
fseek(fp, 0, SEEK_SET);
581+
/* Mess: In case of -x, the stream is NOT at its start now,
582+
and ungetc() was used to push back the first newline,
583+
which makes the current stream position formally undefined
584+
until that newline is read back. So first we getc(), so
585+
that ftell() is well-defined.
586+
*/
587+
const int maybepushedback = getc(fp);
588+
const long currentpos = ftell(fp);
589+
int ispyc = 0;
590+
rewind(fp);
591+
ispyc = fread(buf, 1, 2, fp) == 2 &&
592+
((unsigned int)buf[1]<<8 | buf[0]) == halfmagic;
593+
fseek(fp, currentpos, SEEK_SET);
594+
ungetc(maybepushedback, fp);
595+
return ispyc;
585596
}
586597
return 0;
587598
}

0 commit comments

Comments
 (0)