Mercurial > cpython-lru_cache-weakref
diff Python/import.c @ 43605:a2bb76d11d66
Patch 1267 by Christian Heimes.
Move the initialization of sys.std{in,out,err} and __builtin__.open
to C code.
This solves the problem that "python -S" wouldn't work.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 19 Oct 2007 23:16:50 +0000 |
| parents | 19319e70fc3e |
| children | f0bcc11ca965 |
line wrap: on
line diff
--- a/Python/import.c Fri Oct 19 22:06:24 2007 +0000 +++ b/Python/import.c Fri Oct 19 23:16:50 2007 +0000 @@ -91,6 +91,9 @@ /* This table is defined in config.c: */ extern struct _inittab _PyImport_Inittab[]; +/* Method from Parser/tokenizer.c */ +extern char * PyTokenizer_FindEncoding(FILE *fp); + struct _inittab *PyImport_Inittab = _PyImport_Inittab; /* these tables define the module suffixes that Python recognizes */ @@ -2558,6 +2561,7 @@ struct filedescr *fdp; char pathname[MAXPATHLEN+1]; FILE *fp = NULL; + char *encoding = NULL; pathname[0] = '\0'; if (path == Py_None) @@ -2566,7 +2570,14 @@ if (fdp == NULL) return NULL; if (fp != NULL) { - fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose); + if (strchr(fdp->mode, 'b') == NULL) { + /* Python text file, get encoding from tokenizer */ + encoding = PyTokenizer_FindEncoding(fp); + encoding = (encoding != NULL) ? encoding : + (char*)PyUnicode_GetDefaultEncoding(); + } + fob = PyFile_FromFileEx(fp, pathname, fdp->mode, fclose, -1, + (char*)encoding, NULL); if (fob == NULL) { fclose(fp); return NULL;
