Index: Python/bltinmodule.c =================================================================== --- Python/bltinmodule.c (revision 42857) +++ Python/bltinmodule.c (working copy) @@ -31,28 +31,36 @@ static PyObject *filtertuple (PyObject *, PyObject *); static PyObject * -builtin___import__(PyObject *self, PyObject *args) +builtin___import__(PyObject *self, PyObject *args, PyObject *kwds) { char *name; PyObject *globals = NULL; PyObject *locals = NULL; PyObject *fromlist = NULL; int level = -1; + static char *kwlist[] = {"name", "globals", "locals", "fromlist", + "level", NULL}; - if (!PyArg_ParseTuple(args, "s|OOOi:__import__", - &name, &globals, &locals, &fromlist, &level)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__", + kwlist, &name, &globals, &locals, + &fromlist, &level)) return NULL; return PyImport_ImportModuleLevel(name, globals, locals, fromlist, level); } PyDoc_STRVAR(import_doc, -"__import__(name, globals, locals, fromlist) -> module\n\ +"__import__(name, globals, locals, fromlist, level) -> module\n\ \n\ Import a module. The globals are only used to determine the context;\n\ they are not modified. The locals are currently unused. The fromlist\n\ should be a list of names to emulate ``from name import ...'', or an\n\ -empty list to emulate ``import name''.\n\ +empty list to emulate ``import name''. Level is the relative import\n\ +level to use, to emulate 'from . import name'. A positive number is\n\ +the number of dots, 0 means an absolute import, and -1 means the old\n\ +semantics: try relative to the current package, and then absolute. The\n\ +default for 'level' is -1, regardless of future-statements. The module\n\ +name may be the empty string, but only when level is a positive number.\n\n\ When importing a module from a package, note that __import__('A.B', ...)\n\ returns package A when fromlist is empty, but its submodule B when\n\ fromlist is not empty."); @@ -397,7 +405,7 @@ If coercion is not possible, raise TypeError."); static PyObject * -builtin_compile(PyObject *self, PyObject *args) +builtin_compile(PyObject *self, PyObject *args, PyObject *kwds) { char *str; char *filename; @@ -408,9 +416,12 @@ PyCompilerFlags cf; PyObject *result = NULL, *cmd, *tmp = NULL; Py_ssize_t length; + static char *kwlist[] = {"source", "filename", "mode", "flags", + "dont_inherit", NULL}; - if (!PyArg_ParseTuple(args, "Oss|ii:compile", &cmd, &filename, - &startstr, &supplied_flags, &dont_inherit)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile", + kwlist, &cmd, &filename, &startstr, + &supplied_flags, &dont_inherit)) return NULL; cf.cf_flags = supplied_flags; @@ -2208,7 +2219,8 @@ static PyMethodDef builtin_methods[] = { - {"__import__", builtin___import__, METH_VARARGS, import_doc}, + {"__import__", (PyCFunction)builtin___import__, + METH_VARARGS|METH_KEYWORDS, import_doc}, {"abs", builtin_abs, METH_O, abs_doc}, {"all", builtin_all, METH_O, all_doc}, {"any", builtin_any, METH_O, any_doc}, @@ -2217,7 +2229,8 @@ {"chr", builtin_chr, METH_VARARGS, chr_doc}, {"cmp", builtin_cmp, METH_VARARGS, cmp_doc}, {"coerce", builtin_coerce, METH_VARARGS, coerce_doc}, - {"compile", builtin_compile, METH_VARARGS, compile_doc}, + {"compile", (PyCFunction)builtin_compile, + METH_VARARGS|METH_KEYWORDS, compile_doc}, {"delattr", builtin_delattr, METH_VARARGS, delattr_doc}, {"dir", builtin_dir, METH_VARARGS, dir_doc}, {"divmod", builtin_divmod, METH_VARARGS, divmod_doc}, Index: Lib/test/test_builtin.py =================================================================== --- Lib/test/test_builtin.py (revision 42857) +++ Lib/test/test_builtin.py (working copy) @@ -106,8 +106,11 @@ __import__('sys') __import__('time') __import__('string') + __import__(name='sys') + __import__(name='time', level=0) self.assertRaises(ImportError, __import__, 'spamspam') self.assertRaises(TypeError, __import__, 1, 2, 3, 4) + self.assertRaises(TypeError, __import__, 'sys', name='sys') def test_abs(self): # int @@ -235,15 +238,21 @@ compile('print 1\n', '', 'exec') bom = '\xef\xbb\xbf' compile(bom + 'print 1\n', '', 'exec') + compile(source='pass', filename='?', mode='exec') + compile(dont_inherit=0, filename='tmp', source='0', mode='eval') + compile('pass', '?', dont_inherit=1, mode='exec') self.assertRaises(TypeError, compile) self.assertRaises(ValueError, compile, 'print 42\n', '', 'badmode') self.assertRaises(ValueError, compile, 'print 42\n', '', 'single', 0xff) self.assertRaises(TypeError, compile, chr(0), 'f', 'exec') + self.assertRaises(TypeError, compile, 'pass', '?', 'exec', + mode='eval', source='0', filename='tmp') if have_unicode: compile(unicode('print u"\xc3\xa5"\n', 'utf8'), '', 'exec') self.assertRaises(TypeError, compile, unichr(0), 'f', 'exec') self.assertRaises(ValueError, compile, unicode('a = 1'), 'f', 'bad') + def test_delattr(self): import sys sys.spam = 1