From f7f11ac27261f78df12bdd9696a4d9bb5866a137 Mon Sep 17 00:00:00 2001 From: stephan-hof Date: Sat, 18 Feb 2017 16:44:00 +0100 Subject: [PATCH] Refactor acquire_of. Instead of two functiond doing nearly the same, create a now one with the Wrapper class to create as a parameter. Since only a single argument is needed use METH_O instead of VARARGS. Now the argument parsing inside the function can be omited, because python does this for us. --- src/Acquisition/_Acquisition.c | 66 +++++++++++++++------------------- 1 file changed, 28 insertions(+), 38 deletions(-) diff --git a/src/Acquisition/_Acquisition.c b/src/Acquisition/_Acquisition.c index 815df58..4414520 100644 --- a/src/Acquisition/_Acquisition.c +++ b/src/Acquisition/_Acquisition.c @@ -1582,52 +1582,42 @@ static PyExtensionClass XaqWrappertype = { }; static PyObject * -acquire_of(PyObject *self, PyObject *args) +acquire_of(PyObject *self, PyObject *inst, PyExtensionClass *target) { - PyObject *inst; - - UNLESS(PyArg_ParseTuple(args, "O", &inst)) return NULL; - - UNLESS(PyExtensionInstance_Check(inst)) - { - PyErr_SetString(PyExc_TypeError, - "attempt to wrap extension method using an object that\n" - "is not an extension class instance."); - return NULL; + if (!PyExtensionInstance_Check(inst)) { + PyErr_SetString(PyExc_TypeError, + "attempt to wrap extension method using an object that" + " is not an extension class instance."); + return NULL; } - return newWrapper(self, inst, (PyTypeObject *)&Wrappertype); -} + return newWrapper(self, inst, target); +} static PyObject * -xaq_of(PyObject *self, PyObject *args) +aq__of__(PyObject *self, PyObject *inst) { - PyObject *inst; - - UNLESS(PyArg_ParseTuple(args, "O", &inst)) return NULL; - - UNLESS(PyExtensionInstance_Check(inst)) - { - PyErr_SetString(PyExc_TypeError, - "attempt to wrap extension method using an object that\n" - "is not an extension class instance."); - return NULL; - } + return acquire_of(self, inst, &Wrappertype); +} - return newWrapper(self, inst, (PyTypeObject *)&XaqWrappertype); +static PyObject * +xaq__of__(PyObject *self, PyObject *inst) +{ + return acquire_of(self, inst, &XaqWrappertype); } static struct PyMethodDef Acquirer_methods[] = { - {"__of__",(PyCFunction)acquire_of, METH_VARARGS, - "__of__(context) -- return the object in a context"}, - - {NULL, NULL} /* sentinel */ + {"__of__",(PyCFunction)aq__of__, METH_O, + "__of__(context) -- return the object in a context"}, + + {NULL, NULL} }; -static struct PyMethodDef Xaq_methods[] = { - {"__of__",(PyCFunction)xaq_of, METH_VARARGS,""}, - - {NULL, NULL} /* sentinel */ +static struct PyMethodDef ExplicitAcquirer_methods[] = { + {"__of__",(PyCFunction)xaq__of__, METH_O, + "__of__(context) -- return the object in a context"}, + + {NULL, NULL} }; static PyObject * @@ -2087,13 +2077,13 @@ module_init(void) PURE_MIXIN_CLASS(Acquirer, "Base class for objects that implicitly" - " acquire attributes from containers\n" - , Acquirer_methods); + " acquire attributes from containers\n", + Acquirer_methods); PURE_MIXIN_CLASS(ExplicitAcquirer, "Base class for objects that explicitly" - " acquire attributes from containers\n" - , Xaq_methods); + " acquire attributes from containers\n", + ExplicitAcquirer_methods); UNLESS(ExtensionClassImported) return NULL;