summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2007-03-13 20:46:32 (GMT)
committerGeorg Brandl <georg@python.org>2007-03-13 20:46:32 (GMT)
commit5240d7416c739e22298463cb08ba8aad7c762a4a (patch)
tree19d10f5e864c314c645a9020616626ef54ce638f
parent5dc4fe09b7648f9801558e766b21a3d3b2dcad3b (diff)
downloadcpython-5240d7416c739e22298463cb08ba8aad7c762a4a.zip
cpython-5240d7416c739e22298463cb08ba8aad7c762a4a.tar.gz
cpython-5240d7416c739e22298463cb08ba8aad7c762a4a.tar.bz2
Patch #1444529: the builtin compile() now accepts keyword arguments.
(backport)
-rw-r--r--Doc/lib/libfuncs.tex10
-rw-r--r--Lib/test/test_builtin.py9
-rw-r--r--Misc/NEWS2
-rw-r--r--Python/bltinmodule.c11
4 files changed, 23 insertions, 9 deletions
diff --git a/Doc/lib/libfuncs.tex b/Doc/lib/libfuncs.tex
index 63c1e31..c38e9fc 100644
--- a/Doc/lib/libfuncs.tex
+++ b/Doc/lib/libfuncs.tex
@@ -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
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 333e01e..52178c8 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -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
diff --git a/Misc/NEWS b/Misc/NEWS
index 41db7ff..27669f8 100644
--- 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.
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 082f64c..a846377 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -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},