mirror of
https://github.com/python/cpython
synced 2026-09-29 12:10:30 +03:00
ignore the coding cookie in compile(), exec(), and eval() if the source is a string #4626
This commit is contained in:
parent
0663a1ed79
commit
f5b52246ed
10 changed files with 63 additions and 14 deletions
|
|
@ -49,7 +49,11 @@ PyParser_ParseStringFlagsFilenameEx(const char *s, const char *filename,
|
|||
|
||||
initerr(err_ret, filename);
|
||||
|
||||
if ((tok = PyTokenizer_FromString(s)) == NULL) {
|
||||
if (*flags & PyPARSE_IGNORE_COOKIE)
|
||||
tok = PyTokenizer_FromUTF8(s);
|
||||
else
|
||||
tok = PyTokenizer_FromString(s);
|
||||
if (tok == NULL) {
|
||||
err_ret->error = PyErr_Occurred() ? E_DECODE : E_NOMEM;
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -715,6 +715,28 @@ PyTokenizer_FromString(const char *str)
|
|||
return tok;
|
||||
}
|
||||
|
||||
struct tok_state *
|
||||
PyTokenizer_FromUTF8(const char *str)
|
||||
{
|
||||
struct tok_state *tok = tok_new();
|
||||
if (tok == NULL)
|
||||
return NULL;
|
||||
tok->decoding_state = STATE_RAW;
|
||||
tok->read_coding_spec = 1;
|
||||
tok->enc = NULL;
|
||||
tok->str = str;
|
||||
tok->encoding = (char *)PyMem_MALLOC(6);
|
||||
if (!tok->encoding) {
|
||||
PyTokenizer_Free(tok);
|
||||
return NULL;
|
||||
}
|
||||
strcpy(tok->encoding, "utf-8");
|
||||
|
||||
/* XXX: constify members. */
|
||||
tok->buf = tok->cur = tok->end = tok->inp = (char*)str;
|
||||
return tok;
|
||||
}
|
||||
|
||||
|
||||
/* Set up tokenizer for file */
|
||||
|
||||
|
|
|
|||
|
|
@ -61,6 +61,7 @@ struct tok_state {
|
|||
};
|
||||
|
||||
extern struct tok_state *PyTokenizer_FromString(const char *);
|
||||
extern struct tok_state *PyTokenizer_FromUTF8(const char *);
|
||||
extern struct tok_state *PyTokenizer_FromFile(FILE *, char*,
|
||||
char *, char *);
|
||||
extern void PyTokenizer_Free(struct tok_state *);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue