A small change to the C API for weakly-referencable types: Such types
authorFred Drake <fdrake@acm.org>
Thu, 22 Mar 2001 18:26:47 +0000 (18:26 +0000)
committerFred Drake <fdrake@acm.org>
Thu, 22 Mar 2001 18:26:47 +0000 (18:26 +0000)
must now initialize the extra field used by the weak-ref machinery to
NULL themselves, to avoid having to require PyObject_INIT() to check
if the type supports weak references and do it there.  This causes less
work to be done for all objects (the type object does not need to be
consulted to check for the Py_TPFLAGS_HAVE_WEAKREFS bit).

Include/objimpl.h
Misc/NEWS
Objects/classobject.c

index 4197bde3e18bd6b0cd2da109bc9bf031530e6c67..4aa38d5673d4074b9f9633b0716003c329c8aa60 100644 (file)
@@ -167,11 +167,7 @@ extern DL_IMPORT(void) _PyObject_Del(PyObject *);
 /* Macros trading binary compatibility for speed. See also pymem.h.
    Note that these macros expect non-NULL object pointers.*/
 #define PyObject_INIT(op, typeobj) \
-       ((op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), \
-        (PyType_SUPPORTS_WEAKREFS((typeobj)) \
-             ? *(PyObject_GET_WEAKREFS_LISTPTR(op)) = NULL \
-              : NULL), \
-         (op))
+       ( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
 #define PyObject_INIT_VAR(op, typeobj, size) \
        ( (op)->ob_size = (size), PyObject_INIT((op), (typeobj)) )
 
index a1cf72a498316e2612c52c82dfada07c141f11d2..ac755a2ac7296eaec86fac8c6ee8a182de18d918 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -61,6 +61,12 @@ Python/C API
 - Py_BuildValue() now has a "D" conversion to create a Python complex
   number from a Py_complex C value.
 
+- Extensions types which support weak references must now set the
+  field allocated for the weak reference machinery to NULL themselves;
+  this is done to avoid the cost of checking each object for having a
+  weakly referencable type in PyObject_INIT(), since most types are
+  not weakly referencable.
+
 Distutils
 
 - the sdist command now writes a PKG-INFO file, as described in PEP 241,
index 00cfdde6272572f22bdc66dc7b66962df0abf0c7..d7b4c1e5a6c491dc40d47623bdf93366dc0482fb 100644 (file)
@@ -453,6 +453,7 @@ PyInstance_NewRaw(PyObject *klass, PyObject *dict)
                Py_DECREF(dict);
                return NULL;
        }
+       inst->in_weakreflist = NULL;
        Py_INCREF(klass);
        inst->in_class = (PyClassObject *)klass;
        inst->in_dict = dict;