diff options
Diffstat (limited to 'Objects/funcobject.c')
| -rw-r--r-- | Objects/funcobject.c | 126 |
1 files changed, 116 insertions, 10 deletions
diff --git a/Objects/funcobject.c b/Objects/funcobject.c index 2292a9e..089356b 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -6,7 +6,7 @@ #include "structmember.h" PyObject * -PyFunction_New(PyObject *code, PyObject *globals) +PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname) { PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type); @@ -54,6 +54,11 @@ PyFunction_New(PyObject *code, PyObject *globals) Py_INCREF(module); op->func_module = module; } + if (qualname) + op->func_qualname = qualname; + else + op->func_qualname = op->func_name; + Py_INCREF(op->func_qualname); } else return NULL; @@ -62,6 +67,12 @@ PyFunction_New(PyObject *code, PyObject *globals) } PyObject * +PyFunction_New(PyObject *code, PyObject *globals) +{ + return PyFunction_NewWithQualName(code, globals, NULL); +} + +PyObject * PyFunction_GetCode(PyObject *op) { if (!PyFunction_Check(op)) { @@ -334,6 +345,32 @@ func_set_name(PyFunctionObject *op, PyObject *value) } static PyObject * +func_get_qualname(PyFunctionObject *op) +{ + Py_INCREF(op->func_qualname); + return op->func_qualname; +} + +static int +func_set_qualname(PyFunctionObject *op, PyObject *value) +{ + PyObject *tmp; + + /* Not legal to del f.__qualname__ or to set it to anything + * other than a string object. */ + if (value == NULL || !PyUnicode_Check(value)) { + PyErr_SetString(PyExc_TypeError, + "__qualname__ must be set to a string object"); + return -1; + } + tmp = op->func_qualname; + Py_INCREF(value); + op->func_qualname = value; + Py_DECREF(tmp); + return 0; +} + +static PyObject * func_get_defaults(PyFunctionObject *op) { if (op->func_defaults == NULL) { @@ -441,6 +478,7 @@ static PyGetSetDef func_getsetlist[] = { (setter)func_set_annotations}, {"__dict__", (getter)func_get_dict, (setter)func_set_dict}, {"__name__", (getter)func_get_name, (setter)func_set_name}, + {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname}, {NULL} /* Sentinel */ }; @@ -561,6 +599,7 @@ func_dealloc(PyFunctionObject *op) Py_XDECREF(op->func_dict); Py_XDECREF(op->func_closure); Py_XDECREF(op->func_annotations); + Py_XDECREF(op->func_qualname); PyObject_GC_Del(op); } @@ -568,7 +607,7 @@ static PyObject* func_repr(PyFunctionObject *op) { return PyUnicode_FromFormat("<function %U at %p>", - op->func_name, op); + op->func_qualname, op); } static int @@ -584,6 +623,7 @@ func_traverse(PyFunctionObject *f, visitproc visit, void *arg) Py_VISIT(f->func_dict); Py_VISIT(f->func_closure); Py_VISIT(f->func_annotations); + Py_VISIT(f->func_qualname); return 0; } @@ -667,8 +707,8 @@ PyTypeObject PyFunction_Type = { 0, /* tp_hash */ function_call, /* tp_call */ 0, /* tp_str */ - PyObject_GenericGetAttr, /* tp_getattro */ - PyObject_GenericSetAttr, /* tp_setattro */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ func_doc, /* tp_doc */ @@ -714,6 +754,7 @@ PyTypeObject PyFunction_Type = { typedef struct { PyObject_HEAD PyObject *cm_callable; + PyObject *cm_dict; } classmethod; static void @@ -721,6 +762,7 @@ cm_dealloc(classmethod *cm) { _PyObject_GC_UNTRACK((PyObject *)cm); Py_XDECREF(cm->cm_callable); + Py_XDECREF(cm->cm_dict); Py_TYPE(cm)->tp_free((PyObject *)cm); } @@ -728,6 +770,7 @@ static int cm_traverse(classmethod *cm, visitproc visit, void *arg) { Py_VISIT(cm->cm_callable); + Py_VISIT(cm->cm_dict); return 0; } @@ -735,6 +778,7 @@ static int cm_clear(classmethod *cm) { Py_CLEAR(cm->cm_callable); + Py_CLEAR(cm->cm_dict); return 0; } @@ -774,6 +818,35 @@ static PyMemberDef cm_memberlist[] = { {NULL} /* Sentinel */ }; +static PyObject * +cm_get___isabstractmethod__(classmethod *cm, void *closure) +{ + int res = _PyObject_IsAbstract(cm->cm_callable); + if (res == -1) { + return NULL; + } + else if (res) { + Py_RETURN_TRUE; + } + Py_RETURN_FALSE; +} + +static PyObject * +cm_get___dict__(classmethod *cm, void *closure) +{ + Py_INCREF(cm->cm_dict); + return cm->cm_dict; +} + +static PyGetSetDef cm_getsetlist[] = { + {"__isabstractmethod__", + (getter)cm_get___isabstractmethod__, NULL, + NULL, + NULL}, + {"__dict__", (getter)cm_get___dict__, NULL, NULL, NULL}, + {NULL} /* Sentinel */ +}; + PyDoc_STRVAR(classmethod_doc, "classmethod(function) -> method\n\ \n\ @@ -812,7 +885,7 @@ PyTypeObject PyClassMethod_Type = { 0, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ - PyObject_GenericGetAttr, /* tp_getattro */ + 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, @@ -825,12 +898,12 @@ PyTypeObject PyClassMethod_Type = { 0, /* tp_iternext */ 0, /* tp_methods */ cm_memberlist, /* tp_members */ - 0, /* tp_getset */ + cm_getsetlist, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ cm_descr_get, /* tp_descr_get */ 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ + offsetof(classmethod, cm_dict), /* tp_dictoffset */ cm_init, /* tp_init */ PyType_GenericAlloc, /* tp_alloc */ PyType_GenericNew, /* tp_new */ @@ -869,6 +942,7 @@ PyClassMethod_New(PyObject *callable) typedef struct { PyObject_HEAD PyObject *sm_callable; + PyObject *sm_dict; } staticmethod; static void @@ -876,6 +950,7 @@ sm_dealloc(staticmethod *sm) { _PyObject_GC_UNTRACK((PyObject *)sm); Py_XDECREF(sm->sm_callable); + Py_XDECREF(sm->sm_dict); Py_TYPE(sm)->tp_free((PyObject *)sm); } @@ -883,6 +958,7 @@ static int sm_traverse(staticmethod *sm, visitproc visit, void *arg) { Py_VISIT(sm->sm_callable); + Py_VISIT(sm->sm_dict); return 0; } @@ -890,6 +966,7 @@ static int sm_clear(staticmethod *sm) { Py_CLEAR(sm->sm_callable); + Py_CLEAR(sm->sm_dict); return 0; } @@ -927,6 +1004,35 @@ static PyMemberDef sm_memberlist[] = { {NULL} /* Sentinel */ }; +static PyObject * +sm_get___isabstractmethod__(staticmethod *sm, void *closure) +{ + int res = _PyObject_IsAbstract(sm->sm_callable); + if (res == -1) { + return NULL; + } + else if (res) { + Py_RETURN_TRUE; + } + Py_RETURN_FALSE; +} + +static PyObject * +sm_get___dict__(staticmethod *sm, void *closure) +{ + Py_INCREF(sm->sm_dict); + return sm->sm_dict; +} + +static PyGetSetDef sm_getsetlist[] = { + {"__isabstractmethod__", + (getter)sm_get___isabstractmethod__, NULL, + NULL, + NULL}, + {"__dict__", (getter)sm_get___dict__, NULL, NULL, NULL}, + {NULL} /* Sentinel */ +}; + PyDoc_STRVAR(staticmethod_doc, "staticmethod(function) -> method\n\ \n\ @@ -962,7 +1068,7 @@ PyTypeObject PyStaticMethod_Type = { 0, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ - PyObject_GenericGetAttr, /* tp_getattro */ + 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, @@ -975,12 +1081,12 @@ PyTypeObject PyStaticMethod_Type = { 0, /* tp_iternext */ 0, /* tp_methods */ sm_memberlist, /* tp_members */ - 0, /* tp_getset */ + sm_getsetlist, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ sm_descr_get, /* tp_descr_get */ 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ + offsetof(staticmethod, sm_dict), /* tp_dictoffset */ sm_init, /* tp_init */ PyType_GenericAlloc, /* tp_alloc */ PyType_GenericNew, /* tp_new */ |
