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 /Parser/tokenizer.c | |
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 'Parser/tokenizer.c')
-rw-r--r-- | Parser/tokenizer.c | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 8c24cf2..5b3fd9e 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -1602,40 +1602,44 @@ PyTokenizer_RestoreEncoding(struct tok_state* tok, int len, int *offset) } #endif -/* Get -*- encoding -*- from a Python file +/* Get -*- encoding -*- from a Python file. PyTokenizer_FindEncoding returns NULL when it can't find the encoding in the first or second line of the file (in which case the encoding should be assumed to be PyUnicode_GetDefaultEncoding()). - The char * returned was malloc'ed from PyMem_MALLOC() and thus must be freed - when no longer needed. + The char * returned is malloc'ed via PyMem_MALLOC() and thus must be freed + by the caller. */ char * -PyTokenizer_FindEncoding(FILE *fp) { +PyTokenizer_FindEncoding(int fd) +{ struct tok_state *tok; - char *p_start=NULL, *p_end=NULL, *encoding=NULL; + FILE *fp; + char *p_start =NULL , *p_end =NULL , *encoding = NULL; - if ((tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL)) == NULL) { - /* lseek() usage is on purpose; see note later in code. */ - lseek(fileno(fp), 0, 0); + fd = dup(fd); + if (fd < 0) { + return NULL; + } + fp = fdopen(fd, "r"); + if (fp == NULL) { + return NULL; + } + tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL); + if (tok == NULL) { + fclose(fp); return NULL; } - while(((tok->lineno < 2) && (tok->done == E_OK))) { + while (tok->lineno < 2 && tok->done == E_OK) { PyTokenizer_Get(tok, &p_start, &p_end); } - - /* lseek() must be used instead of fseek()/rewind() as those fail on - OS X 10.4 to properly seek back to the beginning when reading from - the file descriptor instead of the file pointer. */ - lseek(fileno(fp), 0, 0); - + fclose(fp); if (tok->encoding) { encoding = (char *)PyMem_MALLOC(strlen(tok->encoding) + 1); strcpy(encoding, tok->encoding); } PyTokenizer_Free(tok); - return encoding; } |