Index: Python/codecs.c =================================================================== --- Python/codecs.c (revision 42606) +++ Python/codecs.c (working copy) @@ -831,7 +831,7 @@ interp->codec_error_registry == NULL) Py_FatalError("can't initialize codec registry"); - mod = PyImport_ImportModuleEx("encodings", NULL, NULL, NULL); + mod = PyImport_ImportModuleLvl("encodings", NULL, NULL, NULL, 0); if (mod == NULL) { if (PyErr_ExceptionMatches(PyExc_ImportError)) { /* Ignore ImportErrors... this is done so that Index: Python/ceval.c =================================================================== --- Python/ceval.c (revision 42606) +++ Python/ceval.c (working copy) @@ -2023,13 +2023,24 @@ "__import__ not found"); break; } + v = POP(); u = TOP(); - w = PyTuple_Pack(4, - w, - f->f_globals, - f->f_locals == NULL ? - Py_None : f->f_locals, - u); + if (PyInt_AsLong(u) != -1 || PyErr_Occurred()) + w = PyTuple_Pack(5, + w, + f->f_globals, + f->f_locals == NULL ? + Py_None : f->f_locals, + v, + u); + else + w = PyTuple_Pack(4, + w, + f->f_globals, + f->f_locals == NULL ? + Py_None : f->f_locals, + v); + Py_DECREF(v); Py_DECREF(u); if (w == NULL) { u = POP(); Index: Python/graminit.c =================================================================== --- Python/graminit.c (revision 42606) +++ Python/graminit.c (working copy) @@ -513,34 +513,45 @@ {74, 1}, }; static arc arcs_26_1[2] = { - {75, 1}, - {12, 2}, + {75, 2}, + {12, 3}, }; -static arc arcs_26_2[1] = { - {72, 3}, +static arc arcs_26_2[3] = { + {75, 4}, + {12, 3}, + {72, 5}, }; -static arc arcs_26_3[3] = { - {28, 4}, - {13, 5}, - {76, 4}, +static arc arcs_26_3[1] = { + {72, 5}, }; -static arc arcs_26_4[1] = { - {0, 4}, +static arc arcs_26_4[2] = { + {75, 4}, + {12, 3}, }; -static arc arcs_26_5[1] = { +static arc arcs_26_5[3] = { + {28, 6}, + {13, 7}, {76, 6}, }; static arc arcs_26_6[1] = { - {15, 4}, + {0, 6}, }; -static state states_26[7] = { +static arc arcs_26_7[1] = { + {76, 8}, +}; +static arc arcs_26_8[1] = { + {15, 6}, +}; +static state states_26[9] = { {1, arcs_26_0}, {2, arcs_26_1}, - {1, arcs_26_2}, - {3, arcs_26_3}, - {1, arcs_26_4}, - {1, arcs_26_5}, + {3, arcs_26_2}, + {1, arcs_26_3}, + {2, arcs_26_4}, + {3, arcs_26_5}, {1, arcs_26_6}, + {1, arcs_26_7}, + {1, arcs_26_8}, }; static arc arcs_27_0[1] = { {19, 1}, @@ -1783,7 +1794,7 @@ "\000\000\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000\000\000\000\000"}, {281, "import_name", 0, 3, states_25, "\000\000\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\000\000\000\000"}, - {282, "import_from", 0, 7, states_26, + {282, "import_from", 0, 9, states_26, "\000\000\000\000\000\000\000\000\000\004\000\000\000\000\000\000\000\000\000\000\000"}, {283, "import_as_name", 0, 4, states_27, "\000\000\010\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"}, Index: Python/ast.c =================================================================== --- Python/ast.c (revision 42606) +++ Python/ast.c (working copy) @@ -2145,9 +2145,8 @@ /* import_stmt: import_name | import_from import_name: 'import' dotted_as_names - import_from: 'from' dotted_name 'import' ('*' | - '(' import_as_names ')' | - import_as_names) + import_from: 'from' ('.'* dotted_name | '.') 'import' + ('*' | '(' import_as_names ')' | import_as_names) */ int i; asdl_seq *aliases; @@ -2171,24 +2170,41 @@ else if (TYPE(n) == import_from) { int n_children; int lineno = LINENO(n); - alias_ty mod = alias_for_import_name(c, CHILD(n, 1)); - if (!mod) - return NULL; - - switch (TYPE(CHILD(n, 3))) { + int idx, ndots = 0; + alias_ty mod = NULL; + identifier modname; + + /* Count the number of dots (for relative imports) and check for the + optional module name */ + for (idx = 1; idx < NCH(n); idx++) { + if (TYPE(CHILD(n, idx)) == dotted_name) { + mod = alias_for_import_name(c, CHILD(n, idx)); + idx++; + break; + } else if (TYPE(CHILD(n, idx)) != DOT) { + break; + } + ndots++; + } + idx++; /* skip over the 'import' keyword */ + switch (TYPE(CHILD(n, idx))) { case STAR: /* from ... import * */ - n = CHILD(n, 3); + n = CHILD(n, idx); n_children = 1; + if (ndots) { + ast_error(n, "'import *' not allowed with 'from .'"); + return NULL; + } break; case LPAR: /* from ... import (x, y, z) */ - n = CHILD(n, 4); + n = CHILD(n, idx + 1); n_children = NCH(n); break; case import_as_names: /* from ... import x, y, z */ - n = CHILD(n, 3); + n = CHILD(n, idx); n_children = NCH(n); if (n_children % 2 == 0) { ast_error(n, "trailing comma not allowed without" @@ -2219,7 +2235,12 @@ return NULL; asdl_seq_APPEND(aliases, import_alias); } - return ImportFrom(mod->name, aliases, lineno, c->c_arena); + if (mod != NULL) + modname = mod->name; + else + modname = new_identifier("", c->c_arena); + return ImportFrom(modname, aliases, ndots, lineno, + c->c_arena); } PyErr_Format(PyExc_SystemError, "unknown import statement: starts with command '%s'", Index: Python/import.c =================================================================== --- Python/import.c (revision 42606) +++ Python/import.c (working copy) @@ -1893,9 +1893,11 @@ } /* Forward declarations for helper routines */ -static PyObject *get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen); +static PyObject *get_parent(PyObject *globals, char *buf, + Py_ssize_t *p_buflen, int lvl); static PyObject *load_next(PyObject *mod, PyObject *altmod, - char **p_name, char *buf, Py_ssize_t *p_buflen); + char **p_name, char *buf, Py_ssize_t *p_buflen, + int lvl); static int mark_miss(char *name); static int ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen, int recursive); @@ -1904,25 +1906,25 @@ /* The Magnum Opus of dotted-name import :-) */ static PyObject * -import_module_ex(char *name, PyObject *globals, PyObject *locals, - PyObject *fromlist) +import_module_lvl(char *name, PyObject *globals, PyObject *locals, + PyObject *fromlist, int lvl) { char buf[MAXPATHLEN+1]; Py_ssize_t buflen = 0; PyObject *parent, *head, *next, *tail; - parent = get_parent(globals, buf, &buflen); + parent = get_parent(globals, buf, &buflen, lvl); if (parent == NULL) return NULL; - head = load_next(parent, Py_None, &name, buf, &buflen); + head = load_next(parent, Py_None, &name, buf, &buflen, lvl); if (head == NULL) return NULL; tail = head; Py_INCREF(tail); while (name) { - next = load_next(tail, tail, &name, buf, &buflen); + next = load_next(tail, tail, &name, buf, &buflen, lvl); Py_DECREF(tail); if (next == NULL) { Py_DECREF(head); @@ -1950,13 +1952,15 @@ return tail; } +/* For DLL compatibility */ +#undef PyImport_ImportModuleEx PyObject * PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist) { PyObject *result; lock_import(); - result = import_module_ex(name, globals, locals, fromlist); + result = import_module_lvl(name, globals, locals, fromlist, -1); if (unlock_import() < 0) { Py_XDECREF(result); PyErr_SetString(PyExc_RuntimeError, @@ -1965,7 +1969,25 @@ } return result; } +#define PyImport_ImportModuleEx(n, g, l, f) \ + PyImport_ImportModuleLvl(n, g, l, f, -1); +PyObject * +PyImport_ImportModuleLvl(char *name, PyObject *globals, PyObject *locals, + PyObject *fromlist, int lvl) +{ + PyObject *result; + lock_import(); + result = import_module_lvl(name, globals, locals, fromlist, lvl); + if (unlock_import() < 0) { + Py_XDECREF(result); + PyErr_SetString(PyExc_RuntimeError, + "not holding the import lock"); + return NULL; + } + return result; +} + /* Return the package that an import is being performed in. If globals comes from the module foo.bar.bat (not itself a package), this returns the sys.modules entry for foo.bar. If globals is from a package's __init__.py, @@ -1978,13 +2000,13 @@ corresponding entry is not found in sys.modules, Py_None is returned. */ static PyObject * -get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen) +get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int lvl) { static PyObject *namestr = NULL; static PyObject *pathstr = NULL; PyObject *modname, *modpath, *modules, *parent; - if (globals == NULL || !PyDict_Check(globals)) + if (globals == NULL || !PyDict_Check(globals) || !lvl) return Py_None; if (namestr == NULL) { @@ -2013,12 +2035,16 @@ return NULL; } strcpy(buf, PyString_AS_STRING(modname)); - *p_buflen = len; } else { char *start = PyString_AS_STRING(modname); char *lastdot = strrchr(start, '.'); size_t len; + if (lastdot == NULL && lvl > 0) { + PyErr_SetString(PyExc_ValueError, + "Relative importpath too deep"); + return NULL; + } if (lastdot == NULL) return Py_None; len = lastdot - start; @@ -2029,13 +2055,24 @@ } strncpy(buf, start, len); buf[len] = '\0'; - *p_buflen = len; } + while (--lvl > 0) { + char *dot = strrchr(buf, '.'); + if (dot == NULL) { + PyErr_SetString(PyExc_ValueError, + "Relative importpath too deep"); + return NULL; + } + *dot = '\0'; + } + *p_buflen = strlen(buf); + modules = PyImport_GetModuleDict(); parent = PyDict_GetItemString(modules, buf); if (parent == NULL) - parent = Py_None; + PyErr_Format(PyExc_SystemError, + "Parent module '%.200s' not loaded", buf); return parent; /* We expect, but can't guarantee, if parent != None, that: - parent.__name__ == buf @@ -2046,7 +2083,7 @@ /* altmod is either None or same as mod */ static PyObject * load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf, - Py_ssize_t *p_buflen) + Py_ssize_t *p_buflen, int lvl) { char *name = *p_name; char *dot = strchr(name, '.'); @@ -2054,6 +2091,18 @@ char *p; PyObject *result; + if (strlen(name) == 0) { + /* empty module name is only allowed in 'from . import' */ + if (lvl <= 0) { + PyErr_SetString(PyExc_ValueError, + "Empty module name"); + return NULL; + } + Py_INCREF(mod); + *p_name = NULL; + return mod; + } + if (dot == NULL) { *p_name = NULL; len = strlen(name); @@ -2395,8 +2444,8 @@ /* No globals -- use standard builtins, and fake globals */ PyErr_Clear(); - builtins = PyImport_ImportModuleEx("__builtin__", - NULL, NULL, NULL); + builtins = PyImport_ImportModuleLvl("__builtin__", + NULL, NULL, NULL, 0); if (builtins == NULL) return NULL; globals = Py_BuildValue("{OO}", builtins_str, builtins); Index: Python/compile.c =================================================================== --- Python/compile.c (revision 42606) +++ Python/compile.c (working copy) @@ -2436,10 +2436,22 @@ XXX Perhaps change the representation to make this case simpler? */ int i, n = asdl_seq_LEN(s->v.Import.names); + for (i = 0; i < n; i++) { alias_ty alias = asdl_seq_GET(s->v.Import.names, i); int r; + PyObject *level; + if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSIMPORT)) + level = PyInt_FromLong(0); + else + level = PyInt_FromLong(-1); + + if (level == NULL) + return 0; + + ADDOP_O(c, LOAD_CONST, level, consts); + Py_DECREF(level); ADDOP_O(c, LOAD_CONST, Py_None, consts); ADDOP_NAME(c, IMPORT_NAME, alias->name, names); @@ -2472,9 +2484,22 @@ int i, n = asdl_seq_LEN(s->v.ImportFrom.names); PyObject *names = PyTuple_New(n); + PyObject *level; + if (!names) return 0; + if (s->v.ImportFrom.level == 0 && c->c_flags && + !(c->c_flags->cf_flags & CO_FUTURE_ABSIMPORT)) + level = PyInt_FromLong(-1); + else + level = PyInt_FromLong(s->v.ImportFrom.level); + + if (!level) { + Py_DECREF(names); + return 0; + } + /* build up the names */ for (i = 0; i < n; i++) { alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i); @@ -2493,6 +2518,8 @@ } } + ADDOP_O(c, LOAD_CONST, level, consts); + Py_DECREF(level); ADDOP_O(c, LOAD_CONST, names, consts); Py_DECREF(names); ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); Index: Python/Python-ast.c =================================================================== --- Python/Python-ast.c (revision 42606) +++ Python/Python-ast.c (working copy) @@ -114,6 +114,7 @@ char *ImportFrom_fields[]={ "module", "names", + "level", }; PyTypeObject *Exec_type; char *Exec_fields[]={ @@ -477,7 +478,7 @@ Import_type = make_type("Import", stmt_type, Import_fields, 1); if (!Import_type) return 0; ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, - 2); + 3); if (!ImportFrom_type) return 0; Exec_type = make_type("Exec", stmt_type, Exec_fields, 3); if (!Exec_type) return 0; @@ -1087,7 +1088,8 @@ } stmt_ty -ImportFrom(identifier module, asdl_seq * names, int lineno, PyArena *arena) +ImportFrom(identifier module, asdl_seq * names, int level, int lineno, PyArena + *arena) { stmt_ty p; if (!module) { @@ -1103,6 +1105,7 @@ p->kind = ImportFrom_kind; p->v.ImportFrom.module = module; p->v.ImportFrom.names = names; + p->v.ImportFrom.level = level; p->lineno = lineno; return p; } @@ -2151,6 +2154,11 @@ if (PyObject_SetAttrString(result, "names", value) == -1) goto failed; Py_DECREF(value); + value = ast2obj_int(o->v.ImportFrom.level); + if (!value) goto failed; + if (PyObject_SetAttrString(result, "level", value) == -1) + goto failed; + Py_DECREF(value); break; case Exec_kind: result = PyType_GenericNew(Exec_type, NULL, NULL); Index: Python/future.c =================================================================== --- Python/future.c (revision 42606) +++ Python/future.c (working copy) @@ -29,6 +29,8 @@ continue; } else if (strcmp(feature, FUTURE_DIVISION) == 0) { ff->ff_features |= CO_FUTURE_DIVISION; + } else if (strcmp(feature, FUTURE_ABSIMPORT) == 0) { + ff->ff_features |= CO_FUTURE_ABSIMPORT; } else if (strcmp(feature, "braces") == 0) { PyErr_SetString(PyExc_SyntaxError, "not a chance"); Index: Python/bltinmodule.c =================================================================== --- Python/bltinmodule.c (revision 42606) +++ Python/bltinmodule.c (working copy) @@ -37,11 +37,12 @@ PyObject *globals = NULL; PyObject *locals = NULL; PyObject *fromlist = NULL; + int lvl = -1; - if (!PyArg_ParseTuple(args, "s|OOO:__import__", - &name, &globals, &locals, &fromlist)) + if (!PyArg_ParseTuple(args, "s|OOOi:__import__", + &name, &globals, &locals, &fromlist, &lvl)) return NULL; - return PyImport_ImportModuleEx(name, globals, locals, fromlist); + return PyImport_ImportModuleLvl(name, globals, locals, fromlist, lvl); } PyDoc_STRVAR(import_doc, Index: Include/code.h =================================================================== --- Include/code.h (revision 42606) +++ Include/code.h (working copy) @@ -45,6 +45,7 @@ #define CO_GENERATOR_ALLOWED 0x1000 #endif #define CO_FUTURE_DIVISION 0x2000 +#define CO_FUTURE_ABSIMPORT 0x4000 /* absolute import by default */ #define CO_MAXBLOCKS 20 /* Max static block nesting within a function */ Index: Include/pythonrun.h =================================================================== --- Include/pythonrun.h (revision 42606) +++ Include/pythonrun.h (working copy) @@ -7,7 +7,7 @@ extern "C" { #endif -#define PyCF_MASK (CO_FUTURE_DIVISION) +#define PyCF_MASK (CO_FUTURE_DIVISION | CO_FUTURE_ABSIMPORT) #define PyCF_MASK_OBSOLETE (CO_NESTED) #define PyCF_SOURCE_IS_UTF8 0x0100 #define PyCF_DONT_IMPLY_DEDENT 0x0200 Index: Include/import.h =================================================================== --- Include/import.h (revision 42606) +++ Include/import.h (working copy) @@ -14,8 +14,16 @@ PyAPI_FUNC(PyObject *) PyImport_GetModuleDict(void); PyAPI_FUNC(PyObject *) PyImport_AddModule(const char *name); PyAPI_FUNC(PyObject *) PyImport_ImportModule(const char *name); +PyAPI_FUNC(PyObject *) PyImport_ImportModuleLvl(char *name, + PyObject *globals, PyObject *locals, PyObject *fromlist, int lvl); + +/* For DLL compatibility */ +#undef PyImport_ImportModuleEx PyAPI_FUNC(PyObject *) PyImport_ImportModuleEx( char *name, PyObject *globals, PyObject *locals, PyObject *fromlist); +#define PyImport_ImportModuleEx(n, g, l, f) \ + PyImport_ImportModuleLvl(n, g, l, f, -1); + PyAPI_FUNC(PyObject *) PyImport_Import(PyObject *name); PyAPI_FUNC(PyObject *) PyImport_ReloadModule(PyObject *m); PyAPI_FUNC(void) PyImport_Cleanup(void); Index: Include/compile.h =================================================================== --- Include/compile.h (revision 42606) +++ Include/compile.h (working copy) @@ -22,6 +22,7 @@ #define FUTURE_NESTED_SCOPES "nested_scopes" #define FUTURE_GENERATORS "generators" #define FUTURE_DIVISION "division" +#define FUTURE_ABSIMPORT "absolute_import" struct _mod; /* Declare the existence of this type */ PyAPI_FUNC(PyCodeObject *) PyAST_Compile(struct _mod *, const char *, Index: Include/Python-ast.h =================================================================== --- Include/Python-ast.h (revision 42606) +++ Include/Python-ast.h (working copy) @@ -153,6 +153,7 @@ struct { identifier module; asdl_seq *names; + int level; } ImportFrom; struct { @@ -363,8 +364,8 @@ *arena); stmt_ty Assert(expr_ty test, expr_ty msg, int lineno, PyArena *arena); stmt_ty Import(asdl_seq * names, int lineno, PyArena *arena); -stmt_ty ImportFrom(identifier module, asdl_seq * names, int lineno, PyArena - *arena); +stmt_ty ImportFrom(identifier module, asdl_seq * names, int level, int lineno, + PyArena *arena); stmt_ty Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, PyArena *arena); stmt_ty Global(asdl_seq * names, int lineno, PyArena *arena); Index: Grammar/Grammar =================================================================== --- Grammar/Grammar (revision 42606) +++ Grammar/Grammar (working copy) @@ -59,7 +59,7 @@ raise_stmt: 'raise' [test [',' test [',' test]]] import_stmt: import_name | import_from import_name: 'import' dotted_as_names -import_from: ('from' ('.')* dotted_name +import_from: ('from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' | import_as_names)) import_as_name: NAME [NAME NAME] dotted_as_name: dotted_name [NAME NAME] Index: Misc/NEWS =================================================================== --- Misc/NEWS (revision 42606) +++ Misc/NEWS (working copy) @@ -12,6 +12,15 @@ Core and builtins ----------------- +- PEP 328: Absolute/Relative Imports were added. With a 'from __future__ + import absolute_import' statement, 'import module' is now absolute. This + will be the default meaning in Python 2.7 and later. Relative imports are + achieved through the new 'from . import module' mechanism. The __import__ + builtin grew a 5th argument for relativeness of an import statement, which + is only passed when an import is explicitly relative or absolute. + Replacement __import__ hooks should accept this optional 5th argument or + they won't work with explicitly relative or absolute imports. + - PEP 308: conditional expressions were added (x if cond else y). - Patch 1433928: Index: Parser/Python.asdl =================================================================== --- Parser/Python.asdl (revision 42606) +++ Parser/Python.asdl (working copy) @@ -33,7 +33,7 @@ | Assert(expr test, expr? msg) | Import(alias* names) - | ImportFrom(identifier module, alias* names) + | ImportFrom(identifier module, alias* names, int? level) -- Doesn't capture requirement that locals must be -- defined if globals is Index: Lib/__future__.py =================================================================== --- Lib/__future__.py (revision 42606) +++ Lib/__future__.py (working copy) @@ -51,6 +51,7 @@ "nested_scopes", "generators", "division", + "absolute_import", ] __all__ = ["all_feature_names"] + all_feature_names @@ -62,6 +63,7 @@ CO_NESTED = 0x0010 # nested_scopes CO_GENERATOR_ALLOWED = 0x1000 # generators CO_FUTURE_DIVISION = 0x2000 # division +CO_FUTURE_ABSIMPORT = 0x4000 # absolute_import class _Feature: def __init__(self, optionalRelease, mandatoryRelease, compiler_flag): @@ -102,3 +104,7 @@ division = _Feature((2, 2, 0, "alpha", 2), (3, 0, 0, "alpha", 0), CO_FUTURE_DIVISION) + +absolute_import = _Feature((2, 5, 0, "alpha", 1), + (2, 7, 0, "alpha", 0), + CO_FUTURE_ABSIMPORT) Index: Lib/test/test_pkgimport.py =================================================================== --- Lib/test/test_pkgimport.py (revision 42606) +++ Lib/test/test_pkgimport.py (working copy) @@ -9,10 +9,12 @@ while sys.modules.has_key(self.package_name): self.package_name += random.choose(string.letters) self.module_name = self.package_name + '.foo' + self.module2_name = self.package_name + '.foo2' unittest.TestCase.__init__(self, *args, **kw) def remove_modules(self): - for module_name in (self.package_name, self.module_name): + for module_name in (self.package_name, self.module_name, + self.module2_name): if sys.modules.has_key(module_name): del sys.modules[module_name] @@ -24,6 +26,8 @@ os.mkdir(self.package_dir) open(os.path.join(self.package_dir, '__init__'+os.extsep+'py'), 'w') self.module_path = os.path.join(self.package_dir, 'foo'+os.extsep+'py') + self.module2_path = os.path.join(self.package_dir, + 'foo2'+os.extsep+'py') def tearDown(self): for file in os.listdir(self.package_dir): @@ -34,12 +38,12 @@ sys.path.remove(self.test_dir) self.remove_modules() - def rewrite_file(self, contents): + def rewrite_file(self, modpath, contents): for extension in "co": - compiled_path = self.module_path + extension + compiled_path = modpath + extension if os.path.exists(compiled_path): os.remove(compiled_path) - f = open(self.module_path, 'w') + f = open(modpath, 'w') f.write(contents) f.close() @@ -48,7 +52,7 @@ # Generate a couple of broken modules to try importing. # ...try loading the module when there's a SyntaxError - self.rewrite_file('for') + self.rewrite_file(self.module_path, 'for') try: __import__(self.module_name) except SyntaxError: pass else: raise RuntimeError, 'Failed to induce SyntaxError' @@ -61,7 +65,7 @@ var += random.choose(string.letters) # ...make a module that just contains that - self.rewrite_file(var) + self.rewrite_file(self.module_path, var) try: __import__(self.module_name) except NameError: pass @@ -69,11 +73,32 @@ # ...now change the module so that the NameError doesn't # happen - self.rewrite_file('%s = 1' % var) + self.rewrite_file(self.module_path, '%s = 1' % var) module = __import__(self.module_name).foo self.assertEqual(getattr(module, var), 1) + # Test old relative imports inside a package (will go away in 2.7) + self.rewrite_file(self.module2_path, 'import foo') + foo2 = __import__(self.module2_name).foo2 + self.assertTrue(foo2.foo is module) + self.assertEqual(getattr(foo2.foo, var), 1) + # Test relative imports inside a package + self.rewrite_file(self.module2_path, 'from . import foo') + foo2 = __import__(self.module2_name).foo2 + self.assertTrue(foo2.foo is module) + self.assertEqual(getattr(foo2.foo, var), 1) + + # Test relative imports inside a package with absolute-imports on + self.rewrite_file(self.module2_path, + "from __future__ import absolute_import;" + "from . import foo") + foo2 = __import__(self.module2_name).foo2 + self.assertTrue(foo2.foo is module) + self.assertEqual(getattr(foo2.foo, var), 1) + + + def test_main(): run_unittest(TestImport) Index: Lib/test/test_importhooks.py =================================================================== --- Lib/test/test_importhooks.py (revision 42606) +++ Lib/test/test_importhooks.py (working copy) @@ -12,6 +12,10 @@ return __file__ """ +absimp = "import sub\n" +relimp = "from . import sub\n" +futimp = "from __future__ import absolute_import\n" + reload_src = test_src+"""\ reloaded = True """ @@ -19,6 +23,11 @@ test_co = compile(test_src, "", "exec") reload_co = compile(reload_src, "", "exec") +test2_oldabs_co = compile(absimp + test_src, "", "exec") +test2_newabs_co = compile(futimp + absimp + test_src, "", "exec") +test2_newrel_co = compile(relimp + test_src, "", "exec") +test2_futrel_co = compile(futimp + relimp + test_src, "", "exec") + test_path = "!!!_test_!!!" @@ -38,6 +47,11 @@ "hooktestpackage": (True, test_co), "hooktestpackage.sub": (True, test_co), "hooktestpackage.sub.subber": (False, test_co), + "hooktestpackage.oldabs": (False, test2_oldabs_co), + "hooktestpackage.newabs": (False, test2_newabs_co), + "hooktestpackage.newrel": (False, test2_newrel_co), + "hooktestpackage.futrel": (False, test2_futrel_co), + "sub": (False, test_co), "reloadmodule": (False, test_co), } @@ -176,7 +190,33 @@ TestImporter.modules['reloadmodule'] = (False, reload_co) reload(reloadmodule) self.failUnless(hasattr(reloadmodule,'reloaded')) + + import hooktestpackage.oldabs + self.assertEqual(hooktestpackage.oldabs.get_name(), + "hooktestpackage.oldabs") + self.assertEqual(hooktestpackage.oldabs.sub, + hooktestpackage.sub) + import hooktestpackage.newrel + self.assertEqual(hooktestpackage.newrel.get_name(), + "hooktestpackage.newrel") + self.assertEqual(hooktestpackage.newrel.sub, + hooktestpackage.sub) + + import hooktestpackage.futrel + self.assertEqual(hooktestpackage.futrel.get_name(), + "hooktestpackage.futrel") + self.assertEqual(hooktestpackage.futrel.sub, + hooktestpackage.sub) + + import sub + self.assertEqual(sub.get_name(), "sub") + + import hooktestpackage.newabs + self.assertEqual(hooktestpackage.newabs.get_name(), + "hooktestpackage.newabs") + self.assertEqual(hooktestpackage.newabs.sub, sub) + def testMetaPath(self): i = MetaImporter() sys.meta_path.append(i) Index: Lib/test/test_ast.py =================================================================== --- Lib/test/test_ast.py (revision 42606) +++ Lib/test/test_ast.py (working copy) @@ -145,7 +145,7 @@ ('Module', [('TryFinally', [('Pass',)], [('Pass',)])]), ('Module', [('Assert', ('Name', 'v', ('Load',)), None)]), ('Module', [('Import', [('alias', 'sys', None)])]), -('Module', [('ImportFrom', 'sys', [('alias', 'v', None)])]), +('Module', [('ImportFrom', 'sys', [('alias', 'v', None)], 0)]), ('Module', [('Exec', ('Str', 'v'), None, None)]), ('Module', [('Global', ['v'])]), ('Module', [('Expr', ('Num', 1))]),