Skip to content

Commit

Permalink
bpo-29532: Altering a kwarg dictionary passed to functools.partial() (p…
Browse files Browse the repository at this point in the history
…ython#190)

no longer affects a partial object after creation.
(cherry picked from commit 9639e4a)
  • Loading branch information
serhiy-storchaka committed Feb 20, 2017
1 parent 9a4577a commit 4dbd02c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ def func(a=10, b=20):
p(b=7)
self.assertEqual(d, {'a':3})

def test_kwargs_copy(self):
# Issue #29532: Altering a kwarg dictionary passed to a constructor
# should not affect a partial object after creation
d = {'a': 3}
p = self.partial(capture, **d)
self.assertEqual(p(), ((), {'a': 3}))
d['a'] = 5
self.assertEqual(p(), ((), {'a': 3}))

def test_arg_combinations(self):
# exercise special code paths for zero args in either partial
# object or the caller
Expand Down
5 changes: 4 additions & 1 deletion Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,13 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
if (kw == NULL) {
pto->kw = PyDict_New();
}
else {
else if (Py_REFCNT(kw) == 1) {
Py_INCREF(kw);
pto->kw = kw;
}
else {
pto->kw = PyDict_Copy(kw);
}
}
else {
pto->kw = PyDict_Copy(pkw);
Expand Down

0 comments on commit 4dbd02c

Please sign in to comment.