summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-02-22 09:46:32 (GMT)
committerGitHub <noreply@github.com>2017-02-22 09:46:32 (GMT)
commit5010a77a4da76d8d3fd861a63a7f1ce8f0e9c520 (patch)
tree4d2bc1177ae8ee62768774567684506c7a392681
parent8fa7e22134ac9626b2188ff877f6aeecd3c9e618 (diff)
downloadcpython-5010a77a4da76d8d3fd861a63a7f1ce8f0e9c520.zip
cpython-5010a77a4da76d8d3fd861a63a7f1ce8f0e9c520.tar.gz
cpython-5010a77a4da76d8d3fd861a63a7f1ce8f0e9c520.tar.bz2
[3.5] bpo-29532: Altering a kwarg dictionary passed to functools.partial() no longer affects a partial object after creation. (#222)
-rw-r--r--Lib/test/test_functools.py9
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/_functoolsmodule.c5
3 files changed, 16 insertions, 1 deletions
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 9ccd0ca..a82ca7a 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -80,6 +80,15 @@ class TestPartial:
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
diff --git a/Misc/NEWS b/Misc/NEWS
index 0382337..a01d454 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -32,6 +32,9 @@ Extension Modules
Library
-------
+- bpo-29532: Altering a kwarg dictionary passed to functools.partial()
+ no longer affects a partial object after creation.
+
- Issue #28556: Various updates to typing module: typing.Counter, typing.ChainMap,
improved ABC caching, etc. Original PRs by Jelle Zijlstra, Ivan Levkivskyi,
Manuel Krebber, and Ɓukasz Langa.
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index be0f5f9..0471d66 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -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);