Skip to content

Commit 9639e4a

Browse files
bpo-29532: Altering a kwarg dictionary passed to functools.partial() (#190)
no longer affects a partial object after creation.
1 parent d0e8212 commit 9639e4a

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Lib/test/test_functools.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ def func(a=10, b=20):
9090
p(b=7)
9191
self.assertEqual(d, {'a':3})
9292

93+
def test_kwargs_copy(self):
94+
# Issue #29532: Altering a kwarg dictionary passed to a constructor
95+
# should not affect a partial object after creation
96+
d = {'a': 3}
97+
p = self.partial(capture, **d)
98+
self.assertEqual(p(), ((), {'a': 3}))
99+
d['a'] = 5
100+
self.assertEqual(p(), ((), {'a': 3}))
101+
93102
def test_arg_combinations(self):
94103
# exercise special code paths for zero args in either partial
95104
# object or the caller

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ Extension Modules
232232
Library
233233
-------
234234

235+
- bpo-29532: Altering a kwarg dictionary passed to functools.partial()
236+
no longer affects a partial object after creation.
237+
235238
- bpo-22807: Add uuid.SafeUUID and uuid.UUID.is_safe to relay information from
236239
the platform about whether generated UUIDs are generated with a
237240
multiprocessing safe method.

Modules/_functoolsmodule.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,13 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
8888
if (kw == NULL) {
8989
pto->kw = PyDict_New();
9090
}
91-
else {
91+
else if (Py_REFCNT(kw) == 1) {
9292
Py_INCREF(kw);
9393
pto->kw = kw;
9494
}
95+
else {
96+
pto->kw = PyDict_Copy(kw);
97+
}
9598
}
9699
else {
97100
pto->kw = PyDict_Copy(pkw);

0 commit comments

Comments
 (0)