diff options
author | Guido van Rossum <guido@python.org> | 2007-10-22 00:09:51 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-10-22 00:09:51 (GMT) |
commit | 40d20bcf1fccfe8af2393f1aec88ba18e38d0bc1 (patch) | |
tree | 0d6616333b9c3cb004b659751fe9baa4bac67ad2 /Python | |
parent | c2954e5273520031d3debfac8c668b9e611303b3 (diff) | |
download | cpython-40d20bcf1fccfe8af2393f1aec88ba18e38d0bc1.zip cpython-40d20bcf1fccfe8af2393f1aec88ba18e38d0bc1.tar.gz cpython-40d20bcf1fccfe8af2393f1aec88ba18e38d0bc1.tar.bz2 |
Issue 1267, continued.
Additional patch by Christian Heimes to deal more cleanly with the
FILE* vs file-descriptor issues.
I cleaned up his code a bit, and moved the lseek() call into import.c.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/import.c | 17 | ||||
-rw-r--r-- | Python/pythonrun.c | 6 |
2 files changed, 16 insertions, 7 deletions
diff --git a/Python/import.c b/Python/import.c index 2a316ca..2493554 100644 --- a/Python/import.c +++ b/Python/import.c @@ -92,7 +92,7 @@ static PyObject *extensions = NULL; extern struct _inittab _PyImport_Inittab[]; /* Method from Parser/tokenizer.c */ -extern char * PyTokenizer_FindEncoding(FILE *fp); +extern char * PyTokenizer_FindEncoding(int); struct _inittab *PyImport_Inittab = _PyImport_Inittab; @@ -2561,6 +2561,7 @@ call_find_module(char *name, PyObject *path) struct filedescr *fdp; char pathname[MAXPATHLEN+1]; FILE *fp = NULL; + int fd = -1; char *found_encoding = NULL; char *encoding = NULL; @@ -2571,17 +2572,25 @@ call_find_module(char *name, PyObject *path) if (fdp == NULL) return NULL; if (fp != NULL) { + fd = fileno(fp); + if (fd != -1) + fd = dup(fd); + fclose(fp); + fp = NULL; + } + if (fd != -1) { if (strchr(fdp->mode, 'b') == NULL) { /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed memory. */ - found_encoding = PyTokenizer_FindEncoding(fp); + found_encoding = PyTokenizer_FindEncoding(fd); + lseek(fd, 0, 0); /* Reset position */ encoding = (found_encoding != NULL) ? found_encoding : (char*)PyUnicode_GetDefaultEncoding(); } - fob = PyFile_FromFileEx(fp, pathname, fdp->mode, fclose, -1, + fob = PyFile_FromFd(fd, pathname, fdp->mode, -1, (char*)encoding, NULL); if (fob == NULL) { - fclose(fp); + close(fd); PyMem_FREE(found_encoding); return NULL; } diff --git a/Python/pythonrun.c b/Python/pythonrun.c index f641547..330667a 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -719,7 +719,7 @@ initstdio(void) } /* Set sys.stdin */ - if (!(std = PyFile_FromFileEx(stdin, "<stdin>", "r", fclose, -1, + if (!(std = PyFile_FromFd(fileno(stdin), "<stdin>", "r", -1, NULL, "\n"))) { goto error; } @@ -728,7 +728,7 @@ initstdio(void) Py_DECREF(std); /* Set sys.stdout */ - if (!(std = PyFile_FromFileEx(stdout, "<stdout>", "w", fclose, -1, + if (!(std = PyFile_FromFd(fileno(stdout), "<stdout>", "w", -1, NULL, "\n"))) { goto error; } @@ -737,7 +737,7 @@ initstdio(void) Py_DECREF(std); /* Set sys.stderr */ - if (!(std = PyFile_FromFileEx(stderr, "<stderr>", "w", fclose, -1, + if (!(std = PyFile_FromFd(fileno(stderr), "<stderr>", "w", -1, NULL, "\n"))) { goto error; } |