[Python-checkins] CVS: python/dist/src/Python ceval.c,2.189,2.190 compile.c,2.126,2.127 graminit.c,2.24,2.25 import.c,2.145,2.146
Thomas Wouters
python-dev@python.org
Thu, 17 Aug 2000 15:55:03 -0700
Update of /cvsroot/python/python/dist/src/Python
In directory slayer.i.sourceforge.net:/tmp/cvs-serv24798/Python
Modified Files:
ceval.c compile.c graminit.c import.c
Log Message:
Apply SF patch #101135, adding 'import module as m' and 'from module import
name as n'. By doing some twists and turns, "as" is not a reserved word.
There is a slight change in semantics for 'from module import name' (it will
now honour the 'global' keyword) but only in cases that are explicitly
undocumented.
Index: ceval.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/ceval.c,v
retrieving revision 2.189
retrieving revision 2.190
diff -C2 -r2.189 -r2.190
*** ceval.c 2000/08/11 22:15:52 2.189
--- ceval.c 2000/08/17 22:55:00 2.190
***************
*** 67,71 ****
PyObject *, PyObject *);
static PyObject *cmp_outcome(int, PyObject *, PyObject *);
! static int import_from(PyObject *, PyObject *, PyObject *);
static PyObject *build_class(PyObject *, PyObject *, PyObject *);
static int exec_statement(PyFrameObject *,
--- 67,72 ----
PyObject *, PyObject *);
static PyObject *cmp_outcome(int, PyObject *, PyObject *);
! static PyObject *import_from(PyObject *, PyObject *);
! static int import_all_from(PyObject *, PyObject *);
static PyObject *build_class(PyObject *, PyObject *, PyObject *);
static int exec_statement(PyFrameObject *,
***************
*** 1415,1421 ****
break;
! case IMPORT_FROM:
! w = GETNAMEV(oparg);
! v = TOP();
PyFrame_FastToLocals(f);
if ((x = f->f_locals) == NULL) {
--- 1416,1421 ----
break;
! case IMPORT_STAR:
! v = POP();
PyFrame_FastToLocals(f);
if ((x = f->f_locals) == NULL) {
***************
*** 1424,1432 ****
break;
}
! err = import_from(x, v, w);
PyFrame_LocalsToFast(f, 0);
if (err == 0) continue;
break;
case JUMP_FORWARD:
JUMPBY(oparg);
--- 1424,1441 ----
break;
}
! err = import_all_from(x, v);
PyFrame_LocalsToFast(f, 0);
+ Py_DECREF(v);
if (err == 0) continue;
break;
+ case IMPORT_FROM:
+ w = GETNAMEV(oparg);
+ v = TOP();
+ x = import_from(v, w);
+ PUSH(x);
+ if (x != NULL) continue;
+ break;
+
case JUMP_FORWARD:
JUMPBY(oparg);
***************
*** 2648,2653 ****
}
! static int
! import_from(PyObject *locals, PyObject *v, PyObject *name)
{
PyObject *w, *x;
--- 2657,2662 ----
}
! static PyObject *
! import_from(PyObject *v, PyObject *name)
{
PyObject *w, *x;
***************
*** 2655,2688 ****
PyErr_SetString(PyExc_TypeError,
"import-from requires module object");
return -1;
}
! w = PyModule_GetDict(v);
! if (PyString_AsString(name)[0] == '*') {
! int pos, err;
! PyObject *name, *value;
! pos = 0;
! while (PyDict_Next(w, &pos, &name, &value)) {
! if (!PyString_Check(name) ||
! PyString_AsString(name)[0] == '_')
continue;
! Py_INCREF(value);
! err = PyDict_SetItem(locals, name, value);
! Py_DECREF(value);
! if (err != 0)
! return -1;
! }
! return 0;
! }
! else {
! x = PyDict_GetItem(w, name);
! if (x == NULL) {
! PyErr_Format(PyExc_ImportError,
! "cannot import name %.230s",
! PyString_AsString(name));
return -1;
- }
- else
- return PyDict_SetItem(locals, name, x);
}
}
--- 2664,2705 ----
PyErr_SetString(PyExc_TypeError,
"import-from requires module object");
+ return NULL;
+ }
+ w = PyModule_GetDict(v); /* TDB: can this not fail ? */
+ x = PyDict_GetItem(w, name);
+ if (x == NULL) {
+ PyErr_Format(PyExc_ImportError,
+ "cannot import name %.230s",
+ PyString_AsString(name));
+ } else
+ Py_INCREF(x);
+ return x;
+ }
+
+ static int
+ import_all_from(PyObject *locals, PyObject *v)
+ {
+ int pos = 0, err;
+ PyObject *name, *value;
+ PyObject *w;
+
+ if (!PyModule_Check(v)) {
+ PyErr_SetString(PyExc_TypeError,
+ "import-from requires module object");
return -1;
}
! w = PyModule_GetDict(v); /* TBD: can this not fail ? */
!
! while (PyDict_Next(w, &pos, &name, &value)) {
! if (!PyString_Check(name) ||
! PyString_AsString(name)[0] == '_')
continue;
! Py_INCREF(value);
! err = PyDict_SetItem(locals, name, value);
! Py_DECREF(value);
! if (err != 0)
return -1;
}
+ return 0;
}
***************
*** 2826,2830 ****
opcode = (*next_instr++);
! if (opcode != IMPORT_FROM) {
Py_INCREF(Py_None);
return Py_None;
--- 2843,2847 ----
opcode = (*next_instr++);
! if (opcode != IMPORT_FROM && opcode != IMPORT_STAR) {
Py_INCREF(Py_None);
return Py_None;
***************
*** 2834,2849 ****
if (list == NULL)
return NULL;
!
! do {
! oparg = (next_instr[1]<<8) + next_instr[0];
! next_instr += 2;
! name = Getnamev(f, oparg);
! if (PyList_Append(list, name) < 0) {
Py_DECREF(list);
! break;
! }
! opcode = (*next_instr++);
! } while (opcode == IMPORT_FROM);
!
return list;
}
--- 2851,2876 ----
if (list == NULL)
return NULL;
!
! if (opcode == IMPORT_STAR) {
! name = PyString_FromString("*");
! if (!name)
Py_DECREF(list);
! else {
! if (PyList_Append(list, name) < 0)
! Py_DECREF(list);
! Py_DECREF(name);
! }
! } else {
! do {
! oparg = (next_instr[1]<<8) + next_instr[0];
! next_instr += 2;
! name = Getnamev(f, oparg);
! if (PyList_Append(list, name) < 0) {
! Py_DECREF(list);
! break;
! }
! opcode = (*next_instr++);
! } while (opcode == IMPORT_FROM);
! }
return list;
}
Index: compile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v
retrieving revision 2.126
retrieving revision 2.127
diff -C2 -r2.126 -r2.127
*** compile.c 2000/08/15 16:41:26 2.126
--- compile.c 2000/08/17 22:55:00 2.127
***************
*** 2097,2100 ****
--- 2097,2116 ----
static void
+ com_from_import(struct compiling *c, node *n)
+ {
+ com_addopname(c, IMPORT_FROM, CHILD(n, 0));
+ com_push(c, 1);
+ if (NCH(n) > 1) {
+ if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
+ com_error(c, PyExc_SyntaxError, "invalid syntax");
+ return;
+ }
+ com_addopname(c, STORE_NAME, CHILD(n, 2));
+ } else
+ com_addopname(c, STORE_NAME, CHILD(n, 0));
+ com_pop(c, 1);
+ }
+
+ static void
com_import_stmt(struct compiling *c, node *n)
{
***************
*** 2108,2114 ****
com_addopname(c, IMPORT_NAME, CHILD(n, 1));
com_push(c, 1);
! for (i = 3; i < NCH(n); i += 2)
! com_addopname(c, IMPORT_FROM, CHILD(n, i));
! com_addbyte(c, POP_TOP);
com_pop(c, 1);
}
--- 2124,2134 ----
com_addopname(c, IMPORT_NAME, CHILD(n, 1));
com_push(c, 1);
! if (TYPE(CHILD(n, 3)) == STAR)
! com_addbyte(c, IMPORT_STAR);
! else {
! for (i = 3; i < NCH(n); i += 2)
! com_from_import(c, CHILD(n, i));
! com_addbyte(c, POP_TOP);
! }
com_pop(c, 1);
}
***************
*** 2116,2123 ****
/* 'import' ... */
for (i = 1; i < NCH(n); i += 2) {
! REQ(CHILD(n, i), dotted_name);
! com_addopname(c, IMPORT_NAME, CHILD(n, i));
com_push(c, 1);
! com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
com_pop(c, 1);
}
--- 2136,2153 ----
/* 'import' ... */
for (i = 1; i < NCH(n); i += 2) {
! node *subn = CHILD(n, i);
! REQ(subn, dotted_as_name);
! com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
com_push(c, 1);
! if (NCH(subn) > 1) {
! if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
! com_error(c, PyExc_SyntaxError,
! "invalid syntax");
! return;
! }
! com_addopname(c, STORE_NAME, CHILD(subn, 2));
! } else
! com_addopname(c, STORE_NAME,
! CHILD(CHILD(subn, 0),0));
com_pop(c, 1);
}
***************
*** 3296,3299 ****
--- 3326,3330 ----
com_addlocal_o(c, GETNAMEOBJ(oparg));
break;
+ case IMPORT_STAR:
case EXEC_STMT:
c->c_flags &= ~CO_OPTIMIZED;
***************
*** 3302,3305 ****
--- 3333,3337 ----
}
+ /* TBD: Is this still necessary ? */
if (PyDict_GetItemString(c->c_locals, "*") != NULL)
c->c_flags &= ~CO_OPTIMIZED;
Index: graminit.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/graminit.c,v
retrieving revision 2.24
retrieving revision 2.25
diff -C2 -r2.24 -r2.25
*** graminit.c 2000/08/12 18:09:51 2.24
--- graminit.c 2000/08/17 22:55:00 2.25
***************
*** 364,368 ****
};
static arc arcs_20_2[1] = {
! {50, 4},
};
static arc arcs_20_3[2] = {
--- 364,368 ----
};
static arc arcs_20_2[1] = {
! {52, 4},
};
[...2357 lines suppressed...]
{25, 0},
{2, 0},
{3, 0},
! {316, 0},
{1, "lambda"},
! {314, 0},
{307, 0},
+ {308, 0},
+ {309, 0},
{1, "class"},
{315, 0},
! {317, 0},
! {318, 0},
};
grammar _PyParser_Grammar = {
! 63,
dfas,
! {130, labels},
256
};
Index: import.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/import.c,v
retrieving revision 2.145
retrieving revision 2.146
diff -C2 -r2.145 -r2.146
*** import.c 2000/08/11 22:15:52 2.145
--- import.c 2000/08/17 22:55:00 2.146
***************
*** 67,71 ****
added to the .pyc file header? */
/* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
! #define MAGIC (50811 | ((long)'\r'<<16) | ((long)'\n'<<24))
/* Magic word as global; note that _PyImport_Init() can change the
--- 67,71 ----
added to the .pyc file header? */
/* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
! #define MAGIC (50815 | ((long)'\r'<<16) | ((long)'\n'<<24))
/* Magic word as global; note that _PyImport_Init() can change the