]> granicus.if.org Git - python/commitdiff
bpo-29800: Fix crashes in partial.__repr__ if the keys of partial.keywords are not...
authorMichael Seifert <michaelseifert04@yahoo.de>
Wed, 15 Mar 2017 05:26:33 +0000 (06:26 +0100)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 15 Mar 2017 05:26:33 +0000 (07:26 +0200)
Lib/test/test_functools.py
Misc/ACKS
Misc/NEWS
Modules/_functoolsmodule.c

index 612ca17a60bc8cd90cbea0f32f059151a3c5e600..29ea49362262dd0d4d30172abd094a0cab930f93 100644 (file)
@@ -403,6 +403,32 @@ class TestPartialC(TestPartial, unittest.TestCase):
         else:
             self.fail('partial object allowed __dict__ to be deleted')
 
+    def test_manually_adding_non_string_keyword(self):
+        p = self.partial(capture)
+        # Adding a non-string/unicode keyword to partial kwargs
+        p.keywords[1234] = 'value'
+        r = repr(p)
+        self.assertIn('1234', r)
+        self.assertIn("'value'", r)
+        with self.assertRaises(TypeError):
+            p()
+
+    def test_keystr_replaces_value(self):
+        p = self.partial(capture)
+
+        class MutatesYourDict(object):
+            def __str__(self):
+                p.keywords[self] = ['sth2']
+                return 'astr'
+
+        # Raplacing the value during key formatting should keep the original
+        # value alive (at least long enough).
+        p.keywords[MutatesYourDict()] = ['sth']
+        r = repr(p)
+        self.assertIn('astr', r)
+        self.assertIn("['sth']", r)
+
+
 class TestPartialPy(TestPartial, unittest.TestCase):
     partial = py_functools.partial
 
index b7f1282c69c0ce0e46cd471038e0ec4c7b63c051..c99aeaa335e971339c1c15b9cc3f69b0ab38eda7 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1374,6 +1374,7 @@ Federico Schwindt
 Barry Scott
 Steven Scott
 Nick Seidenman
+Michael Seifert
 Žiga Seilnacht
 Yury Selivanov
 Fred Sells
index b2a1a6eaee12911154c794fcdf0761ea8da9d3db..2f7bd7ac7b20d4e14c5edc19c96589fb926a986a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -281,6 +281,9 @@ Extension Modules
 Library
 -------
 
+- bpo-29800: Fix crashes in partial.__repr__ if the keys of partial.keywords
+  are not strings.  Patch by Michael Seifert.
+
 - bpo-8256: Fixed possible failing or crashing input() if attributes "encoding"
   or "errors" of sys.stdin or sys.stdout are not set or are not strings.
 
index f2af4ab462dbf2d8ab091a130d10ab2421ac2983..592edbb61404594adaa1159375307a6c4487c5c0 100644 (file)
@@ -297,8 +297,11 @@ partial_repr(partialobject *pto)
     /* Pack keyword arguments */
     assert (PyDict_Check(pto->kw));
     for (i = 0; PyDict_Next(pto->kw, &i, &key, &value);) {
-        Py_SETREF(arglist, PyUnicode_FromFormat("%U, %U=%R", arglist,
+        /* Prevent key.__str__ from deleting the value. */
+        Py_INCREF(value);
+        Py_SETREF(arglist, PyUnicode_FromFormat("%U, %S=%R", arglist,
                                                 key, value));
+        Py_DECREF(value);
         if (arglist == NULL)
             goto done;
     }