]> 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 07:42:02 +0000 (08:42 +0100)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 15 Mar 2017 07:42:02 +0000 (09:42 +0200)
Lib/test/test_functools.py
Misc/ACKS
Misc/NEWS
Modules/_functoolsmodule.c

index b7d648d0b15a553840c5aaf29cf42f1d1bb6d1be..cd4664cec0874567dead66d8fd8b900deace242d 100644 (file)
@@ -402,6 +402,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 c3b29a42a34f75c0d8539194905c69d541e5a847..03afeb8f3876ee0dfee203f273cc69d32ccfbf67 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1371,6 +1371,7 @@ Federico Schwindt
 Barry Scott
 Steven Scott
 Nick Seidenman
+Michael Seifert
 Žiga Seilnacht
 Yury Selivanov
 Fred Sells
index 284a2031eeafc8cd127ee0931068d3723a8b73ef..4c5efefb6593785315c805a3bd3ea7c911e0fd22 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,9 @@ Core and Builtins
 Library
 -------
 
+- bpo-29800: Fix crashes in partial.__repr__ if the keys of partial.keywords
+  are not strings.  Patch by Michael Seifert.
+
 - bpo-29742: get_extra_info() raises exception if get called on closed ssl transport.
   Patch by Nikolay Kim.
 
index 7abc9f464027f7712dc4a021bcf51e5d55c5fdc3..1bcf16a7e00f53998e6c53d393e40dd015547c45 100644 (file)
@@ -250,8 +250,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;
     }