Skip to content

Commit 0dee9c1

Browse files
committed
prevent lambda functions from having docstrings #8164
1 parent 78c1871 commit 0dee9c1

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

Lib/test/test_compile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ def f():
353353
f1, f2 = f()
354354
self.assertNotEqual(id(f1.func_code), id(f2.func_code))
355355

356+
def test_lambda_doc(self):
357+
l = lambda: "foo"
358+
self.assertIsNone(l.__doc__)
359+
356360
def test_unicode_encoding(self):
357361
code = u"# -*- coding: utf-8 -*-\npass\n"
358362
self.assertRaises(SyntaxError, compile, code, "tmp", "exec")

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ What's New in Python 2.7 beta 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #8164: Don't allow lambda functions to have a docstring.
16+
1517
- Issue #3137: Don't ignore errors at startup, especially a keyboard interrupt
1618
(SIGINT). If an error occurs while importing the site module, the error is
1719
printed and Python exits. Initialize the GIL before importing the site

Python/compile.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,11 @@ compiler_lambda(struct compiler *c, expr_ty e)
15181518

15191519
/* unpack nested arguments */
15201520
compiler_arguments(c, args);
1521+
1522+
/* Make None the first constant, so the lambda can't have a
1523+
docstring. */
1524+
if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1525+
return 0;
15211526

15221527
c->u->u_argcount = asdl_seq_LEN(args->args);
15231528
VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);

0 commit comments

Comments
 (0)