Skip to content

Commit 0852993

Browse files
committed
review feedback
1 parent a3fd2d6 commit 0852993

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

Doc/library/functools.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ The :mod:`functools` module defines the following functions:
353353
f"got {len(fargs)}")
354354
newargs = list(args)
355355
j = 0
356-
for i in range(len(args)):
357-
if args[i] is Placeholder:
356+
for i, arg in enumarate(args):
357+
if arg is Placeholder:
358358
newargs[i] = fargs[j]
359359
j += 1
360360
newargs.extend(fargs[j:])
@@ -381,7 +381,7 @@ The :mod:`functools` module defines the following functions:
381381
If ``Placeholder`` sentinels are present in *args*, they will be filled first
382382
when :func:`partial` is called. This allows custom selection of positional arguments
383383
to be pre-filled when constructing :ref:`partial object<partial-objects>`.
384-
If ``Placeholder`` sentinels are used, all of them must be filled at call time.:
384+
If ``Placeholder`` sentinels are used, all of them must be filled at call time:
385385

386386
>>> from functools import partial, Placeholder
387387
>>> say_to_world = partial(print, Placeholder, 'world!')

Lib/functools.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -281,18 +281,22 @@ class PlaceholderType:
281281
The type of the Placeholder singleton.
282282
Used as a placeholder for partial arguments.
283283
"""
284-
_instance = None
284+
_singleton = None
285+
285286
def __new__(cls):
286-
if cls._instance is None:
287-
cls._instance = object.__new__(cls)
288-
return cls._instance
287+
if cls._singleton is None:
288+
cls._singleton = object.__new__(cls)
289+
return cls._singleton
289290

290291
def __repr__(self):
291292
return 'Placeholder'
292293

293294
def __bool__(self):
294295
raise TypeError("Placeholder should not be used in a boolean context")
295296

297+
def __reduce__(self):
298+
return type(self), ()
299+
296300

297301
Placeholder = PlaceholderType()
298302

@@ -406,7 +410,7 @@ def __setstate__(self, state):
406410
self.placeholder_count = placeholder_count
407411

408412
try:
409-
from _functools import partial, Placeholder
413+
from _functools import partial, PlaceholderType, Placeholder
410414
except ImportError:
411415
pass
412416

Lib/test/test_functools.py

+7
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ def test_placeholders_optimization(self):
247247
expected = (-1, 0, 1, 2, 3, 4, 5)
248248
self.assertTrue(expected == got and empty == {})
249249

250+
def test_construct_placeholder_singleton(self):
251+
PH = self.module.Placeholder
252+
tp = type(PH)
253+
self.assertIs(tp(), PH)
254+
self.assertRaises(TypeError, tp, 1, 2)
255+
self.assertRaises(TypeError, tp, a=1, b=2)
256+
250257
def test_repr(self):
251258
args = (object(), object())
252259
args_repr = ', '.join(repr(a) for a in args)

Modules/_functoolsmodule.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ static void
7474
placeholder_dealloc(PyObject* placeholder)
7575
{
7676
/* This should never get called, but we also don't want to SEGV if
77-
* we accidentally decref None out of existence. Instead,
78-
* since None is an immortal object, re-set the reference count.
77+
* we accidentally decref Placeholder out of existence. Instead,
78+
* since Placeholder is an immortal object, re-set the reference count.
7979
*/
8080
_Py_SetImmortal(placeholder);
8181
}
@@ -114,7 +114,7 @@ static PyType_Slot placeholder_type_slots[] = {
114114
};
115115

116116
static PyType_Spec placeholder_type_spec = {
117-
.name = "partial.PlaceholderType",
117+
.name = "functools.PlaceholderType",
118118
.basicsize = sizeof(placeholderobject),
119119
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
120120
.slots = placeholder_type_slots
@@ -1701,9 +1701,6 @@ _functools_exec(PyObject *module)
17011701
return -1;
17021702
}
17031703

1704-
// if (PyModule_AddObject(module, "Placeholder", Py_GetPlaceholder()) < 0) {
1705-
// return -1;
1706-
// }
17071704
state->placeholder_type = (PyTypeObject *)PyType_FromModuleAndSpec(module,
17081705
&placeholder_type_spec, NULL);
17091706
if (state->placeholder_type == NULL) {

0 commit comments

Comments
 (0)