diff options
Diffstat (limited to 'Parser')
-rw-r--r-- | Parser/tokenizer.c | 36 | ||||
-rw-r--r-- | Parser/tokenizer.h | 2 |
2 files changed, 21 insertions, 17 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; } diff --git a/Parser/tokenizer.h b/Parser/tokenizer.h index a66d78e..c45dea1 100644 --- a/Parser/tokenizer.h +++ b/Parser/tokenizer.h @@ -67,7 +67,7 @@ extern void PyTokenizer_Free(struct tok_state *); extern int PyTokenizer_Get(struct tok_state *, char **, char **); extern char * PyTokenizer_RestoreEncoding(struct tok_state* tok, int len, int *offset); -extern char * PyTokenizer_FindEncoding(FILE *fp); +extern char * PyTokenizer_FindEncoding(int); #ifdef __cplusplus } |