Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9047c8f

Browse files
committed
SF bug #1048870: call arg of lambda not updating
1 parent 7cb13a9 commit 9047c8f

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

Lib/test/test_compile.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ def test_import(self):
252252
for stmt in fail:
253253
self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
254254

255+
def test_for_distinct_code_objects(self):
256+
# SF bug 1048870
257+
def f():
258+
f1 = lambda x=1: x
259+
f2 = lambda x=2: x
260+
return f1, f2
261+
f1, f2 = f()
262+
self.assertNotEqual(id(f1.func_code), id(f2.func_code))
263+
255264
def test_main():
256265
test_support.run_unittest(TestSpecifics)
257266

Misc/NEWS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ Core and builtins
3737
Extension Modules
3838
-----------------
3939

40-
...
40+
- Bug #1048870: the compiler now generates distinct code objects for
41+
functions with identical bodies. This was producing confusing
42+
traceback messages which pointed to the function where the code
43+
object was first defined rather than the function being executed.
4144

4245
Library
4346
-------

Python/compile.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ code_compare(PyCodeObject *co, PyCodeObject *cp)
261261
if (cmp) return (cmp<0)?-1:1;
262262
cmp = co->co_flags - cp->co_flags;
263263
if (cmp) return (cmp<0)?-1:1;
264+
cmp = co->co_firstlineno - cp->co_firstlineno;
265+
if (cmp) return (cmp<0)?-1:1;
264266
cmp = PyObject_Compare(co->co_code, cp->co_code);
265267
if (cmp) return cmp;
266268
cmp = PyObject_Compare(co->co_consts, cp->co_consts);

0 commit comments

Comments
 (0)