diff options
author | Fred Drake <fdrake@acm.org> | 2001-02-01 05:27:45 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2001-02-01 05:27:45 (GMT) |
commit | 41deb1efc2f969b58e49af669cc20d15ccdb04c6 (patch) | |
tree | 6478ef737151fb4e091594b78f611318a15d3a31 /Objects | |
parent | 2de7471d69b950a64e52a950675d59d9f4071da1 (diff) | |
download | cpython-41deb1efc2f969b58e49af669cc20d15ccdb04c6.zip cpython-41deb1efc2f969b58e49af669cc20d15ccdb04c6.tar.gz cpython-41deb1efc2f969b58e49af669cc20d15ccdb04c6.tar.bz2 |
PEP 205, Weak References -- initial checkin.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/classobject.c | 5 | ||||
-rw-r--r-- | Objects/object.c | 23 |
2 files changed, 28 insertions, 0 deletions
diff --git a/Objects/classobject.c b/Objects/classobject.c index 218e031..bb17df1 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -515,6 +515,10 @@ instance_dealloc(register PyInstanceObject *inst) #ifdef Py_REF_DEBUG extern long _Py_RefTotal; #endif + + if (!PyObject_ClearWeakRefs((PyObject *) inst)) + return; + /* Temporarily resurrect the object. */ #ifdef Py_TRACE_REFS #ifndef Py_REF_DEBUG @@ -1771,6 +1775,7 @@ PyTypeObject PyInstance_Type = { (traverseproc)instance_traverse, /* tp_traverse */ 0, /* tp_clear */ instance_richcompare, /* tp_richcompare */ + offsetof(PyInstanceObject, in_weakreflist) /* tp_weaklistoffset */ }; diff --git a/Objects/object.c b/Objects/object.c index 40c939a..a4c9f08 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -100,6 +100,10 @@ PyObject_Init(PyObject *op, PyTypeObject *tp) /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ op->ob_type = tp; _Py_NewReference(op); + if (PyType_SUPPORTS_WEAKREFS(tp)) { + PyObject **weaklist = PyObject_GET_WEAKREFS_LISTPTR(op); + *weaklist = NULL; + } return op; } @@ -119,6 +123,10 @@ PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size) op->ob_size = size; op->ob_type = tp; _Py_NewReference((PyObject *)op); + if (PyType_SUPPORTS_WEAKREFS(tp)) { + PyObject **weaklist = PyObject_GET_WEAKREFS_LISTPTR(op); + *weaklist = NULL; + } return op; } @@ -1458,6 +1466,21 @@ PyObject_Free(void *p) } +/* Hook to clear up weak references only once the _weakref module is + imported. We use a dummy implementation to simplify the code at each + call site instead of requiring a test for NULL. +*/ + +static int +empty_clear_weak_refs(PyObject *o) +{ + return 1; +} + +int (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs; + + + /* These methods are used to control infinite recursion in repr, str, print, etc. Container objects that may recursively contain themselves, e.g. builtin dictionaries and lists, should used Py_ReprEnter() and |