Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions 26 Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,32 @@ def __getitem__(self, key):
self.assertRaises(TypeError, f.__setstate__, BadSequence())


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 = staticmethod(py_functools.partial)

Expand Down
1 change: 1 addition & 0 deletions 1 Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,7 @@ Federico Schwindt
Barry Scott
Steven Scott
Nick Seidenman
Michael Seifert
Žiga Seilnacht
Yury Selivanov
Fred Sells
Expand Down
3 changes: 3 additions & 0 deletions 3 Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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-29742: get_extra_info() raises exception if get called on closed ssl transport.
Patch by Nikolay Kim.

Expand Down
5 changes: 4 additions & 1 deletion 5 Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,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;
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.