comparison Python/pythonrun.c @ 16648:a51b9961f3bb legacy-trunk

Bug #132850 unix line terminator on windows. Miserable hack to replace the previous miserable hack in maybe_pyc_file.
author Tim Peters <tim.peters@gmail.com>
date Sat, 17 Feb 2001 22:02:34 +0000
parents e8e636cc1a09
children 27f04d714ecb
comparison
equal deleted inserted replaced
16647:728c11562187 16648:a51b9961f3bb
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 /* Mess: In case of -x, the stream is NOT at its start now, 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, 582 and ungetc() was used to push back the first newline,
583 which makes the current stream position formally undefined 583 which makes the current stream position formally undefined,
584 until that newline is read back. So first we getc(), so 584 and a x-platform nightmare.
585 that ftell() is well-defined. 585 Unfortunately, we have no direct way to know whether -x
586 was specified. So we use a terrible hack: if the current
587 stream position is not 0, we assume -x was specified, and
588 give up. Bug 132850 on SourceForge spells out the
589 hopelessness of trying anything else (fseek and ftell
590 don't work predictably x-platform for text-mode files).
586 */ 591 */
587 const int maybepushedback = getc(fp);
588 const long currentpos = ftell(fp);
589 int ispyc = 0; 592 int ispyc = 0;
590 rewind(fp); 593 if (ftell(fp) == 0) {
591 ispyc = fread(buf, 1, 2, fp) == 2 && 594 if (fread(buf, 1, 2, fp) == 2 &&
592 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic; 595 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
593 fseek(fp, currentpos, SEEK_SET); 596 ispyc = 1;
594 ungetc(maybepushedback, fp); 597 rewind(fp);
598 }
595 return ispyc; 599 return ispyc;
596 } 600 }
597 return 0; 601 return 0;
598 } 602 }
599 603