Mercurial > cpython-lru_cache-weakref
annotate Include/weakrefobject.h @ 83447:678564fc0962 3.3
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 19 Apr 2013 05:45:27 +0300 |
| parents | 0748c22b37e5 |
| children | 1800107873c0 |
| rev | line source |
|---|---|
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
1 /* Weak references objects for Python. */ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
2 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
3 #ifndef Py_WEAKREFOBJECT_H |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
4 #define Py_WEAKREFOBJECT_H |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
5 #ifdef __cplusplus |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
6 extern "C" { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
7 #endif |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
8 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
9 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
10 typedef struct _PyWeakReference PyWeakReference; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
11 |
|
33862
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
12 /* PyWeakReference is the base struct for the Python ReferenceType, ProxyType, |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
13 * and CallableProxyType. |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
14 */ |
|
66664
62b61abd02b8
Merge branches/pep-0384.
Martin v. Löwis <martin@v.loewis.de>
parents:
65494
diff
changeset
|
15 #ifndef Py_LIMITED_API |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 struct _PyWeakReference { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
17 PyObject_HEAD |
|
33862
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
18 |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
19 /* The object to which this is a weak reference, or Py_None if none. |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
20 * Note that this is a stealth reference: wr_object's refcount is |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
21 * not incremented to reflect this pointer. |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
22 */ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
23 PyObject *wr_object; |
|
33862
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
24 |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
25 /* A callable to invoke when wr_object dies, or NULL if none. */ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
26 PyObject *wr_callback; |
|
33862
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
27 |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
28 /* A cache for wr_object's hash code. As usual for hashes, this is -1 |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
29 * if the hash code isn't known yet. |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
30 */ |
|
65494
e8969fb822fc
make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents:
44351
diff
changeset
|
31 Py_hash_t hash; |
|
33862
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
32 |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
33 /* If wr_object is weakly referenced, wr_object has a doubly-linked NULL- |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
34 * terminated list of weak references to it. These are the list pointers. |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
35 * If wr_object goes away, wr_object is set to Py_None, and these pointers |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
36 * have no meaning then. |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
37 */ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
38 PyWeakReference *wr_prev; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
39 PyWeakReference *wr_next; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
40 }; |
|
66664
62b61abd02b8
Merge branches/pep-0384.
Martin v. Löwis <martin@v.loewis.de>
parents:
65494
diff
changeset
|
41 #endif |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
42 |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
20287
diff
changeset
|
43 PyAPI_DATA(PyTypeObject) _PyWeakref_RefType; |
|
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
20287
diff
changeset
|
44 PyAPI_DATA(PyTypeObject) _PyWeakref_ProxyType; |
|
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
20287
diff
changeset
|
45 PyAPI_DATA(PyTypeObject) _PyWeakref_CallableProxyType; |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
46 |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
30991
diff
changeset
|
47 #define PyWeakref_CheckRef(op) PyObject_TypeCheck(op, &_PyWeakref_RefType) |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
30991
diff
changeset
|
48 #define PyWeakref_CheckRefExact(op) \ |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
42334
diff
changeset
|
49 (Py_TYPE(op) == &_PyWeakref_RefType) |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
50 #define PyWeakref_CheckProxy(op) \ |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
42334
diff
changeset
|
51 ((Py_TYPE(op) == &_PyWeakref_ProxyType) || \ |
|
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
42334
diff
changeset
|
52 (Py_TYPE(op) == &_PyWeakref_CallableProxyType)) |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
30991
diff
changeset
|
53 |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
30991
diff
changeset
|
54 /* This macro calls PyWeakref_CheckRef() last since that can involve a |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
30991
diff
changeset
|
55 function call; this makes it more likely that the function call |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
30991
diff
changeset
|
56 will be avoided. */ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
57 #define PyWeakref_Check(op) \ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
58 (PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op)) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
59 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
60 |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
20287
diff
changeset
|
61 PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob, |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
62 PyObject *callback); |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
20287
diff
changeset
|
63 PyAPI_FUNC(PyObject *) PyWeakref_NewProxy(PyObject *ob, |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
64 PyObject *callback); |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
20287
diff
changeset
|
65 PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref); |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
66 |
|
66664
62b61abd02b8
Merge branches/pep-0384.
Martin v. Löwis <martin@v.loewis.de>
parents:
65494
diff
changeset
|
67 #ifndef Py_LIMITED_API |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
33862
diff
changeset
|
68 PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head); |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 |
|
30991
14d321466a06
SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents:
24752
diff
changeset
|
70 PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self); |
|
66664
62b61abd02b8
Merge branches/pep-0384.
Martin v. Löwis <martin@v.loewis.de>
parents:
65494
diff
changeset
|
71 #endif |
|
30991
14d321466a06
SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents:
24752
diff
changeset
|
72 |
|
80759
0748c22b37e5
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents:
66664
diff
changeset
|
73 /* Explanation for the Py_REFCNT() check: when a weakref's target is part |
|
0748c22b37e5
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents:
66664
diff
changeset
|
74 of a long chain of deallocations which triggers the trashcan mechanism, |
|
0748c22b37e5
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents:
66664
diff
changeset
|
75 clearing the weakrefs can be delayed long after the target's refcount |
|
0748c22b37e5
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents:
66664
diff
changeset
|
76 has dropped to zero. In the meantime, code accessing the weakref will |
|
0748c22b37e5
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents:
66664
diff
changeset
|
77 be able to "see" the target object even though it is supposed to be |
|
0748c22b37e5
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents:
66664
diff
changeset
|
78 unreachable. See issue #16602. */ |
|
0748c22b37e5
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents:
66664
diff
changeset
|
79 |
|
0748c22b37e5
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents:
66664
diff
changeset
|
80 #define PyWeakref_GET_OBJECT(ref) \ |
|
0748c22b37e5
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents:
66664
diff
changeset
|
81 (Py_REFCNT(((PyWeakReference *)(ref))->wr_object) > 0 \ |
|
0748c22b37e5
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents:
66664
diff
changeset
|
82 ? ((PyWeakReference *)(ref))->wr_object \ |
|
0748c22b37e5
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents:
66664
diff
changeset
|
83 : Py_None) |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
84 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
85 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
86 #ifdef __cplusplus |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
87 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
88 #endif |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
89 #endif /* !Py_WEAKREFOBJECT_H */ |
