Skip to content

Commit 2c16d38

Browse files
committed
PlaceholderType Hidden
1 parent 202c929 commit 2c16d38

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

Lib/functools.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def reduce(function, sequence, initial=_initial_missing):
274274
################################################################################
275275

276276

277-
class PlaceholderType:
277+
class __PlaceholderTypeBase:
278278
"""The type of the Placeholder singleton.
279279
280280
Used as a placeholder for partial arguments.
@@ -296,7 +296,7 @@ def __reduce__(self):
296296
return 'Placeholder'
297297

298298

299-
Placeholder = PlaceholderType()
299+
Placeholder = type('PlaceholderType', (__PlaceholderTypeBase,), {})()
300300

301301

302302
# Purely functional, no descriptor behaviour
@@ -311,19 +311,20 @@ class partial:
311311
def __new__(cls, func, /, *args, **keywords):
312312
if not callable(func):
313313
raise TypeError("the first argument must be callable")
314-
np = 0
315314
if args:
316315
if args[-1] is Placeholder:
317316
raise TypeError("trailing Placeholders are not allowed")
318-
np = args[:-1].count(Placeholder)
317+
np = args.count(Placeholder)
318+
else:
319+
np = 0
319320
if isinstance(func, partial):
320321
pargs = func.args
321322
pnp = func.placeholder_count
322323
# merge args with args of `func` which is `partial`
323324
if pnp and args:
324325
all_args = list(pargs)
325326
nargs = len(args)
326-
pos, j = 0, 0
327+
pos = j = 0
327328
end = nargs if nargs < pnp else pnp
328329
while j < end:
329330
pos = all_args.index(Placeholder, pos)
@@ -351,19 +352,20 @@ def __call__(self, /, *args, **keywords):
351352
np = self.placeholder_count
352353
p_args = self.args
353354
if np:
354-
if len(args) < np:
355+
n = len(args)
356+
if n < np:
355357
raise TypeError(
356358
"missing positional arguments "
357359
"in 'partial' call; expected "
358-
f"at least {np}, got {len(args)}")
360+
f"at least {np}, got {n}")
359361
p_args = list(p_args)
360-
j, pos = 0, 0
362+
pos = j = 0
361363
while j < np:
362364
pos = p_args.index(Placeholder, pos)
363365
p_args[pos] = args[j]
364-
j += 1
365366
pos += 1
366-
args = args[j:]
367+
j += 1
368+
args = args[np:] if n > np else ()
367369
keywords = {**self.keywords, **keywords}
368370
return self.func(*p_args, *args, **keywords)
369371

@@ -409,7 +411,7 @@ def __setstate__(self, state):
409411
self.placeholder_count = placeholder_count
410412

411413
try:
412-
from _functools import partial, PlaceholderType, Placeholder
414+
from _functools import partial, Placeholder
413415
except ImportError:
414416
pass
415417

0 commit comments

Comments
 (0)