Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e262a9

Browse files
committed
A small change to the C API for weakly-referencable types: Such types
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).
1 parent 82f1480 commit 4e262a9

3 files changed

Lines changed: 8 additions & 5 deletions

File tree

Include/objimpl.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,7 @@ extern DL_IMPORT(void) _PyObject_Del(PyObject *);
167167
/* Macros trading binary compatibility for speed. See also pymem.h.
168168
Note that these macros expect non-NULL object pointers.*/
169169
#define PyObject_INIT(op, typeobj) \
170-
((op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), \
171-
(PyType_SUPPORTS_WEAKREFS((typeobj)) \
172-
? *(PyObject_GET_WEAKREFS_LISTPTR(op)) = NULL \
173-
: NULL), \
174-
(op))
170+
( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
175171
#define PyObject_INIT_VAR(op, typeobj, size) \
176172
( (op)->ob_size = (size), PyObject_INIT((op), (typeobj)) )
177173

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ Python/C API
6161
- Py_BuildValue() now has a "D" conversion to create a Python complex
6262
number from a Py_complex C value.
6363

64+
- Extensions types which support weak references must now set the
65+
field allocated for the weak reference machinery to NULL themselves;
66+
this is done to avoid the cost of checking each object for having a
67+
weakly referencable type in PyObject_INIT(), since most types are
68+
not weakly referencable.
69+
6470
Distutils
6571

6672
- the sdist command now writes a PKG-INFO file, as described in PEP 241,

Objects/classobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ PyInstance_NewRaw(PyObject *klass, PyObject *dict)
453453
Py_DECREF(dict);
454454
return NULL;
455455
}
456+
inst->in_weakreflist = NULL;
456457
Py_INCREF(klass);
457458
inst->in_class = (PyClassObject *)klass;
458459
inst->in_dict = dict;

0 commit comments

Comments
 (0)