Commit de96f31c authored by Stefan Behnel's avatar Stefan Behnel

support __signature__ property on CyFunction in Py3.4

parent ee6c58e1
......@@ -70,6 +70,8 @@ static int __Pyx_CyFunction_init(void);
//////////////////// CythonFunction ////////////////////
//@substitute: naming
//@requires: CommonTypes.c::FetchCommonType
//@requires: ObjectHandling.c::PyObjectCallMethod
//@requires: ObjectHandling.c::PyObjectGetAttrStr
static PyObject *
__Pyx_CyFunction_get_doc(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *closure)
......@@ -347,6 +349,31 @@ __Pyx_CyFunction_get_annotations(__pyx_CyFunctionObject *op) {
return result;
}
#if PY_VERSION_HEX >= 0x030400C1
static PyObject *
__Pyx_CyFunction_get_signature(__pyx_CyFunctionObject *op) {
PyObject *inspect_module, *signature_class, *signature;
// from inspect import Signature
inspect_module = PyImport_ImportModuleLevelObject(PYIDENT("inspect"), NULL, NULL, NULL, 0);
if (unlikely(!inspect_module))
goto bad;
signature_class = __Pyx_PyObject_GetAttrStr(inspect_module, PYIDENT("Signature"));
Py_DECREF(inspect_module);
if (unlikely(!signature_class))
goto bad;
// return Signature.from_function(op)
signature = __Pyx_PyObject_CallMethod1(signature_class, PYIDENT("from_function"), op);
Py_DECREF(signature_class);
if (likely(signature))
return signature;
bad:
// make sure we raise an AttributeError from this property on any errors
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_SetString(PyExc_AttributeError, "failed to calculate __signature__");
return NULL;
}
#endif
static PyGetSetDef __pyx_CyFunction_getsets[] = {
{(char *) "func_doc", (getter)__Pyx_CyFunction_get_doc, (setter)__Pyx_CyFunction_set_doc, 0, 0},
{(char *) "__doc__", (getter)__Pyx_CyFunction_get_doc, (setter)__Pyx_CyFunction_set_doc, 0, 0},
......@@ -366,6 +393,9 @@ static PyGetSetDef __pyx_CyFunction_getsets[] = {
{(char *) "__defaults__", (getter)__Pyx_CyFunction_get_defaults, (setter)__Pyx_CyFunction_set_defaults, 0, 0},
{(char *) "__kwdefaults__", (getter)__Pyx_CyFunction_get_kwdefaults, (setter)__Pyx_CyFunction_set_kwdefaults, 0, 0},
{(char *) "__annotations__", (getter)__Pyx_CyFunction_get_annotations, (setter)__Pyx_CyFunction_set_annotations, 0, 0},
#if PY_VERSION_HEX >= 0x030400C1
{(char *) "__signature__", (getter)__Pyx_CyFunction_get_signature, 0, 0, 0},
#endif
{0, 0, 0, 0, 0}
};
......
......@@ -55,6 +55,22 @@ def inspect_signature(a, b, c=123, *, d=234):
return inspect.signature(inspect_signature) if IS_PY34 else None
def test___signature__(a, b, c=123, *, d=234):
"""
>>> sig = test___signature__(1, 2)
>>> if IS_PY34: list(sig.parameters)
... else: ['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd']
>>> if IS_PY34: sig.parameters['c'].default == 123
... else: True
True
>>> if IS_PY34: sig.parameters['d'].default == 234
... else: True
True
"""
return inspect_signature.__signature__ if IS_PY34 else None
def test_dict():
"""
>>> test_dict.foo = 123
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment