diff options
author | Amaury Forgeot d'Arc <amauryfa@gmail.com> | 2008-07-02 20:50:16 (GMT) |
---|---|---|
committer | Amaury Forgeot d'Arc <amauryfa@gmail.com> | 2008-07-02 20:50:16 (GMT) |
commit | e43d33a4db0c0c9afcb70a26f682abfe889e845b (patch) | |
tree | 47197bd06fa88bf96cef5f673018d20887fa301c /PC | |
parent | 4118174315f4cba03208886af868fe31f1cd5b9d (diff) | |
download | cpython-e43d33a4db0c0c9afcb70a26f682abfe889e845b.zip cpython-e43d33a4db0c0c9afcb70a26f682abfe889e845b.tar.gz cpython-e43d33a4db0c0c9afcb70a26f682abfe889e845b.tar.bz2 |
#3247 Get rid of Py_FindMethod; use tp_members instead.
Otherwise dir(_sre.SRE_Match) returns an empty list.
First step: handle most occurrences, remove tp_getattr and fill the tp_methods and tp_members slots.
Add some test about attribute access.
Diffstat (limited to 'PC')
-rw-r--r-- | PC/_subprocess.c | 27 | ||||
-rw-r--r-- | PC/winreg.c | 64 |
2 files changed, 46 insertions, 45 deletions
diff --git a/PC/_subprocess.c b/PC/_subprocess.c index c256ca3..77a8a85 100644 --- a/PC/_subprocess.c +++ b/PC/_subprocess.c @@ -111,12 +111,6 @@ static PyMethodDef sp_handle_methods[] = { }; static PyObject* -sp_handle_getattr(sp_handle_object* self, char* name) -{ - return Py_FindMethod(sp_handle_methods, (PyObject*) self, name); -} - -static PyObject* sp_handle_as_int(sp_handle_object* self) { return PyLong_FromLong((long) self->handle); @@ -129,14 +123,28 @@ static PyTypeObject sp_handle_type = { "_subprocess_handle", sizeof(sp_handle_object), 0, (destructor) sp_handle_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ - (getattrfunc) sp_handle_getattr,/*tp_getattr*/ + 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ 0, /*tp_repr*/ &sp_handle_as_number, /*tp_as_number */ 0, /*tp_as_sequence */ 0, /*tp_as_mapping */ - 0 /*tp_hash*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + 0, /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + sp_handle_methods, /*tp_methods*/ }; /* -------------------------------------------------------------------- */ @@ -560,8 +568,9 @@ PyInit__subprocess() PyObject *m; /* patch up object descriptors */ - Py_TYPE(&sp_handle_type) = &PyType_Type; sp_handle_as_number.nb_int = (unaryfunc) sp_handle_as_int; + if (PyType_Ready(&sp_handle_type) < 0) + return NULL; m = PyModule_Create(&_subprocessmodule); if (m == NULL) diff --git a/PC/winreg.c b/PC/winreg.c index 571209f..e53a448 100644 --- a/PC/winreg.c +++ b/PC/winreg.c @@ -455,9 +455,24 @@ static PyNumberMethods PyHKEY_NumberMethods = PyHKEY_unaryFailureFunc, /* nb_float */ }; +static PyObject *PyHKEY_CloseMethod(PyObject *self, PyObject *args); +static PyObject *PyHKEY_DetachMethod(PyObject *self, PyObject *args); +static PyObject *PyHKEY_Enter(PyObject *self); +static PyObject *PyHKEY_Exit(PyObject *self, PyObject *args); -/* fwd declare __getattr__ */ -static PyObject *PyHKEY_getattr(PyObject *self, const char *name); +static struct PyMethodDef PyHKEY_methods[] = { + {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc}, + {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc}, + {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL}, + {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL}, + {NULL} +}; + +#define OFF(e) offsetof(PyHKEYObject, e) +static PyMemberDef PyHKEY_memberlist[] = { + {"handle", T_INT, OFF(hkey), READONLY}, + {NULL} /* Sentinel */ +}; /* The type itself */ PyTypeObject PyHKEY_Type = @@ -468,7 +483,7 @@ PyTypeObject PyHKEY_Type = 0, PyHKEY_deallocFunc, /* tp_dealloc */ 0, /* tp_print */ - PyHKEY_getattr, /* tp_getattr */ + 0, /* tp_getattr */ 0, /* tp_setattr */ PyHKEY_compareFunc, /* tp_compare */ 0, /* tp_repr */ @@ -483,13 +498,14 @@ PyTypeObject PyHKEY_Type = 0, /* tp_as_buffer */ 0, /* tp_flags */ PyHKEY_doc, /* tp_doc */ -}; - -#define OFF(e) offsetof(PyHKEYObject, e) - -static PyMemberDef PyHKEY_memberlist[] = { - {"handle", T_INT, OFF(hkey), READONLY}, - {NULL} /* Sentinel */ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + PyHKEY_methods, /*tp_methods*/ + PyHKEY_memberlist, /*tp_members*/ }; /************************************************************************ @@ -536,31 +552,6 @@ PyHKEY_Exit(PyObject *self, PyObject *args) } -static struct PyMethodDef PyHKEY_methods[] = { - {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc}, - {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc}, - {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL}, - {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL}, - {NULL} -}; - -/*static*/ PyObject * -PyHKEY_getattr(PyObject *self, const char *name) -{ - PyObject *res; - - res = Py_FindMethod(PyHKEY_methods, self, name); - if (res != NULL) - return res; - PyErr_Clear(); - if (strcmp(name, "handle") == 0) - return PyLong_FromVoidPtr(((PyHKEYObject *)self)->hkey); - PyErr_Format(PyExc_AttributeError, - "'%.50s' object has no attribute '%.400s'", - Py_TYPE(self)->tp_name, name); - return NULL; -} - /************************************************************************ The public PyHKEY API (well, not public yet :-) ************************************************************************/ @@ -1582,8 +1573,9 @@ PyMODINIT_FUNC PyInit_winreg(void) if (m == NULL) return NULL; d = PyModule_GetDict(m); - Py_TYPE(&PyHKEY_Type) = &PyType_Type; PyHKEY_Type.tp_doc = PyHKEY_doc; + if (PyType_Ready(&PyHKEY_Type) < 0) + return NULL; Py_INCREF(&PyHKEY_Type); if (PyDict_SetItemString(d, "HKEYType", (PyObject *)&PyHKEY_Type) != 0) |