[Python-checkins] r54358 - in python/trunk: Doc/lib/libfuncs.tex Lib/test/test_builtin.py Misc/NEWS Python/bltinmodule.c

georg.brandl python-checkins at python.org
Tue Mar 13 21:46:39 CET 2007


Author: georg.brandl
Date: Tue Mar 13 21:46:32 2007
New Revision: 54358
Modified:
 python/trunk/Doc/lib/libfuncs.tex
 python/trunk/Lib/test/test_builtin.py
 python/trunk/Misc/NEWS
 python/trunk/Python/bltinmodule.c
Log:
Patch #1444529: the builtin compile() now accepts keyword arguments.
 (backport)
Modified: python/trunk/Doc/lib/libfuncs.tex
==============================================================================
--- python/trunk/Doc/lib/libfuncs.tex	(original)
+++ python/trunk/Doc/lib/libfuncs.tex	Tue Mar 13 21:46:32 2007
@@ -175,15 +175,15 @@
 \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 @@
 
 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
Modified: python/trunk/Lib/test/test_builtin.py
==============================================================================
--- python/trunk/Lib/test/test_builtin.py	(original)
+++ python/trunk/Lib/test/test_builtin.py	Tue Mar 13 21:46:32 2007
@@ -107,9 +107,12 @@
 __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 @@
 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
Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Tue Mar 13 21:46:32 2007
@@ -12,6 +12,8 @@
 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.
 
Modified: python/trunk/Python/bltinmodule.c
==============================================================================
--- python/trunk/Python/bltinmodule.c	(original)
+++ python/trunk/Python/bltinmodule.c	Tue Mar 13 21:46:32 2007
@@ -402,7 +402,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;
@@ -413,9 +413,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;
@@ -2237,7 +2240,7 @@
 	{"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},


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /