Skip to content

Commit

Permalink
prevent lambda functions from having docstrings python#8164
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminp committed Mar 17, 2010
1 parent 78c1871 commit 0dee9c1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ def f():
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")
Expand Down
2 changes: 2 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 0dee9c1

Please sign in to comment.