[Python-checkins] r79034 - in python/trunk: Lib/test/test_compile.py Misc/NEWS Python/compile.c

benjamin.peterson python-checkins at python.org
Wed Mar 17 21:41:42 CET 2010


Author: benjamin.peterson
Date: Wed Mar 17 21:41:42 2010
New Revision: 79034

Log:
prevent lambda functions from having docstrings #8164

Modified:
   python/trunk/Lib/test/test_compile.py
   python/trunk/Misc/NEWS
   python/trunk/Python/compile.c

Modified: python/trunk/Lib/test/test_compile.py
==============================================================================
--- python/trunk/Lib/test/test_compile.py	(original)
+++ python/trunk/Lib/test/test_compile.py	Wed Mar 17 21:41:42 2010
@@ -353,6 +353,10 @@
         f1, f2 = f()
         self.assertNotEqual(id(f1.func_code), id(f2.func_code))
 
+    def test_lambda_doc(self):
+        l = lambda: "foo"
+        self.assertIsNone(l.__doc__)
+
     def test_unicode_encoding(self):
         code = u"# -*- coding: utf-8 -*-\npass\n"
         self.assertRaises(SyntaxError, compile, code, "tmp", "exec")

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Wed Mar 17 21:41:42 2010
@@ -12,6 +12,8 @@
 Core and Builtins
 -----------------
 
+- Issue #8164: Don't allow lambda functions to have a docstring.
+
 - Issue #3137: Don't ignore errors at startup, especially a keyboard interrupt
   (SIGINT).  If an error occurs while importing the site module, the error is
   printed and Python exits.  Initialize the GIL before importing the site

Modified: python/trunk/Python/compile.c
==============================================================================
--- python/trunk/Python/compile.c	(original)
+++ python/trunk/Python/compile.c	Wed Mar 17 21:41:42 2010
@@ -1518,6 +1518,11 @@
 
 	/* unpack nested arguments */
 	compiler_arguments(c, args);
+
+	/* Make None the first constant, so the lambda can't have a
+	   docstring. */
+	if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
+		return 0;
 	
 	c->u->u_argcount = asdl_seq_LEN(args->args);
 	VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);


More information about the Python-checkins mailing list