[C-API] Weird sys.exc_info reference segfault

Antoine Pitrou solipsis at pitrou.net
Wed Oct 6 08:01:44 EDT 2010


On Tue, 05 Oct 2010 16:17:57 +0200
"Jonas H." <jonas at lophus.org> wrote:
> 
> Right now I have this minimal struct:
> 
> static PyTypeObject StartResponse_Type = {
>      PyObject_HEAD_INIT(&PyType_Type)
>      0,                          /* ob_size */
>      "start_response",           /* tp_name */
>      sizeof(StartResponse),      /* tp_basicsize */
>      0,                          /* tp_itemsize */
>      (destructor)PyObject_FREE,  /* tp_dealloc */
>      0, 0, 0, 0, 0, 0, 0, 0, 0,  /* tp_print, tp_{get,set}attr, stuff */
>      start_response              /* tp_call */
> };
> 
> I'm not sure about the `PyObject_HEAD_INIT` argument, but passing NULL 
> to it made `dir(obj)` crash. 

It shouldn't. Are you sure you're calling PyType_Ready in the module
initialization routine?

By the way, it is recommended to use at least Py_TPFLAGS_DEFAULT for
tp_flags.

> So does setting `GenericGetAttr` as 
> `tp_getattr`.

tp_getattr has the same signature as PyObject_GetAttrString. You're
looking for tp_getattro, which takes the attribute name as a PyObject *
rather than as a char *.

However, if you want your type to have a __dict__, what you need to do
is to have a "PyObject *dict" member (the name is not important, of
course), and initialize tp_dictoffset to offsetof(StartResponse, dict).
You don't have to set tp_getattr, PyType_Ready() should do the right
thing.

Regards

Antoine.





More information about the Python-list mailing list