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

Skip to content

Commit b09f7ed

Browse files
committed
Preliminary support for "from __future__ import generators" to enable
the yield statement. I figure we have to have this in before I can release 2.2a1 on Wednesday. Note: test_generators is currently broken, I'm counting on Tim to fix this.
1 parent 045ca7a commit b09f7ed

8 files changed

Lines changed: 47 additions & 3 deletions

File tree

Include/compile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ DL_IMPORT(PyCodeObject *) PyNode_CompileFlags(struct _node *, char *,
6565
#define NESTED_SCOPES_DEFAULT 1
6666
#define FUTURE_NESTED_SCOPES "nested_scopes"
6767

68+
#define GENERATORS_DEFAULT 0
69+
#define FUTURE_GENERATORS "generators"
70+
6871
/* for internal use only */
6972
#define _PyCode_GETCODEPTR(co, pp) \
7073
((*(co)->co_code->ob_type->tp_as_buffer->bf_getreadbuffer) \

Lib/__future__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ def __repr__(self):
6767
`self.getMandatoryRelease()` + ")"
6868

6969
nested_scopes = _Feature((2, 1, 0, "beta", 1), (2, 2, 0, "alpha", 0))
70+
generators = _Feature((2, 2, 0, "alpha", 1), (2, 3, 0, "final", 0))

Lib/inspect.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
# This module is in the public domain. No warranties.
2626

27+
from __future__ import generators
28+
2729
__author__ = 'Ka-Ping Yee <[email protected]>'
2830
__date__ = '1 Jan 2001'
2931

Lib/tokenize.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
function to which the 5 fields described above are passed as 5 arguments,
2323
each time a new token is found."""
2424

25+
from __future__ import generators
26+
2527
__author__ = 'Ka-Ping Yee <[email protected]>'
2628
__credits__ = \
2729
'GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip Montanaro'

Lib/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
Types that are part of optional modules (e.g. array) are not listed.
44
"""
5+
from __future__ import generators
56

67
import sys
78

Parser/parser.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ PyParser_New(grammar *g, int start)
7979
if (ps == NULL)
8080
return NULL;
8181
ps->p_grammar = g;
82+
ps->p_generators = 0;
8283
ps->p_tree = PyNode_New(start);
8384
if (ps->p_tree == NULL) {
8485
PyMem_DEL(ps);
@@ -131,8 +132,9 @@ push(register stack *s, int type, dfa *d, int newstate, int lineno)
131132
/* PARSER PROPER */
132133

133134
static int
134-
classify(grammar *g, int type, char *str)
135+
classify(parser_state *ps, int type, char *str)
135136
{
137+
grammar *g = ps->p_grammar;
136138
register int n = g->g_ll.ll_nlabels;
137139

138140
if (type == NAME) {
@@ -143,6 +145,10 @@ classify(grammar *g, int type, char *str)
143145
if (l->lb_type == NAME && l->lb_str != NULL &&
144146
l->lb_str[0] == s[0] &&
145147
strcmp(l->lb_str, s) == 0) {
148+
if (!ps->p_generators &&
149+
s[0] == 'y' &&
150+
strcmp(s, "yield") == 0)
151+
break; /* not a keyword */
146152
D(printf("It's a keyword\n"));
147153
return n - i;
148154
}
@@ -164,6 +170,22 @@ classify(grammar *g, int type, char *str)
164170
return -1;
165171
}
166172

173+
static void
174+
future_hack(parser_state *ps)
175+
{
176+
node *n = ps->p_stack.s_top->s_parent;
177+
node *ch;
178+
179+
if (strcmp(STR(CHILD(n, 0)), "from") != 0)
180+
return;
181+
ch = CHILD(n, 1);
182+
if (strcmp(STR(CHILD(ch, 0)), "__future__") != 0)
183+
return;
184+
ch = CHILD(n, 3);
185+
if (NCH(ch) == 1 && strcmp(STR(CHILD(ch, 0)), "generators") == 0)
186+
ps->p_generators = 1;
187+
}
188+
167189
int
168190
PyParser_AddToken(register parser_state *ps, register int type, char *str,
169191
int lineno, int *expected_ret)
@@ -174,7 +196,7 @@ PyParser_AddToken(register parser_state *ps, register int type, char *str,
174196
D(printf("Token %s/'%s' ... ", _PyParser_TokenNames[type], str));
175197

176198
/* Find out which label this token is */
177-
ilabel = classify(ps->p_grammar, type, str);
199+
ilabel = classify(ps, type, str);
178200
if (ilabel < 0)
179201
return E_SYNTAX;
180202

@@ -217,7 +239,14 @@ PyParser_AddToken(register parser_state *ps, register int type, char *str,
217239
while (s = &d->d_state
218240
[ps->p_stack.s_top->s_state],
219241
s->s_accept && s->s_narcs == 1) {
220-
D(printf(" Direct pop.\n"));
242+
D(printf(" DFA '%s', state %d: "
243+
"Direct pop.\n",
244+
d->d_name,
245+
ps->p_stack.s_top->s_state));
246+
if (d->d_name[0] == 'i' &&
247+
strcmp(d->d_name,
248+
"import_stmt") == 0)
249+
future_hack(ps);
221250
s_pop(&ps->p_stack);
222251
if (s_empty(&ps->p_stack)) {
223252
D(printf(" ACCEPT.\n"));
@@ -230,6 +259,9 @@ PyParser_AddToken(register parser_state *ps, register int type, char *str,
230259
}
231260

232261
if (s->s_accept) {
262+
if (d->d_name[0] == 'i' &&
263+
strcmp(d->d_name, "import_stmt") == 0)
264+
future_hack(ps);
233265
/* Pop this dfa and try again */
234266
s_pop(&ps->p_stack);
235267
D(printf(" Pop ...\n"));

Parser/parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ typedef struct {
2525
stack p_stack; /* Stack of parser states */
2626
grammar *p_grammar; /* Grammar to use */
2727
node *p_tree; /* Top of parse tree */
28+
int p_generators; /* 1 if yield is a keyword */
2829
} parser_state;
2930

3031
parser_state *PyParser_New(grammar *g, int start);

Python/future.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ future_check_features(PyFutureFeatures *ff, node *n, char *filename)
3131
feature = STR(CHILD(ch, 0));
3232
if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
3333
ff->ff_nested_scopes = 1;
34+
} else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
35+
/* OK; this is processed by the parser */
3436
} else if (strcmp(feature, "braces") == 0) {
3537
PyErr_SetString(PyExc_SyntaxError,
3638
"not a chance");

0 commit comments

Comments
 (0)