comparison Python/Python-ast.c @ 36712:5c36c222e7ca legacy-trunk

SF patch #1438387, PEP 328: relative and absolute imports. - IMPORT_NAME takes an extra argument from the stack: the relativeness of the import. Only passed to __import__ when it's not -1. - __import__() takes an optional 5th argument for the same thing; it __defaults to -1 (old semantics: try relative, then absolute) - 'from . import name' imports name (be it module or regular attribute) from the current module's *package*. Likewise, 'from .module import name' will import name from a sibling to the current module. - Importing from outside a package is not allowed; 'from . import sys' in a toplevel module will not work, nor will 'from .. import sys' in a (single-level) package. - 'from __future__ import absolute_import' will turn on the new semantics for import and from-import: imports will be absolute, except for from-import with dots. Includes tests for regular imports and importhooks, parser changes and a NEWS item, but no compiler-package changes or documentation changes.
author Thomas Wouters <thomas@python.org>
date Tue, 28 Feb 2006 16:09:29 +0000
parents cd847e726837
children bbf16fd024df
comparison
equal deleted inserted replaced
36711:f15302d8b2de 36712:5c36c222e7ca
118 }; 118 };
119 PyTypeObject *ImportFrom_type; 119 PyTypeObject *ImportFrom_type;
120 char *ImportFrom_fields[]={ 120 char *ImportFrom_fields[]={
121 "module", 121 "module",
122 "names", 122 "names",
123 "level",
123 }; 124 };
124 PyTypeObject *Exec_type; 125 PyTypeObject *Exec_type;
125 char *Exec_fields[]={ 126 char *Exec_fields[]={
126 "body", 127 "body",
127 "globals", 128 "globals",
483 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2); 484 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
484 if (!Assert_type) return 0; 485 if (!Assert_type) return 0;
485 Import_type = make_type("Import", stmt_type, Import_fields, 1); 486 Import_type = make_type("Import", stmt_type, Import_fields, 1);
486 if (!Import_type) return 0; 487 if (!Import_type) return 0;
487 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 488 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
488 2); 489 3);
489 if (!ImportFrom_type) return 0; 490 if (!ImportFrom_type) return 0;
490 Exec_type = make_type("Exec", stmt_type, Exec_fields, 3); 491 Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
491 if (!Exec_type) return 0; 492 if (!Exec_type) return 0;
492 Global_type = make_type("Global", stmt_type, Global_fields, 1); 493 Global_type = make_type("Global", stmt_type, Global_fields, 1);
493 if (!Global_type) return 0; 494 if (!Global_type) return 0;
1116 p->lineno = lineno; 1117 p->lineno = lineno;
1117 return p; 1118 return p;
1118 } 1119 }
1119 1120
1120 stmt_ty 1121 stmt_ty
1121 ImportFrom(identifier module, asdl_seq * names, int lineno, PyArena *arena) 1122 ImportFrom(identifier module, asdl_seq * names, int level, int lineno, PyArena
1123 *arena)
1122 { 1124 {
1123 stmt_ty p; 1125 stmt_ty p;
1124 if (!module) { 1126 if (!module) {
1125 PyErr_SetString(PyExc_ValueError, 1127 PyErr_SetString(PyExc_ValueError,
1126 "field module is required for ImportFrom"); 1128 "field module is required for ImportFrom");
1132 return NULL; 1134 return NULL;
1133 } 1135 }
1134 p->kind = ImportFrom_kind; 1136 p->kind = ImportFrom_kind;
1135 p->v.ImportFrom.module = module; 1137 p->v.ImportFrom.module = module;
1136 p->v.ImportFrom.names = names; 1138 p->v.ImportFrom.names = names;
1139 p->v.ImportFrom.level = level;
1137 p->lineno = lineno; 1140 p->lineno = lineno;
1138 return p; 1141 return p;
1139 } 1142 }
1140 1143
1141 stmt_ty 1144 stmt_ty
2198 goto failed; 2201 goto failed;
2199 Py_DECREF(value); 2202 Py_DECREF(value);
2200 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias); 2203 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2201 if (!value) goto failed; 2204 if (!value) goto failed;
2202 if (PyObject_SetAttrString(result, "names", value) == -1) 2205 if (PyObject_SetAttrString(result, "names", value) == -1)
2206 goto failed;
2207 Py_DECREF(value);
2208 value = ast2obj_int(o->v.ImportFrom.level);
2209 if (!value) goto failed;
2210 if (PyObject_SetAttrString(result, "level", value) == -1)
2203 goto failed; 2211 goto failed;
2204 Py_DECREF(value); 2212 Py_DECREF(value);
2205 break; 2213 break;
2206 case Exec_kind: 2214 case Exec_kind:
2207 result = PyType_GenericNew(Exec_type, NULL, NULL); 2215 result = PyType_GenericNew(Exec_type, NULL, NULL);