diff -burp Python-2.6/Include/import.h Python-2.6-imp_import_module/Include/import.h --- Python-2.6/Include/import.h 2008-01-04 00:16:32.000000000 +0200 +++ Python-2.6-imp_import_module/Include/import.h 2008-11-26 16:29:53.000000000 +0200 @@ -16,7 +16,8 @@ PyAPI_FUNC(PyObject *) PyImport_AddModul PyAPI_FUNC(PyObject *) PyImport_ImportModule(const char *name); PyAPI_FUNC(PyObject *) PyImport_ImportModuleNoBlock(const char *); PyAPI_FUNC(PyObject *) PyImport_ImportModuleLevel(char *name, - PyObject *globals, PyObject *locals, PyObject *fromlist, int level); + PyObject *globals, PyObject *locals, PyObject *fromlist, + int level, char submodule); #define PyImport_ImportModuleEx(n, g, l, f) \ PyImport_ImportModuleLevel(n, g, l, f, -1) diff -burp Python-2.6/Python/bltinmodule.c Python-2.6-imp_import_module/Python/bltinmodule.c --- Python-2.6/Python/bltinmodule.c 2008-08-18 05:01:21.000000000 +0300 +++ Python-2.6-imp_import_module/Python/bltinmodule.c 2008-11-28 19:29:01.000000000 +0200 @@ -46,7 +46,7 @@ builtin___import__(PyObject *self, PyObj kwlist, &name, &globals, &locals, &fromlist, &level)) return NULL; return PyImport_ImportModuleLevel(name, globals, locals, - fromlist, level); + fromlist, level, 0); } PyDoc_STRVAR(import_doc, diff -burp Python-2.6/Python/codecs.c Python-2.6-imp_import_module/Python/codecs.c --- Python-2.6/Python/codecs.c 2008-06-09 07:58:54.000000000 +0300 +++ Python-2.6-imp_import_module/Python/codecs.c 2008-11-26 16:29:53.000000000 +0200 @@ -842,7 +842,7 @@ static int _PyCodecRegistry_Init(void) interp->codec_error_registry == NULL) Py_FatalError("can't initialize codec registry"); - mod = PyImport_ImportModuleLevel("encodings", NULL, NULL, NULL, 0); + mod = PyImport_ImportModuleLevel("encodings", NULL, NULL, NULL, 0, 0); if (mod == NULL) { if (PyErr_ExceptionMatches(PyExc_ImportError)) { /* Ignore ImportErrors... this is done so that diff -burp Python-2.6/Python/import.c Python-2.6-imp_import_module/Python/import.c --- Python-2.6/Python/import.c 2008-09-01 17:18:30.000000000 +0300 +++ Python-2.6-imp_import_module/Python/import.c 2008-11-28 20:45:22.000000000 +0200 @@ -2064,7 +2064,7 @@ static PyObject * import_submodule(PyObj static PyObject * import_module_level(char *name, PyObject *globals, PyObject *locals, - PyObject *fromlist, int level) + PyObject *fromlist, int level, char submodule) { char buf[MAXPATHLEN+1]; Py_ssize_t buflen = 0; @@ -2115,13 +2115,13 @@ import_module_level(char *name, PyObject fromlist = NULL; } - if (fromlist == NULL) { + if (fromlist == NULL && !submodule) { Py_DECREF(tail); return head; } Py_DECREF(head); - if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) { + if (!submodule && !ensure_fromlist(tail, fromlist, buf, buflen, 0)) { Py_DECREF(tail); return NULL; } @@ -2131,11 +2131,12 @@ import_module_level(char *name, PyObject PyObject * PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals, - PyObject *fromlist, int level) + PyObject *fromlist, int level, char submodule) { PyObject *result; lock_import(); - result = import_module_level(name, globals, locals, fromlist, level); + result = import_module_level(name, globals, locals, fromlist, level, + submodule); if (unlock_import() < 0) { Py_XDECREF(result); PyErr_SetString(PyExc_RuntimeError, @@ -2701,7 +2702,7 @@ PyImport_Import(PyObject *module_name) PyErr_Clear(); builtins = PyImport_ImportModuleLevel("__builtin__", - NULL, NULL, NULL, 0); + NULL, NULL, NULL, 0, 0); if (builtins == NULL) return NULL; globals = Py_BuildValue("{OO}", builtins_str, builtins); @@ -3039,6 +3040,22 @@ imp_reload(PyObject *self, PyObject *v) return PyImport_ReloadModule(v); } +static PyObject * +imp_import_module(PyObject *self, PyObject *args, PyObject *kwds) +{ + /* do we need globals? */ + static char *kwlist[] = {"name", "globals", "level", 0}; + + char *name; + PyObject *globals = NULL; + int level = -1; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|Oi:import_module", + kwlist, &name, &globals, &level)) + return NULL; + + return PyImport_ImportModuleLevel(name, globals, NULL, NULL, level, 1); +} /* Doc strings */ @@ -3094,6 +3111,12 @@ PyDoc_STRVAR(doc_release_lock, Release the interpreter's import lock.\n\ On platforms without threads, this function does nothing."); +PyDoc_STRVAR(doc_import_module, +"import_module(name, globals, level) -> module\n\ +Import the tail module, given dotted module hierarchy in name.\n\ +globals only determines the package context and is not modified.\n\ +level specifies whether to use absolute or relative imports."); + static PyMethodDef imp_methods[] = { {"reload", imp_reload, METH_O, doc_reload}, {"find_module", imp_find_module, METH_VARARGS, doc_find_module}, @@ -3104,6 +3127,7 @@ static PyMethodDef imp_methods[] = { {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held}, {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock}, {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock}, + {"import_module", imp_import_module, METH_VARARGS | METH_KEYWORDS, doc_import_module}, /* The rest are obsolete */ {"get_frozen_object", imp_get_frozen_object, METH_VARARGS}, {"init_builtin", imp_init_builtin, METH_VARARGS},