Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-119127: functools.partial placeholders#119827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
ee7333c8bcc462c67c9b4680d9009591ff5067e9388af20b3607a0b1f55801e58941453722e07a79c2af12aaa7292c767b496a9d238d9c11707b95714b38ca32bca198576493a3fd2d608529936fea348caec6e8115b8c53f5f00b202c9292c16d38400ff558ccc38fe7c82c7c9b7ef3e59d7117bfc5917957a978aaee6afe8e0ad00dd80ed352cfa9038ed549b8c71bc1fdbd30672211185510266b4fadd58a125971fbb9033650d31e5d1a3d39b09e4c5df16f12f882dd600f9cb653d255524404044e800217b38ee45011f47db3c872bdfd16189a6c6ef21c8d73ea8bd3ae70e47ed2eacf5ef78d8d30a8640e6e3d28266c305d14bf68cee642d58d6c28e8744bcbb8964704881ae6c3ad7d95e5d484File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -333,7 +333,7 @@ def __call__(self, /, *args, **keywords): | ||
| if len(args) < placeholder_count: | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| raise TypeError( | ||
| "missing positional arguments in 'partial' call; expected " | ||
| f"at least {placeholder_count}, got {len(args)}") | ||
| f_args = list(self.args) | ||
| j, pos = 0, 0 | ||
| while j < placeholder_count: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -43,27 +43,118 @@ get_functools_state(PyObject *module) | ||
| /* partial object **********************************************************/ | ||
| /* | ||
| Placeholder is an object that can be used to signal that positional | ||
| argument place is empty when using `partial` class | ||
| */ | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| PyObject* | ||
| Py_GetPlaceholder(void); | ||
| static PyObject * | ||
| placeholder_repr(PyObject *op) | ||
| { | ||
| return PyUnicode_FromString("Placeholder"); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Maybe use In particular, you could also decide whether the singleton should be implemented (and exported) using the same logic as for (Sorry, I only remembered about that singleton now and not before) | ||
| } | ||
| static void | ||
| placeholder_dealloc(PyObject* placeholder) | ||
| { | ||
| /* This should never get called, but we also don't want to SEGV if | ||
| * we accidentally decref None out of existence. Instead, | ||
| * since None is an immortal object, re-set the reference count. | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| */ | ||
| _Py_SetImmortal(placeholder); | ||
| } | ||
| static PyObject * | ||
| placeholder_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) | ||
| { | ||
| if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) { | ||
| PyErr_SetString(PyExc_TypeError, "PlaceholderType takes no arguments"); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. You could use | ||
| return NULL; | ||
| } | ||
| return Py_GetPlaceholder(); | ||
| } | ||
| static int | ||
| placeholder_bool(PyObject *v) | ||
| { | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "Placeholder should not be used in a boolean context"); | ||
| return -1; | ||
| } | ||
| static PyNumberMethods placeholder_as_number = { | ||
| .nb_bool = placeholder_bool, | ||
| }; | ||
| PyDoc_STRVAR(placeholder_doc, | ||
| "PlaceholderType()\n" | ||
| "--\n\n" | ||
| "The type of the Placeholder singleton."); | ||
| PyTypeObject _PyPlaceholder_Type = { | ||
| PyVarObject_HEAD_INIT(&PyType_Type, 0) | ||
| "PlaceholderType", | ||
| 0, | ||
| 0, | ||
| placeholder_dealloc, /*tp_dealloc*/ /*never called*/ | ||
| 0, /*tp_vectorcall_offset*/ | ||
| 0, /*tp_getattr*/ | ||
| 0, /*tp_setattr*/ | ||
| 0, /*tp_as_async*/ | ||
| placeholder_repr, /*tp_repr*/ | ||
| &placeholder_as_number, /*tp_as_number*/ | ||
| 0, /*tp_as_sequence*/ | ||
| 0, /*tp_as_mapping*/ | ||
| 0, /*tp_hash */ | ||
| 0, /*tp_call */ | ||
| 0, /*tp_str */ | ||
| 0, /*tp_getattro */ | ||
| 0, /*tp_setattro */ | ||
| 0, /*tp_as_buffer */ | ||
| Py_TPFLAGS_DEFAULT, /*tp_flags */ | ||
| placeholder_doc, /*tp_doc */ | ||
| 0, /*tp_traverse */ | ||
| 0, /*tp_clear */ | ||
| 0, /*tp_richcompare */ | ||
| 0, /*tp_weaklistoffset */ | ||
| 0, /*tp_iter */ | ||
| 0, /*tp_iternext */ | ||
| 0, /*tp_methods */ | ||
| 0, /*tp_members */ | ||
| 0, /*tp_getset */ | ||
| 0, /*tp_base */ | ||
| 0, /*tp_dict */ | ||
| 0, /*tp_descr_get */ | ||
| 0, /*tp_descr_set */ | ||
| 0, /*tp_dictoffset */ | ||
| 0, /*tp_init */ | ||
| 0, /*tp_alloc */ | ||
| placeholder_new, /*tp_new */ | ||
| }; | ||
| PyObject _Py_PlaceholderStruct = _PyObject_HEAD_INIT(&_PyPlaceholder_Type); | ||
| #define PY_CONSTANT_PLACEHOLDER 0 | ||
| static PyObject* constants[] = { | ||
| &_Py_PlaceholderStruct, // PY_CONSTANT_PLACEHOLDER | ||
| }; | ||
| PyObject* | ||
| Py_GetPlaceholder(void) | ||
| { | ||
| if (PY_CONSTANT_PLACEHOLDER < Py_ARRAY_LENGTH(constants)) { | ||
| return constants[PY_CONSTANT_PLACEHOLDER]; | ||
| } | ||
| else { | ||
| PyErr_BadInternalCall(); | ||
| return NULL; | ||
| } | ||
| } | ||
| typedef struct { | ||
| @@ -150,7 +241,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw) | ||
| return NULL; | ||
| } | ||
| pto->placeholder =Py_GetPlaceholder(); | ||
| Py_ssize_t nnp = 0; | ||
| Py_ssize_t nnargs = PyTuple_GET_SIZE(nargs); | ||
| PyObject *item; | ||
| @@ -598,18 +689,19 @@ partial_repr(partialobject *pto) | ||
| static PyObject * | ||
| partial_reduce(partialobject *pto, PyObject *unused) | ||
| { | ||
| return Py_BuildValue("O(O)(OOOnO)", Py_TYPE(pto), pto->fn, pto->fn, | ||
| pto->args, pto->kw, pto->np, | ||
| pto->dict ? pto->dict : Py_None); | ||
| } | ||
| static PyObject * | ||
| partial_setstate(partialobject *pto, PyObject *state) | ||
| { | ||
| PyObject *fn, *fnargs, *kw, *dict; | ||
| Py_ssize_t np; | ||
| if (!PyTuple_Check(state) || | ||
| !PyArg_ParseTuple(state, "OOOnO", &fn, &fnargs, &kw, &np, &dict) || | ||
| !PyCallable_Check(fn) || | ||
| !PyTuple_Check(fnargs) || | ||
| (kw != Py_None && !PyDict_Check(kw))) | ||
| @@ -640,10 +732,10 @@ partial_setstate(partialobject *pto, PyObject *state) | ||
| dict = NULL; | ||
| else | ||
| Py_INCREF(dict); | ||
| Py_SETREF(pto->fn, Py_NewRef(fn)); | ||
| Py_SETREF(pto->args, fnargs); | ||
| Py_SETREF(pto->kw, kw); | ||
| pto->np = np; | ||
| Py_XSETREF(pto->dict, dict); | ||
| partial_setvectorcall(pto); | ||
| Py_RETURN_NONE; | ||
| @@ -1626,6 +1718,7 @@ static PyType_Spec lru_cache_type_spec = { | ||
| /* module level code ********************************************************/ | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| PyDoc_STRVAR(_functools_doc, | ||
| "Tools that operate on functions."); | ||
| @@ -1644,15 +1737,9 @@ _functools_exec(PyObject *module) | ||
| return -1; | ||
| } | ||
| if (PyModule_AddObject(module, "Placeholder", Py_GetPlaceholder()) < 0) { | ||
| return -1; | ||
| } | ||
| state->partial_type = (PyTypeObject *)PyType_FromModuleAndSpec(module, | ||
| &partial_type_spec, NULL); | ||
| if (state->partial_type == NULL) { | ||
| @@ -1697,7 +1784,6 @@ _functools_traverse(PyObject *module, visitproc visit, void *arg) | ||
| { | ||
| _functools_state *state = get_functools_state(module); | ||
| Py_VISIT(state->kwd_mark); | ||
| Py_VISIT(state->partial_type); | ||
| Py_VISIT(state->keyobject_type); | ||
| Py_VISIT(state->lru_list_elem_type); | ||
| @@ -1709,7 +1795,6 @@ _functools_clear(PyObject *module) | ||
| { | ||
| _functools_state *state = get_functools_state(module); | ||
| Py_CLEAR(state->kwd_mark); | ||
| Py_CLEAR(state->partial_type); | ||
| Py_CLEAR(state->keyobject_type); | ||
| Py_CLEAR(state->lru_list_elem_type); | ||