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 |
|---|---|---|
| @@ -352,13 +352,9 @@ def test_setstate(self): | ||
| self.assertEqual(f(), ((), {})) | ||
| # Set State with placeholders | ||
| PH = self.module.Placeholder | ||
| f = self.partial(signature) | ||
| f.__setstate__((capture, (PH, 1), dict(a=10), dict(attr=[]))) | ||
| self.assertEqual(signature(f), (capture, (PH, 1), dict(a=10), dict(attr=[]))) | ||
| with self.assertRaises(TypeError): | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| self.assertEqual(f(), (PH, 1), dict(a=10)) | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -192,7 +192,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw) | ||
| pto->fn = Py_NewRef(func); | ||
| pto->placeholder = state->placeholder; | ||
| if (new_nargs &&Py_Is(PyTuple_GET_ITEM(args, new_nargs), pto->placeholder)) { | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "trailing Placeholders are not allowed"); | ||
| return NULL; | ||
| @@ -204,15 +204,11 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw) | ||
| return NULL; | ||
| } | ||
| /* Count placeholders */ | ||
| Py_ssize_t phcount = 0; | ||
| for (Py_ssize_t i = 0; i < new_nargs - 1; i++) { | ||
| if (Py_Is(PyTuple_GET_ITEM(new_args, i), pto->placeholder)) { | ||
| phcount++; | ||
| } | ||
| } | ||
| /* merge args with args of `func` which is `partial` */ | ||
| @@ -222,6 +218,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw) | ||
| if (new_nargs > pto_phcount) { | ||
| tot_nargs += new_nargs - pto_phcount; | ||
| } | ||
| PyObject *item; | ||
| PyObject *tot_args = PyTuple_New(tot_nargs); | ||
| for (Py_ssize_t i = 0, j = 0; i < tot_nargs; i++) { | ||
| if (i < npargs) { | ||
| @@ -635,37 +632,27 @@ partial_repr(partialobject *pto) | ||
| static PyObject * | ||
| partial_reduce(partialobject *pto, PyObject *unused) | ||
| { | ||
| return Py_BuildValue("O(O)(OOOO)", Py_TYPE(pto), pto->fn, pto->fn, | ||
| pto->args, pto->kw, | ||
| pto->dict ? pto->dict : Py_None); | ||
| } | ||
| static PyObject * | ||
| partial_setstate(partialobject *pto, PyObject *state) | ||
| { | ||
| PyObject *fn, *fnargs, *kw, *dict; | ||
| if (!PyTuple_Check(state)) { | ||
| PyErr_SetString(PyExc_TypeError, "invalid partial state"); | ||
| return NULL; | ||
| } | ||
| Py_ssize_t state_len = PyTuple_GET_SIZE(state); | ||
| if (state_len != 4) { | ||
| PyErr_Format(PyExc_TypeError, | ||
| "expected 4 items in state, got %zd", state_len); | ||
| return NULL; | ||
| } | ||
| if (!PyArg_ParseTuple(state, "OOOO", &fn, &fnargs, &kw, &dict) || | ||
| !PyCallable_Check(fn) || | ||
| !PyTuple_Check(fnargs) || | ||
| (kw != Py_None && !PyDict_Check(kw))) | ||
| @@ -674,6 +661,20 @@ partial_setstate(partialobject *pto, PyObject *state) | ||
| return NULL; | ||
| } | ||
| Py_ssize_t nargs = PyTuple_GET_SIZE(fnargs); | ||
| if (nargs && Py_Is(PyTuple_GET_ITEM(fnargs, nargs - 1), pto->placeholder)) { | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "trailing Placeholders are not allowed"); | ||
| return NULL; | ||
| } | ||
| /* Count placeholders */ | ||
| Py_ssize_t phcount = 0; | ||
| for (Py_ssize_t i = 0; i < nargs - 1; i++) { | ||
| if (Py_Is(PyTuple_GET_ITEM(fnargs, i), pto->placeholder)) { | ||
| phcount++; | ||
| } | ||
| } | ||
| if(!PyTuple_CheckExact(fnargs)) | ||
| fnargs = PySequence_Tuple(fnargs); | ||
| else | ||