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 |
|---|---|---|
| @@ -273,6 +273,11 @@ def reduce(function, sequence, initial=_initial_missing): | ||
| ### partial() argument application | ||
| ################################################################################ | ||
| class Placeholder: | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| """placeholder for partial arguments""" | ||
| # Purely functional, no descriptor behaviour | ||
| class partial: | ||
| """New function with partial application of the given arguments | ||
| @@ -285,8 +290,33 @@ def __new__(cls, func, /, *args, **keywords): | ||
| if not callable(func): | ||
| raise TypeError("the first argument must be callable") | ||
| np = 0 | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| nargs = len(args) | ||
| if args: | ||
| while nargs and args[nargs-1] is Placeholder: | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| nargs -= 1 | ||
| args = args[:nargs] | ||
| np = args.count(Placeholder) | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if isinstance(func, partial): | ||
| pargs = func.args | ||
| pnp = func.np | ||
| if pnp and args: | ||
| all_args = list(pargs) | ||
| nargs = len(args) | ||
| j, pos = 0, 0 | ||
| end = nargs if nargs < pnp else pnp | ||
| while j < end: | ||
| pos = all_args.index(Placeholder, pos) | ||
| all_args[pos] = args[j] | ||
| j += 1 | ||
| pos += 1 | ||
| if pnp < nargs: | ||
| all_args.extend(args[pnp:]) | ||
| np += pnp - end | ||
| args = tuple(all_args) | ||
| else: | ||
| np += pnp | ||
| args = func.args + args | ||
| keywords = {**func.keywords, **keywords} | ||
| func = func.func | ||
| @@ -295,11 +325,25 @@ def __new__(cls, func, /, *args, **keywords): | ||
| self.func = func | ||
| self.args = args | ||
| self.keywords = keywords | ||
| self.np = np | ||
| return self | ||
| def __call__(self, /, *args, **keywords): | ||
| if np := self.np: | ||
| if len(args) < np: | ||
| raise ValueError("unfilled placeholders in 'partial' call") | ||
| f_args = list(self.args) | ||
| j, pos = 0, 0 | ||
| while j < np: | ||
| pos = f_args.index(Placeholder, pos) | ||
| f_args[pos] = args[j] | ||
| j += 1 | ||
| pos += 1 | ||
| keywords = {**self.keywords, **keywords} | ||
| return self.func(*f_args, *args[np:], **keywords) | ||
| else: | ||
| keywords = {**self.keywords, **keywords} | ||
| return self.func(*self.args, *args, **keywords) | ||
| @recursive_repr() | ||
| def __repr__(self): | ||
| @@ -340,7 +384,7 @@ def __setstate__(self, state): | ||
| self.keywords = kwds | ||
| try: | ||
| from _functools import partial, Placeholder | ||
| except ImportError: | ||
| pass | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -41,13 +41,31 @@ get_functools_state(PyObject *module) | ||
| /* partial object **********************************************************/ | ||
| typedef struct { | ||
| PyObject_HEAD | ||
| } placeholderobject; | ||
| static PyTypeObject placeholder_type = { | ||
| .ob_base = PyVarObject_HEAD_INIT(NULL, 0) | ||
| .tp_name = "functools.Placeholder", | ||
| .tp_doc = PyDoc_STR("placeholder for partial arguments"), | ||
| .tp_basicsize = sizeof(placeholderobject), | ||
| .tp_itemsize = 0, | ||
| .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION, | ||
| .tp_new = PyType_GenericNew, | ||
| }; | ||
| typedef struct { | ||
| PyObject_HEAD | ||
| PyObject *fn; | ||
| PyObject *args; | ||
| PyObject *kw; | ||
| PyObject *dict; /* __dict__ */ | ||
| PyObject *weakreflist; /* List of weak references */ | ||
| Py_ssize_t np; /* Number of placeholders */ | ||
| vectorcallfunc vectorcall; | ||
| } partialobject; | ||
| @@ -72,6 +90,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw) | ||
| { | ||
| PyObject *func, *pargs, *nargs, *pkw; | ||
| partialobject *pto; | ||
| Py_ssize_t pnp = 0; | ||
| if (PyTuple_GET_SIZE(args) < 1) { | ||
| PyErr_SetString(PyExc_TypeError, | ||
| @@ -98,6 +117,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw) | ||
| pargs = part->args; | ||
| pkw = part->kw; | ||
| func = part->fn; | ||
| pnp = part->np; | ||
| assert(PyTuple_Check(pargs)); | ||
| assert(PyDict_Check(pkw)); | ||
| } | ||
| @@ -120,11 +140,57 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw) | ||
| Py_DECREF(pto); | ||
| return NULL; | ||
| } | ||
| Py_ssize_t nnp = 0; | ||
| Py_ssize_t nnargs = PyTuple_GET_SIZE(nargs); | ||
| PyObject *item; | ||
| if (nnargs > 0){ | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| Py_ssize_t i; | ||
| for (i=nnargs; i > 0; i--) { | ||
| item = PyTuple_GET_ITEM(nargs, i); | ||
| if (!Py_Is(item, (PyObject *) &placeholder_type)) | ||
| break; | ||
| } | ||
| if (i != nnargs) | ||
| nnargs = i; | ||
| if (nnargs > 0){ | ||
| for (Py_ssize_t i=0; i < nnargs; i++){ | ||
| item = PyTuple_GET_ITEM(nargs, i); | ||
| nnp += Py_Is(item, (PyObject *) &placeholder_type); | ||
| } | ||
| } | ||
| } | ||
| if ((pnp > 0) & (nnargs > 0)) { | ||
| Py_ssize_t pnargs = PyTuple_GET_SIZE(pargs); | ||
| Py_ssize_t anargs = pnargs; | ||
| if (nnargs > pnp) | ||
| anargs += nnargs - pnp; | ||
| PyObject *aargs = PyTuple_New(anargs); | ||
| Py_ssize_t j = 0; | ||
| for (Py_ssize_t i=0; i < anargs; i++) { | ||
| if (i < pnargs) { | ||
| item = PyTuple_GET_ITEM(pargs, i); | ||
| if ((j < nnargs) & Py_Is(item, (PyObject *) &placeholder_type)){ | ||
| item = PyTuple_GET_ITEM(nargs, j); | ||
| j++; | ||
| pnp--; | ||
| } | ||
| } else { | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| item = PyTuple_GET_ITEM(nargs, j); | ||
| j++; | ||
| } | ||
| Py_INCREF(item); | ||
| PyTuple_SET_ITEM(aargs, i, item); | ||
| } | ||
| pto->args = aargs; | ||
| pto->np = pnp + nnp; | ||
| Py_DECREF(nargs); | ||
| } else if (pargs == NULL) { | ||
| pto->args = nargs; | ||
| pto->np = nnp; | ||
| } else { | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| pto->args = PySequence_Concat(pargs, nargs); | ||
| pto->np = pnp + nnp; | ||
| Py_DECREF(nargs); | ||
| if (pto->args == NULL) { | ||
| Py_DECREF(pto); | ||
| @@ -217,13 +283,19 @@ partial_vectorcall(partialobject *pto, PyObject *const *args, | ||
| size_t nargsf, PyObject *kwnames) | ||
| { | ||
| PyThreadState *tstate = _PyThreadState_GET(); | ||
| Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); | ||
| /* pto->kw is mutable, so need to check every time */ | ||
| if (PyDict_GET_SIZE(pto->kw)) { | ||
| return partial_vectorcall_fallback(tstate, pto, args, nargsf, kwnames); | ||
| } | ||
| Py_ssize_t np = pto->np; | ||
| if (nargs < np) { | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "unfilled placeholders in 'partial' call"); | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| return NULL; | ||
| } | ||
| Py_ssize_t nargs_total = nargs; | ||
| if (kwnames != NULL) { | ||
| nargs_total += PyTuple_GET_SIZE(kwnames); | ||
| @@ -251,6 +323,7 @@ partial_vectorcall(partialobject *pto, PyObject *const *args, | ||
| } | ||
| Py_ssize_t newnargs_total = pto_nargs + nargs_total; | ||
| newnargs_total = newnargs_total - np; | ||
| PyObject *small_stack[_PY_FASTCALL_SMALL_STACK]; | ||
| PyObject *ret; | ||
| @@ -267,12 +340,28 @@ partial_vectorcall(partialobject *pto, PyObject *const *args, | ||
| } | ||
| } | ||
| Py_ssize_t nargs_new; | ||
| if (np) { | ||
| nargs_new = pto_nargs + nargs - np; | ||
| Py_ssize_t j = 0; // Placeholder counter | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| for (Py_ssize_t i=0; i < pto_nargs; i++) { | ||
| if (Py_Is(pto_args[i], (PyObject *) &placeholder_type)){ | ||
| memcpy(stack + i, args + j, 1 * sizeof(PyObject*)); | ||
| j += 1; | ||
| } else { | ||
| memcpy(stack + i, pto_args + i, 1 * sizeof(PyObject*)); | ||
| } | ||
| } | ||
| if (nargs_total > np){ | ||
| memcpy(stack + pto_nargs, args + np, (nargs_total - np) * sizeof(PyObject*)); | ||
| } | ||
| } else { | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| nargs_new = pto_nargs + nargs; | ||
| /* Copy to new stack, using borrowed references */ | ||
| memcpy(stack, pto_args, pto_nargs * sizeof(PyObject*)); | ||
| memcpy(stack + pto_nargs, args, nargs_total * sizeof(PyObject*)); | ||
| } | ||
| ret = _PyObject_VectorcallTstate(tstate, pto->fn, stack, nargs_new, kwnames); | ||
| if (stack != small_stack) { | ||
| PyMem_Free(stack); | ||
| } | ||
| @@ -304,6 +393,14 @@ partial_call(partialobject *pto, PyObject *args, PyObject *kwargs) | ||
| assert(PyTuple_Check(pto->args)); | ||
| assert(PyDict_Check(pto->kw)); | ||
| Py_ssize_t nargs = PyTuple_GET_SIZE(args); | ||
| Py_ssize_t np = pto->np; | ||
| if (nargs < np) { | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "unfilled placeholders in 'partial' call"); | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| return NULL; | ||
| } | ||
| /* Merge keywords */ | ||
| PyObject *kwargs2; | ||
| if (PyDict_GET_SIZE(pto->kw) == 0) { | ||
| @@ -328,8 +425,33 @@ partial_call(partialobject *pto, PyObject *args, PyObject *kwargs) | ||
| } | ||
| /* Merge positional arguments */ | ||
| PyObject *args2; | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if (np) { | ||
| Py_ssize_t pto_nargs = PyTuple_GET_SIZE(pto->args); | ||
| Py_ssize_t nargs_new = pto_nargs + nargs - np; | ||
| args2 = PyTuple_New(nargs_new); | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| PyObject *pto_args = pto->args; | ||
| PyObject *item; | ||
| Py_ssize_t j = 0; // Placeholder counter | ||
| for (Py_ssize_t i=0; i < pto_nargs; i++) { | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| item = PyTuple_GET_ITEM(pto_args, i); | ||
| if (Py_Is(item, (PyObject *) &placeholder_type)){ | ||
| item = PyTuple_GET_ITEM(args, j); | ||
| j += 1; | ||
| } | ||
| PyTuple_SET_ITEM(args2, i, item); | ||
| } | ||
| if (nargs > np){ | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| for (Py_ssize_t i=pto_nargs; i < nargs_new; i++) { | ||
| item = PyTuple_GET_ITEM(args, j); | ||
| PyTuple_SET_ITEM(args2, i, item); | ||
| j += 1; | ||
| } | ||
| } | ||
| } else { | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| /* Note: tupleconcat() is optimized for empty tuples */ | ||
| args2 = PySequence_Concat(pto->args, args); | ||
| } | ||
| if (args2 == NULL) { | ||
| Py_XDECREF(kwargs2); | ||
| return NULL; | ||
| @@ -354,6 +476,8 @@ static PyMemberDef partial_memberlist[] = { | ||
| "tuple of arguments to future partial calls"}, | ||
| {"keywords", _Py_T_OBJECT, OFF(kw), Py_READONLY, | ||
| "dictionary of keyword arguments to future partial calls"}, | ||
| {"np", Py_T_PYSSIZET, OFF(np), Py_READONLY, | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| "number of placeholders"}, | ||
| {"__weaklistoffset__", Py_T_PYSSIZET, | ||
| offsetof(partialobject, weakreflist), Py_READONLY}, | ||
| {"__dictoffset__", Py_T_PYSSIZET, | ||
| @@ -1497,6 +1621,9 @@ _functools_exec(PyObject *module) | ||
| if (PyModule_AddType(module, state->partial_type) < 0) { | ||
| return -1; | ||
| } | ||
| if (PyModule_AddType(module, &placeholder_type) < 0) { | ||
| return -1; | ||
| } | ||
| PyObject *lru_cache_type = PyType_FromModuleAndSpec(module, | ||
| &lru_cache_type_spec, NULL); | ||