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

Skip to content

Commit 32c59b6

Browse files
committed
mangle keyword-only argname when loading defaults (closes #14607)
1 parent ab2d58e commit 32c59b6

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

Lib/test/test_keywordonlyarg.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ def test_issue13343(self):
170170
# used to fail with a SystemError.
171171
lambda *, k1=unittest: None
172172

173+
def test_mangling(self):
174+
class X:
175+
def f(self, *, __a=42):
176+
return __a
177+
self.assertEqual(X().f(), 42)
178+
173179
def test_main():
174180
run_unittest(KeywordOnlyArgTestCase)
175181

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.2.4
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #14607: Fix defaults keyword-only arguments which started with ``__``.
14+
1315
- Issue #13889: Check and (if necessary) set FPU control word before calling
1416
any of the dtoa.c string <-> float conversion functions, on MSVC builds of
1517
Python. This fixes issues when embedding Python in a Delphi app.

Python/compile.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,11 @@ compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
13191319
arg_ty arg = asdl_seq_GET(kwonlyargs, i);
13201320
expr_ty default_ = asdl_seq_GET(kw_defaults, i);
13211321
if (default_) {
1322-
ADDOP_O(c, LOAD_CONST, arg->arg, consts);
1322+
PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1323+
if (!mangled)
1324+
return -1;
1325+
ADDOP_O(c, LOAD_CONST, mangled, consts);
1326+
Py_DECREF(mangled);
13231327
if (!compiler_visit_expr(c, default_)) {
13241328
return -1;
13251329
}

0 commit comments

Comments
 (0)