Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit da62ecc

Browse files
committed
Add a really stupid warning about 'yield' used as an identifier.
This is really stupid because it cannot be suppressed or altered using the warning framework; that's because the warning framework is built on Python interpreter internals, and the parser generator doesn't have access to any of those (you cannot use anything of type PyObject * in the parser). But it's better than nothing, and implementing a proper check for this appears to require modifying compile.c in a dozen places, for which I don't have the stamina today. I promise we'll do better in 2.2a2. At least it tells you the filename and line number (unlike the first hack I considered :-).
1 parent 16649a8 commit da62ecc

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

Parser/parsetok.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ PyParser_ParseFileFlags(FILE *fp, char *filename, grammar *g, int start,
9292
/* Parse input coming from the given tokenizer structure.
9393
Return error code. */
9494

95+
static char yield_msg[] =
96+
"%s:%d: Warning: 'yield' will become a reserved keyword in the future\n";
97+
9598
static node *
9699
parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
97100
int flags)
@@ -135,6 +138,15 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
135138
if (len > 0)
136139
strncpy(str, a, len);
137140
str[len] = '\0';
141+
142+
/* Warn about yield as NAME */
143+
if (type == NAME && !ps->p_generators &&
144+
len == 5 && str[0] == 'y' && strcmp(str, "yield") == 0)
145+
PySys_WriteStderr(yield_msg,
146+
err_ret->filename==NULL ?
147+
"<string>" : err_ret->filename,
148+
tok->lineno);
149+
138150
if ((err_ret->error =
139151
PyParser_AddToken(ps, (int)type, str, tok->lineno,
140152
&(err_ret->expected))) != E_OK) {

0 commit comments

Comments
 (0)