[Python-checkins] python/dist/src/Python compile.c, 2.325,
2.326 future.c, 2.13, 2.14 graminit.c, 2.37, 2.38
anthonybaxter at users.sourceforge.net
anthonybaxter at users.sourceforge.net
Tue Aug 31 12:07:17 CEST 2004
Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21410/Python
Modified Files:
compile.c future.c graminit.c
Log Message:
SF patch #1007189, multi-line imports, for instance:
"from blah import (foo, bar
baz, bongo)"
Index: compile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v
retrieving revision 2.325
retrieving revision 2.326
diff -u -d -r2.325 -r2.326
--- compile.c 25 Aug 2004 17:19:38 -0000 2.325
+++ compile.c 31 Aug 2004 10:07:13 -0000 2.326
@@ -3490,42 +3490,53 @@
static void
com_import_stmt(struct compiling *c, node *n)
{
+ node *nn;
int i;
REQ(n, import_stmt);
- /* 'import' dotted_name (',' dotted_name)* |
- 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
- if (STR(CHILD(n, 0))[0] == 'f') {
+ n = CHILD(n, 0);
+ /* import_stmt: import_name | import_from */
+ if (TYPE(n) == import_from) {
+ /* 'from' dotted_name 'import' ('*' |
+ '(' import_as_names ')' | import_as_names) */
PyObject *tup;
- /* 'from' dotted_name 'import' ... */
REQ(CHILD(n, 1), dotted_name);
-
- if (TYPE(CHILD(n, 3)) == STAR) {
+ nn = CHILD(n, 3 + (TYPE(CHILD(n, 3)) == LPAR));
+ if (TYPE(nn) == STAR)
tup = Py_BuildValue("(s)", "*");
- } else {
- tup = PyTuple_New((NCH(n) - 2)/2);
- for (i = 3; i < NCH(n); i += 2) {
- PyTuple_SET_ITEM(tup, (i-3)/2,
- PyString_FromString(STR(
- CHILD(CHILD(n, i), 0))));
+ else {
+ if (TYPE(CHILD(nn, NCH(nn) - 1)) == COMMA &&
+ TYPE(CHILD(n, 3)) != LPAR) {
+ com_error(c, PyExc_SyntaxError,
+ "trailing comma not allowed "
+ "without surrounding parentheses");
+ return;
}
+ REQ(nn, import_as_names);
+ tup = PyTuple_New((NCH(nn) + 1) / 2);
+ for (i = 0; i < NCH(nn); i += 2)
+ PyTuple_SET_ITEM(tup, i / 2,
+ PyString_FromString(STR(
+ CHILD(CHILD(nn, i), 0))));
}
com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
Py_DECREF(tup);
com_push(c, 1);
com_addopname(c, IMPORT_NAME, CHILD(n, 1));
- if (TYPE(CHILD(n, 3)) == STAR)
+ if (TYPE(nn) == STAR)
com_addbyte(c, IMPORT_STAR);
else {
- for (i = 3; i < NCH(n); i += 2)
- com_from_import(c, CHILD(n, i));
+ for (i = 0; i < NCH(nn); i += 2)
+ com_from_import(c, CHILD(nn, i));
com_addbyte(c, POP_TOP);
}
com_pop(c, 1);
}
else {
- /* 'import' ... */
- for (i = 1; i < NCH(n); i += 2) {
- node *subn = CHILD(n, i);
+ /* 'import' dotted_as_names */
+ nn = CHILD(n, 1);
+ REQ(nn, dotted_as_names);
+ for (i = 0; i < NCH(nn); i += 2) {
+ node *subn = CHILD(nn, i);
REQ(subn, dotted_as_name);
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
com_push(c, 1);
@@ -6291,14 +6302,15 @@
static void
symtable_import(struct symtable *st, node *n)
{
+ node *nn;
int i;
- /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
- | 'from' dotted_name 'import'
- ('*' | import_as_name (',' import_as_name)*)
- import_as_name: NAME [NAME NAME]
- */
- if (STR(CHILD(n, 0))[0] == 'f') { /* from */
+ /* import_stmt: import_name | import_from */
+ n = CHILD(n, 0);
+ if (TYPE(n) == import_from) {
+ /* import_from: 'from' dotted_name 'import' ('*' |
+ | '(' import_as_names ')' | import_as_names) */
node *dotname = CHILD(n, 1);
+ REQ(dotname, dotted_name);
if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
/* check for bogus imports */
if (n->n_lineno >= st->st_future->ff_last_lineno) {
@@ -6308,7 +6320,8 @@
return;
}
}
- if (TYPE(CHILD(n, 3)) == STAR) {
+ nn = CHILD(n, 3 + (TYPE(CHILD(n, 3)) == LPAR));
+ if (TYPE(nn) == STAR) {
if (st->st_cur->ste_type != TYPE_MODULE) {
if (symtable_warn(st,
"import * only allowed at module level") < 0)
@@ -6317,8 +6330,9 @@
st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
st->st_cur->ste_opt_lineno = n->n_lineno;
} else {
- for (i = 3; i < NCH(n); i += 2) {
- node *c = CHILD(n, i);
+ REQ(nn, import_as_names);
+ for (i = 0; i < NCH(nn); i += 2) {
+ node *c = CHILD(nn, i);
if (NCH(c) > 1) /* import as */
symtable_assign(st, CHILD(c, 2),
DEF_IMPORT);
@@ -6327,10 +6341,12 @@
DEF_IMPORT);
}
}
- } else {
- for (i = 1; i < NCH(n); i += 2) {
- symtable_assign(st, CHILD(n, i), DEF_IMPORT);
- }
+ } else {
+ /* 'import' dotted_as_names */
+ nn = CHILD(n, 1);
+ REQ(nn, dotted_as_names);
+ for (i = 0; i < NCH(nn); i += 2)
+ symtable_assign(st, CHILD(nn, i), DEF_IMPORT);
}
}
Index: future.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/future.c,v
retrieving revision 2.13
retrieving revision 2.14
diff -u -d -r2.13 -r2.14
--- future.c 11 Dec 2002 14:04:59 -0000 2.13
+++ future.c 31 Aug 2004 10:07:13 -0000 2.14
@@ -18,18 +18,18 @@
{
int i;
char *feature;
- node *ch;
-
- REQ(n, import_stmt); /* must by from __future__ import ... */
+ node *ch, *nn;
- for (i = 3; i < NCH(n); i += 2) {
- ch = CHILD(n, i);
- if (TYPE(ch) == STAR) {
- PyErr_SetString(PyExc_SyntaxError,
- FUTURE_IMPORT_STAR);
- PyErr_SyntaxLocation(filename, ch->n_lineno);
- return -1;
- }
+ REQ(n, import_from);
+ nn = CHILD(n, 3 + (TYPE(CHILD(n, 3)) == LPAR));
+ if (TYPE(nn) == STAR) {
+ PyErr_SetString(PyExc_SyntaxError, FUTURE_IMPORT_STAR);
+ PyErr_SyntaxLocation(filename, nn->n_lineno);
+ return -1;
+ }
+ REQ(nn, import_as_names);
+ for (i = 0; i < NCH(nn); i += 2) {
+ ch = CHILD(nn, i);
REQ(ch, import_as_name);
feature = STR(CHILD(ch, 0));
if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
@@ -188,7 +188,8 @@
case import_stmt: {
node *name;
- if (STR(CHILD(n, 0))[0] != 'f') { /* from */
+ n = CHILD(n, 0);
+ if (TYPE(n) != import_from) {
ff->ff_last_lineno = n->n_lineno;
return 0;
}
Index: graminit.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/graminit.c,v
retrieving revision 2.37
retrieving revision 2.38
diff -u -d -r2.37 -r2.38
--- graminit.c 17 Aug 2004 17:29:16 -0000 2.37
+++ graminit.c 31 Aug 2004 10:07:13 -0000 2.38
@@ -488,1135 +488,1177 @@
};
static arc arcs_24_0[2] = {
{70, 1},
- {72, 2},
+ {71, 1},
};
static arc arcs_24_1[1] = {
- {71, 3},
+ {0, 1},
};
-static arc arcs_24_2[1] = {
[...2203 lines suppressed...]
{321, 0},
- {322, 0},
- {324, 0},
+ {1, "class"},
{325, 0},
- {327, 0},
+ {326, 0},
+ {328, 0},
{329, 0},
+ {331, 0},
+ {333, 0},
};
grammar _PyParser_Grammar = {
- 74,
+ 78,
dfas,
- {156, labels},
+ {160, labels},
256
};
More information about the Python-checkins
mailing list