comparison Python/pythonrun.c @ 16560:e8e636cc1a09 legacy-trunk

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!
author Tim Peters <tim.peters@gmail.com>
date Sun, 11 Feb 2001 04:35:39 +0000
parents aa844711463a
children a51b9961f3bb
comparison
equal deleted inserted replaced
16559:8c1c787bd6f9 16560:e8e636cc1a09
576 /* Read only two bytes of the magic. If the file was opened in 576 /* Read only two bytes of the magic. If the file was opened in
577 text mode, the bytes 3 and 4 of the magic (\r\n) might not 577 text mode, the bytes 3 and 4 of the magic (\r\n) might not
578 be read as they are on disk. */ 578 be read as they are on disk. */
579 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF; 579 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
580 unsigned char buf[2]; 580 unsigned char buf[2];
581 if (fread(buf, 1, 2, fp) == 2 581 /* Mess: In case of -x, the stream is NOT at its start now,
582 && ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic) 582 and ungetc() was used to push back the first newline,
583 return 1; 583 which makes the current stream position formally undefined
584 fseek(fp, 0, SEEK_SET); 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;
585 } 596 }
586 return 0; 597 return 0;
587 } 598 }
588 599
589 int 600 int