Mercurial > cpython-lru_cache-weakref
annotate Include/weakrefobject.h @ 82251:bb5a8564e186 2.7
Issue #13153: Tkinter functions now raise TclError instead of ValueError when
a unicode argument contains non-BMP character.
| author | Serhiy Storchaka <storchaka@gmail.com> |
|---|---|
| date | Mon, 18 Feb 2013 13:00:08 +0200 |
| parents | 7e771f0363e2 |
| children | 39e5ab118602 |
| 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 */ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
15 struct _PyWeakReference { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 PyObject_HEAD |
|
33862
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
17 |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
18 /* 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
|
19 * 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
|
20 * 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
|
21 */ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
22 PyObject *wr_object; |
|
33862
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
23 |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
24 /* 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
|
25 PyObject *wr_callback; |
|
33862
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
26 |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
27 /* 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
|
28 * 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
|
29 */ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
30 long hash; |
|
33862
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
31 |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
32 /* 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
|
33 * 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
|
34 * 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
|
35 * have no meaning then. |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32472
diff
changeset
|
36 */ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
37 PyWeakReference *wr_prev; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
38 PyWeakReference *wr_next; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
39 }; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
40 |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
20287
diff
changeset
|
41 PyAPI_DATA(PyTypeObject) _PyWeakref_RefType; |
|
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
20287
diff
changeset
|
42 PyAPI_DATA(PyTypeObject) _PyWeakref_ProxyType; |
|
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
20287
diff
changeset
|
43 PyAPI_DATA(PyTypeObject) _PyWeakref_CallableProxyType; |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
44 |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
30991
diff
changeset
|
45 #define PyWeakref_CheckRef(op) PyObject_TypeCheck(op, &_PyWeakref_RefType) |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
30991
diff
changeset
|
46 #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
|
47 (Py_TYPE(op) == &_PyWeakref_RefType) |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
48 #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
|
49 ((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
|
50 (Py_TYPE(op) == &_PyWeakref_CallableProxyType)) |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
30991
diff
changeset
|
51 |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
30991
diff
changeset
|
52 /* 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
|
53 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
|
54 will be avoided. */ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
55 #define PyWeakref_Check(op) \ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
56 (PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op)) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
57 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
58 |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
20287
diff
changeset
|
59 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
|
60 PyObject *callback); |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
20287
diff
changeset
|
61 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
|
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_GetObject(PyObject *ref); |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
64 |
|
39351
12ffed927a3d
_PyWeakref_GetWeakrefCount() now returns a Py_ssize_t instead of long.
Neal Norwitz <nnorwitz@gmail.com>
parents:
33862
diff
changeset
|
65 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
|
66 |
|
30991
14d321466a06
SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents:
24752
diff
changeset
|
67 PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self); |
|
14d321466a06
SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents:
24752
diff
changeset
|
68 |
|
80762
7e771f0363e2
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:
44351
diff
changeset
|
69 /* Explanation for the Py_REFCNT() check: when a weakref's target is part |
|
7e771f0363e2
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:
44351
diff
changeset
|
70 of a long chain of deallocations which triggers the trashcan mechanism, |
|
7e771f0363e2
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:
44351
diff
changeset
|
71 clearing the weakrefs can be delayed long after the target's refcount |
|
7e771f0363e2
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:
44351
diff
changeset
|
72 has dropped to zero. In the meantime, code accessing the weakref will |
|
7e771f0363e2
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:
44351
diff
changeset
|
73 be able to "see" the target object even though it is supposed to be |
|
7e771f0363e2
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:
44351
diff
changeset
|
74 unreachable. See issue #16602. */ |
|
7e771f0363e2
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:
44351
diff
changeset
|
75 |
|
7e771f0363e2
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:
44351
diff
changeset
|
76 #define PyWeakref_GET_OBJECT(ref) \ |
|
7e771f0363e2
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:
44351
diff
changeset
|
77 (Py_REFCNT(((PyWeakReference *)(ref))->wr_object) > 0 \ |
|
7e771f0363e2
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:
44351
diff
changeset
|
78 ? ((PyWeakReference *)(ref))->wr_object \ |
|
7e771f0363e2
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:
44351
diff
changeset
|
79 : Py_None) |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
80 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
81 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
82 #ifdef __cplusplus |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
83 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
84 #endif |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
85 #endif /* !Py_WEAKREFOBJECT_H */ |
