]> granicus.if.org Git - python/commitdiff
Patch #1444529: the builtin compile() now accepts keyword arguments.
authorGeorg Brandl <georg@python.org>
Tue, 13 Mar 2007 20:46:32 +0000 (20:46 +0000)
committerGeorg Brandl <georg@python.org>
Tue, 13 Mar 2007 20:46:32 +0000 (20:46 +0000)
 (backport)

Doc/lib/libfuncs.tex
Lib/test/test_builtin.py
Misc/NEWS
Python/bltinmodule.c

index 63c1e318af0a3d979039bdb826d20816a6a846ed..c38e9fcf84a464ff226308a21178d8a5ab466229 100644 (file)
@@ -175,15 +175,15 @@ class C:
   \code{\var{x} > \var{y}}.
 \end{funcdesc}
 
-\begin{funcdesc}{compile}{string, filename, kind\optional{,
+\begin{funcdesc}{compile}{source, filename, mode\optional{,
                           flags\optional{, dont_inherit}}}
-  Compile the \var{string} into a code object.  Code objects can be
+  Compile the \var{source} into a code object.  Code objects can be
   executed by an \keyword{exec} statement or evaluated by a call to
   \function{eval()}.  The \var{filename} argument should
   give the file from which the code was read; pass some recognizable value
   if it wasn't read from a file (\code{'<string>'} is commonly used).
-  The \var{kind} argument specifies what kind of code must be
-  compiled; it can be \code{'exec'} if \var{string} consists of a
+  The \var{mode} argument specifies what kind of code must be
+  compiled; it can be \code{'exec'} if \var{source} consists of a
   sequence of statements, \code{'eval'} if it consists of a single
   expression, or \code{'single'} if it consists of a single
   interactive statement (in the latter case, expression statements
@@ -198,7 +198,7 @@ class C:
 
   The optional arguments \var{flags} and \var{dont_inherit}
   (which are new in Python 2.2) control which future statements (see
-  \pep{236}) affect the compilation of \var{string}.  If neither is
+  \pep{236}) affect the compilation of \var{source}.  If neither is
   present (or both are zero) the code is compiled with those future
   statements that are in effect in the code that is calling compile.
   If the \var{flags} argument is given and \var{dont_inherit} is not
index 333e01e7067ea36af78b139e75f4fce520e4bdd6..52178c8b64cbb9a270529cb39a78dcda5664339a 100644 (file)
@@ -107,9 +107,12 @@ class BuiltinTest(unittest.TestCase):
         __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(ValueError, __import__, '')
+        self.assertRaises(TypeError, __import__, 'sys', name='sys')
 
     def test_abs(self):
         # int
@@ -243,15 +246,21 @@ class BuiltinTest(unittest.TestCase):
         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', '<string>', 'badmode')
         self.assertRaises(ValueError, compile, 'print 42\n', '<string>', '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
index 41db7ffbaa03b11789f3b957f61f14e5380643a7..27669f838e99bd142b66dcdb30e610b431a61f9e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.6 alpha 1?
 Core and builtins
 -----------------
 
+- Patch #1444529: the builtin compile() now accepts keyword arguments.
+
 - Bug #1678647: write a newline after printing an exception in any
   case, even when converting the value to a string failed.
 
index 082f64c344747290a030cffeca774fbc96499e98..a846377ec6b7b43ad8e1de58185ee731828a6fa3 100644 (file)
@@ -402,7 +402,7 @@ a common type, using the same rules as used by arithmetic operations.\n\
 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;
@@ -413,9 +413,12 @@ builtin_compile(PyObject *self, PyObject *args)
        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;
@@ -2237,7 +2240,7 @@ static PyMethodDef builtin_methods[] = {
        {"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},