diff options
author | Guido van Rossum <guido@python.org> | 2007-10-19 23:16:50 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-10-19 23:16:50 (GMT) |
commit | ce3a72aec6eaa0293c397c8d0407f7afe0072b2f (patch) | |
tree | 646cf27667087a48b908f330759fb94271b43f60 /Parser | |
parent | 75a902db7859a4751743e98530c5d96a672641be (diff) | |
download | cpython-ce3a72aec6eaa0293c397c8d0407f7afe0072b2f.zip cpython-ce3a72aec6eaa0293c397c8d0407f7afe0072b2f.tar.gz cpython-ce3a72aec6eaa0293c397c8d0407f7afe0072b2f.tar.bz2 |
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.
Diffstat (limited to 'Parser')
-rw-r--r-- | Parser/tokenizer.c | 23 | ||||
-rw-r--r-- | Parser/tokenizer.h | 1 |
2 files changed, 23 insertions, 1 deletions
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 8f67e0e..0ccd02b 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -1601,7 +1601,28 @@ PyTokenizer_RestoreEncoding(struct tok_state* tok, int len, int *offset) } #endif - +/* 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 this case the encoding is + PyUnicode_GetDefaultEncoding(). +*/ +char * +PyTokenizer_FindEncoding(FILE *fp) { + struct tok_state *tok; + char *p_start=NULL, *p_end=NULL; + + if ((tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL)) == NULL) { + rewind(fp); + return NULL; + } + while(((tok->lineno <= 2) && (tok->done == E_OK))) { + PyTokenizer_Get(tok, &p_start, &p_end); + } + + rewind(fp); + return tok->encoding; +} #ifdef Py_DEBUG diff --git a/Parser/tokenizer.h b/Parser/tokenizer.h index 72982bd..a66d78e 100644 --- a/Parser/tokenizer.h +++ b/Parser/tokenizer.h @@ -67,6 +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); #ifdef __cplusplus } |