Mercurial > cpython-lru_cache-weakref
annotate Include/weakrefobject.h @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 1800107873c0 |
| children |
| 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 |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
54 #define PyWeakref_Check(op) \ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
55 (PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op)) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
56 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
57 |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
20287
diff
changeset
|
58 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
|
59 PyObject *callback); |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
20287
diff
changeset
|
60 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
|
61 PyObject *callback); |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
20287
diff
changeset
|
62 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
|
63 |
|
66664
62b61abd02b8
Merge branches/pep-0384.
Martin v. Löwis <martin@v.loewis.de>
parents:
65494
diff
changeset
|
64 #ifndef Py_LIMITED_API |
|
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); |
|
66664
62b61abd02b8
Merge branches/pep-0384.
Martin v. Löwis <martin@v.loewis.de>
parents:
65494
diff
changeset
|
68 #endif |
|
30991
14d321466a06
SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents:
24752
diff
changeset
|
69 |
|
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
|
70 /* 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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 |
|
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 #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
|
78 (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
|
79 ? ((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
|
80 : Py_None) |
|
20280
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 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
83 #ifdef __cplusplus |
|
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 #endif |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
86 #endif /* !Py_WEAKREFOBJECT_H */ |
