From 8a5d5c8f188f978881ed8ad3d913f0cfdc05eef4 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 21 Feb 2017 18:18:27 +0200 Subject: [PATCH 1/2] bpo-29532: Altering a kwarg dictionary passed to functools.partial() no longer affects a partial object after creation. (#209) --- Lib/test/test_functools.py | 9 +++++++++ Misc/NEWS | 13 +++++++++++++ Modules/_functoolsmodule.c | 5 ++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 9ccd0cafc489cf..a82ca7ab88c38f 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -80,6 +80,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 diff --git a/Misc/NEWS b/Misc/NEWS index 038233735f6c13..74ab7d05bc3477 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -32,6 +32,19 @@ Extension Modules Library ------- +- bpo-29532: Altering a kwarg dictionary passed to functools.partial() + no longer affects a partial object after creation. + +- bpo-22807: Add uuid.SafeUUID and uuid.UUID.is_safe to relay information from + the platform about whether generated UUIDs are generated with a + multiprocessing safe method. + +- bpo-29576: Improve some deprecations in importlib. Some deprecated methods + now emit DeprecationWarnings and have better descriptive messages. + +- bpo-29534: Fixed different behaviour of Decimal.from_float() + for _decimal and _pydecimal. Thanks Andrew Nester. + - 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 be0f5f9e652abc..0471d665e65a58 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); From 372a25c7d71d7f37da3f84bc0919900a6bb05674 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 21 Feb 2017 22:40:32 +0200 Subject: [PATCH 2/2] Fix Misc/NEWS. --- Misc/NEWS | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Misc/NEWS b/Misc/NEWS index 74ab7d05bc3477..a01d454ab78f7d 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -35,16 +35,6 @@ Library - bpo-29532: Altering a kwarg dictionary passed to functools.partial() no longer affects a partial object after creation. -- bpo-22807: Add uuid.SafeUUID and uuid.UUID.is_safe to relay information from - the platform about whether generated UUIDs are generated with a - multiprocessing safe method. - -- bpo-29576: Improve some deprecations in importlib. Some deprecated methods - now emit DeprecationWarnings and have better descriptive messages. - -- bpo-29534: Fixed different behaviour of Decimal.from_float() - for _decimal and _pydecimal. Thanks Andrew Nester. - - 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.