summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-02-11 04:35:39 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-02-11 04:35:39 (GMT)
commit3e876565a3913427fc64b57553cb6986f1f7512b (patch)
tree5d8e61b90ffdd5e7bb3bdb84a5510592d3651d40 /Python
parent497314e2e531350e362abf00bb8beaff5211d5b4 (diff)
downloadcpython-3e876565a3913427fc64b57553cb6986f1f7512b.zip
cpython-3e876565a3913427fc64b57553cb6986f1f7512b.tar.gz
cpython-3e876565a3913427fc64b57553cb6986f1f7512b.tar.bz2
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!
Diffstat (limited to 'Python')
-rw-r--r--Python/pythonrun.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 5e0377d..a26781e 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -578,10 +578,21 @@ maybe_pyc_file(FILE *fp, char* filename, char* ext, int closeit)
be read as they are on disk. */
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);
+ /* 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;
}