[Python-bugs-list] [ python-Bugs-586583 ] Two corrects for weakref docs

noreply@sourceforge.net noreply@sourceforge.net
Thu, 25 Jul 2002 09:59:20 -0700


Bugs item #586583, was opened at 2002-07-25 16:59
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=586583&group_id=5470

Category: Documentation
Group: Python 2.2.1
Status: Open
Resolution: None
Priority: 5
Submitted By: Stephan R.A. Deibel (sdeibel)
Assigned to: Fred L. Drake, Jr. (fdrake)
Summary: Two corrects for weakref docs

Initial Comment:
The Python manual (I'm looking at 2.2.1, but I think
it's true since 2.1) omits one detail for creating weak
references on extension types:

It fails to indicate that the tp_flags field of the
PyTypeObject has to
have Py_TPFLAGS_HAVE_WEAKREFS added into it as well. 
Without this, it doesn't work.

So the second code block would read as follows:

PyTypeObject PyInstance_Type = {
    PyObject_HEAD_INIT(&PyType_Type)
    0,   
    "module.instance",

    /* Lots of stuff omitted for brevity... */

    Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
    NULL, /*doc*/
    0L,0L,0L,
    offsetof(PyInstanceObject, in_weakreflist) /*
tp_weaklistoffset */
  
    /* Possibly more stuff here... */
};
  
----------------------
 
You may also want to emphasize that the in_weakreflist
field that is added should be initialized in the object
constructor.  Since you show the code in
instance_dealloc(), it might make sense to show the
allocation function too, with the code that sets the
in_weakreflist field to NULL.

Yea, I know this should be obvious and it is mentioned
in the first
paragraph, but somehow I got confused into thinking it
would be initialize for me because there wasn't a
seperate code example.

Here is another code block that could be shown before
the instance_dealloc block, to make this clear for
dummies like me:

static PyObject *
instance_new() {

  /* Code removed for brevity */

  self = (PyInstance_Object
*)PyObject_NEW(PyInstance_Object,
                                          
&PyInstance_Type);
  if (self == NULL)
    return NULL;
  
  /* More code removed for brevity */
  
  self->in_weakreflist = NULL;

  return (PyObject *) self;
}

Hope that helps...

- Stephan

------------------------------------------------------------------------
 
Wing IDE for Python                         
Archaeopteryx Software, Inc
www.wingide.com                              Take Flight!



----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=586583&group_id=5470