8000 test_trailing_placeholders and bug fix · python/cpython@8fa0ec5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8fa0ec5

Browse files
committed
test_trailing_placeholders and bug fix
1 parent b76ffee commit 8fa0ec5

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

Lib/functools.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,15 @@ def _partial_prepare_merger(args):
310310
else:
311311
order.append(i)
312312
phcount = j - nargs
313-
merger = itemgetter(*order) if phcount else None
313+
if phcount:
314+
if nargs == 1:
315+
i = order[0]
316+
def merger(all_args):
317+
return (all_args[i],)
318+
else:
319+
merger = itemgetter(*order)
320+
else:
321+
merger = None
314322
return phcount, merger
315323

316324
def _partial_repr(self):
@@ -403,7 +411,8 @@ def __setstate__(self, state):
403411

404412
phcount, merger = _partial_prepare_merger(args)
405413

406-
args = tuple(args) # just in case it's a subclass
414+
if type(args) is not tuple:
415+
args = tuple(args) # just in case it's a subclass
407416
if kwds is None:
408417
kwds = {}
409418
elif type(kwds) is not dict: # XXX does it need to be *exactly* dict?

Lib/test/test_functools.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,16 @@ def foo(bar):
211211
p2.new_attr = 'spam'
212212
self.assertEqual(p2.new_attr, 'spam')
213213

214-
# def test_placeholders_trailing_trim(self):
215-
# PH = self.module.Placeholder
216-
# for args, call_args, expected_args in [
217-
# [(PH,), (), ()],
218-
# [(0, PH), (), (0,)],
219-
# [(0, PH, 1, PH, PH, PH), (2,), (0, 2, 1)]
220-
# ]:
221-
# actual_args, actual_kwds = self.partial(capture, *args)(*call_args)
222-
# self.assertEqual(actual_args, expected_args)
223-
# self.assertEqual(actual_kwds, {})
214+
def test_trailing_placeholders(self):
215+
PH = self.module.Placeholder
216+
for args, call_args, expected_args in [
217+
[(PH,), (1,), (1,)],
218+
[(0, PH), (1,), (0, 1)],
219+
[(0, PH, 2, PH, PH), (1, 3, 4), (0, 1, 2, 3, 4)]
220+
]:
221+
actual_args, actual_kwds = self.partial(capture, *args)(*call_args)
222+
self.assertEqual(actual_args, expected_args)
223+
self.assertEqual(actual_kwds, {})
224224

225225
def test_placeholders(self):
226226
PH = self.module.Placeholder
@@ -373,12 +373,6 @@ def test_setstate(self):
373373
f()
374374
self.assertEqual(f(2), ((2, 1), dict(a=10)))
375375

376-
# # Trailing Placeholder error
377-
# f = self.partial(signature)
378-
# msg_regex = re.escape("unexpected trailing Placeholders")
379-
# with self.assertRaisesRegex(TypeError, f'^{msg_regex}$') as cm:
380-
# f.__setstate__((capture, (1, PH), dict(a=10), dict(attr=[])))
381-
382376
def test_setstate_errors(self):
383377
f = self.partial(signature)
384378
self.assertRaises(TypeError, f.__setstate__, (capture, (), {}))

0 commit comments

Comments
 (0)
0