PEP 205, Weak References -- initial checkin. · pythoncapi/cpython@41deb1e · GitHub
Skip to content

Commit 41deb1e

Browse files
committed
PEP 205, Weak References -- initial checkin.
1 parent 2de7471 commit 41deb1e

9 files changed

Lines changed: 1158 additions & 4 deletions

File tree

Include/classobject.h

Lines changed: 1 addition & 0 deletions

Include/object.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ typedef struct _typeobject {
246246
/* rich comparisons */
247247
richcmpfunc tp_richcompare;
248248

249-
/* More spares */
250-
long tp_xxx8;
249+
/* weak reference enabler */
250+
long tp_weaklistoffset;
251251

252252
#ifdef COUNT_ALLOCS
253253
/* these must be last */
@@ -284,6 +284,8 @@ extern DL_IMPORT(int) PyCallable_Check(PyObject *);
284284
extern DL_IMPORT(int) PyNumber_Coerce(PyObject **, PyObject **);
285285
extern DL_IMPORT(int) PyNumber_CoerceEx(PyObject **, PyObject **);
286286

287+
extern DL_IMPORT(int) (*PyObject_ClearWeakRefs)(PyObject *);
288+
287289
/* Helpers for printing recursive container types */
288290
extern DL_IMPORT(int) Py_ReprEnter(PyObject *);
289291
extern DL_IMPORT(void) Py_ReprLeave(PyObject *);
@@ -418,7 +420,7 @@ extern DL_IMPORT(long) _Py_RefTotal;
418420

419421
#define Py_INCREF(op) (_Py_RefTotal++, (op)->ob_refcnt++)
420422
#define Py_DECREF(op) \
421-
if (--_Py_RefTotal, --(op)->ob_refcnt != 0) \
423+
if (--_Py_RefTotal, (--((op)->ob_refcnt) != 0)) \
422424
; \
423425
else \
424426
_Py_Dealloc((PyObject *)(op))

Include/objimpl.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ extern DL_IMPORT(void) _PyObject_Del(PyObject *);
160160
/* Macros trading binary compatibility for speed. See also pymem.h.
161161
Note that these macros expect non-NULL object pointers.*/
162162
#define PyObject_INIT(op, typeobj) \
163-
( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
163+
((op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), \
164+
(PyType_SUPPORTS_WEAKREFS((typeobj)) \
165+
? *(PyObject_GET_WEAKREFS_LISTPTR(op)) = NULL \
166+
: NULL), \
167+
(op))
164168
#define PyObject_INIT_VAR(op, typeobj, size) \
165169
( (op)->ob_size = (size), PyObject_INIT((op), (typeobj)) )
166170

@@ -266,6 +270,12 @@ extern DL_IMPORT(void) _PyGC_Dump(PyGC_Head *);
266270

267271
#endif /* WITH_CYCLE_GC */
268272

273+
/* Test if a type supports weak references */
274+
#define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
275+
276+
#define PyObject_GET_WEAKREFS_LISTPTR(o) \
277+
((PyObject **) (((char *) (o)) + (o)->ob_type->tp_weaklistoffset))
278+
269279
#ifdef __cplusplus
270280
}
271281
#endif

Lib/test/output/test_weakref

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
test_weakref
2+
Basic Weak References
3+
-- Liveness and referent identity
4+
-- Reference objects with callbacks
5+
-- Proxy objects with callbacks
6+
-- Re-use of weak reference objects
7+
reference objects
8+
proxy objects
9+
clearing ref 2
10+
clearing ref 1
11+
clearing ref 2
12+
clearing ref 1
13+
14+
Weak Valued Dictionaries
15+
objects are stored in weak dict
16+
weak dict test complete
17+
18+
Non-callable Proxy References
19+
XXX -- tests not written!
20+
21+
Callable Proxy References

Lib/test/test_weakref.py

Lines changed: 218 additions & 0 deletions

0 commit comments

Comments
 (0)