1#!/usr/bin/env python2.7
2# -*- coding: utf-8 -*-
3""":synopsis: Interface to the cycle-detecting garbage collector.
4"""
5"""
6A list of objects which the collector found to be unreachable but could not be
7freed (uncollectable objects).  By default, this list contains only objects with
8:meth:`__del__` methods. [#]_ Objects that have :meth:`__del__` methods and are
9part of a reference cycle cause the entire reference cycle to be uncollectable,
10including objects not necessarily in the cycle but reachable only from it.
11Python doesn't collect such cycles automatically because, in general, it isn't
12possible for Python to guess a safe order in which to run the :meth:`__del__`
13methods.  If you know a safe order, you can force the issue by examining the
14*garbage* list, and explicitly breaking cycles due to your objects within the
15list.  Note that these objects are kept alive even so by virtue of being in the
16*garbage* list, so they should be removed from *garbage* too.  For example,
17after breaking cycles, do ``del gc.garbage[:]`` to empty the list.  It's
18generally better to avoid the issue by not creating cycles containing objects
19with :meth:`__del__` methods, and *garbage* can be examined in that case to
20verify that no such cycles are being created.
21
22If :const:`DEBUG_SAVEALL` is set, then all unreachable objects will be added to
23this list rather than freed.
24
25The following constants are provided for use with :func:`set_debug`:
26
27
28"""
29garbage = None
30"""
31Print statistics during collection.  This information can be useful when tuning
32the collection frequency.
33
34
35"""
36DEBUG_STATS = None
37"""
38Print information on collectable objects found.
39
40
41"""
42DEBUG_COLLECTABLE = None
43"""
44Print information of uncollectable objects found (objects which are not
45reachable but cannot be freed by the collector).  These objects will be added to
46the ``garbage`` list.
47
48
49"""
50DEBUG_UNCOLLECTABLE = None
51"""
52When :const:`DEBUG_COLLECTABLE` or :const:`DEBUG_UNCOLLECTABLE` is set, print
53information about instance objects found.
54
55
56"""
57DEBUG_INSTANCES = None
58"""
59When :const:`DEBUG_COLLECTABLE` or :const:`DEBUG_UNCOLLECTABLE` is set, print
60information about objects other than instance objects found.
61
62
63"""
64DEBUG_OBJECTS = None
65"""
66When set, all unreachable objects found will be appended to *garbage* rather
67than being freed.  This can be useful for debugging a leaking program.
68
69
70"""
71DEBUG_SAVEALL = None
72"""
73The debugging flags necessary for the collector to print information about a
74leaking program (equal to ``DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE |
75DEBUG_INSTANCES | DEBUG_OBJECTS | DEBUG_SAVEALL``).
76
77"""
78DEBUG_LEAK = None
79def enable():
80	"""
81	Enable automatic garbage collection.
82
83
84	"""
85	pass
86
87def disable():
88	"""
89	Disable automatic garbage collection.
90
91
92	"""
93	pass
94
95def isenabled():
96	"""
97	Returns true if automatic collection is enabled.
98
99
100	"""
101	pass
102
103def collect(generation):
104	"""
105	With no arguments, run a full collection.  The optional argument *generation*
106	may be an integer specifying which generation to collect (from 0 to 2).  A
107	:exc:`ValueError` is raised if the generation number  is invalid. The number of
108	unreachable objects found is returned.
109
110	"""
111	pass
112
113def set_debug(flags):
114	"""
115	Set the garbage collection debugging flags. Debugging information will be
116	written to ``sys.stderr``.  See below for a list of debugging flags which can be
117	combined using bit operations to control debugging.
118
119
120	"""
121	pass
122
123def get_debug():
124	"""
125	Return the debugging flags currently set.
126
127
128	"""
129	pass
130
131def get_objects():
132	"""
133	Returns a list of all objects tracked by the collector, excluding the list
134	returned.
135
136	"""
137	pass
138
139def set_threshold(threshold0,threshold1,threshold2):
140	"""
141	Set the garbage collection thresholds (the collection frequency). Setting
142	*threshold0* to zero disables collection.
143
144	The GC classifies objects into three generations depending on how many
145	collection sweeps they have survived.  New objects are placed in the youngest
146	generation (generation ``0``).  If an object survives a collection it is moved
147	into the next older generation.  Since generation ``2`` is the oldest
148	generation, objects in that generation remain there after a collection.  In
149	order to decide when to run, the collector keeps track of the number object
150	allocations and deallocations since the last collection.  When the number of
151	allocations minus the number of deallocations exceeds *threshold0*, collection
152	starts.  Initially only generation ``0`` is examined.  If generation ``0`` has
153	been examined more than *threshold1* times since generation ``1`` has been
154	examined, then generation ``1`` is examined as well.  Similarly, *threshold2*
155	controls the number of collections of generation ``1`` before collecting
156	generation ``2``.
157
158
159	"""
160	pass
161
162def get_count():
163	"""
164	Return the current collection  counts as a tuple of ``(count0, count1,
165	count2)``.
166
167	"""
168	pass
169
170def get_threshold():
171	"""
172	Return the current collection thresholds as a tuple of ``(threshold0,
173	threshold1, threshold2)``.
174
175
176	"""
177	pass
178
179def get_referrers(objs):
180	"""
181	Return the list of objects that directly refer to any of objs. This function
182	will only locate those containers which support garbage collection; extension
183	types which do refer to other objects but do not support garbage collection will
184	not be found.
185
186	Note that objects which have already been dereferenced, but which live in cycles
187	and have not yet been collected by the garbage collector can be listed among the
188	resulting referrers.  To get only currently live objects, call :func:`collect`
189	before calling :func:`get_referrers`.
190
191	Care must be taken when using objects returned by :func:`get_referrers` because
192	some of them could still be under construction and hence in a temporarily
193	invalid state. Avoid using :func:`get_referrers` for any purpose other than
194	debugging.
195
196	"""
197	pass
198
199def get_referents(objs):
200	"""
201	Return a list of objects directly referred to by any of the arguments. The
202	referents returned are those objects visited by the arguments' C-level
203	:attr:`tp_traverse` methods (if any), and may not be all objects actually
204	directly reachable.  :attr:`tp_traverse` methods are supported only by objects
205	that support garbage collection, and are only required to visit objects that may
206	be involved in a cycle.  So, for example, if an integer is directly reachable
207	from an argument, that integer object may or may not appear in the result list.
208
209	"""
210	pass
211
212def is_tracked(obj):
213	"""
214	Returns True if the object is currently tracked by the garbage collector,
215	False otherwise.  As a general rule, instances of atomic types aren't
216	tracked and instances of non-atomic types (containers, user-defined
217	objectsmore) are.  However, some type-specific optimizations can be present
218	in order to suppress the garbage collector footprint of simple instances
219	(e.g. dicts containing only atomic keys and values)::
220
221	>>> gc.is_tracked(0)
222	False
223	>>> gc.is_tracked("a")
224	False
225	>>> gc.is_tracked([])
226	True
227	>>> gc.is_tracked({})
228	False
229	>>> gc.is_tracked({"a": 1})
230	False
231	>>> gc.is_tracked({"a": []})
232	True
233
234	"""
235	pass
236
237