]> granicus.if.org Git - python/commitdiff
prevent lambda functions from having docstrings #8164
authorBenjamin Peterson <benjamin@python.org>
Wed, 17 Mar 2010 20:41:42 +0000 (20:41 +0000)
committerBenjamin Peterson <benjamin@python.org>
Wed, 17 Mar 2010 20:41:42 +0000 (20:41 +0000)
Lib/test/test_compile.py
Misc/NEWS
Python/compile.c

index 5be65088e68ac6a9c834fa150a01a010bd0706af..1a2009c6ebef5956dedb391e09f2049c5b53adf6 100644 (file)
@@ -353,6 +353,10 @@ if 1:
         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")
index 35fddebf933d93461090401aeeca9d65ba3079fc..ffcdaa7943c024c04612b622d79cf81f53645464 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.7 beta 1?
 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
index 4ea2ee9afe3458e7a8d23bbd50574e1f1726ad24..12210e1296ff4cd2866f8a02752f53d78f092d91 100644 (file)
@@ -1518,6 +1518,11 @@ compiler_lambda(struct compiler *c, expr_ty e)
 
        /* 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);