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

Skip to content

Commit e48fd93

Browse files
bpo-29532: Altering a kwarg dictionary passed to functools.partial() no longer affects a partial object after creation. (#209)
1 parent 51a477c commit e48fd93

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Lib/test/test_functools.py

+9
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ def func(a=10, b=20):
8989
p(b=7)
9090
self.assertEqual(d, {'a':3})
9191

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

Misc/NEWS

+13
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ Extension Modules
6666
Library
6767
-------
6868

69+
- bpo-29532: Altering a kwarg dictionary passed to functools.partial()
70+
no longer affects a partial object after creation.
71+
72+
- bpo-22807: Add uuid.SafeUUID and uuid.UUID.is_safe to relay information from
73+
the platform about whether generated UUIDs are generated with a
74+
multiprocessing safe method.
75+
76+
- bpo-29576: Improve some deprecations in importlib. Some deprecated methods
77+
now emit DeprecationWarnings and have better descriptive messages.
78+
79+
- bpo-29534: Fixed different behaviour of Decimal.from_float()
80+
for _decimal and _pydecimal. Thanks Andrew Nester.
81+
6982
- Issue #28556: Various updates to typing module: typing.Counter, typing.ChainMap,
7083
improved ABC caching, etc. Original PRs by Jelle Zijlstra, Ivan Levkivskyi,
7184
Manuel Krebber, and Łukasz Langa.

Modules/_functoolsmodule.c

+4-1
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)