@@ -252,60 +252,67 @@ true if (and only if) the object pointed to by *a* is a Python list.
252
252
Reference Counts
253
253
----------------
254
254
255
- The reference count is important because today's computers have a finite (and
256
- often severely limited) memory size; it counts how many different places there
257
- are that have a reference to an object. Such a place could be another object,
258
- or a global (or static) C variable, or a local variable in some C function.
259
- When an object's reference count becomes zero, the object is deallocated. If
260
- it contains references to other objects, their reference count is decremented.
261
- Those other objects may be deallocated in turn, if this decrement makes their
262
- reference count become zero, and so on. (There's an obvious problem with
263
- objects that reference each other here; for now, the solution is "don't do
264
- that.")
255
+ The reference count is important because today's computers have a finite
256
+ (and often severely limited) memory size; it counts how many different
257
+ places there are that have a :term: `strong reference ` to an object.
258
+ Such a place could be another object, or a global (or static) C variable,
259
+ or a local variable in some C function.
260
+ When the last :term: `strong reference ` to an object is released
261
+ (i.e. its reference count becomes zero), the object is deallocated.
262
+ If it contains references to other objects, those references are released.
263
+ Those other objects may be deallocated in turn, if there are no more
264
+ references to them, and so on. (There's an obvious problem with
265
+ objects that reference each other here; for now, the solution
266
+ is "don't do that.")
265
267
266
268
.. index ::
267
269
single: Py_INCREF()
268
270
single: Py_DECREF()
269
271
270
- Reference counts are always manipulated explicitly. The normal way is to use
271
- the macro :c:func: `Py_INCREF ` to increment an object's reference count by one,
272
- and :c:func: `Py_DECREF ` to decrement it by one. The :c:func: `Py_DECREF ` macro
272
+ Reference counts are always manipulated explicitly. The normal way is
273
+ to use the macro :c:func: `Py_INCREF ` to take a new reference to an
274
+ object (i.e. increment its reference count by one),
275
+ and :c:func: `Py_DECREF ` to release that reference (i.e. decrement the
276
+ reference count by one). The :c:func: `Py_DECREF ` macro
273
277
is considerably more complex than the incref one, since it must check whether
274
278
the reference count becomes zero and then cause the object's deallocator to be
275
- called. The deallocator is a function pointer contained in the object's type
276
- structure. The type-specific deallocator takes care of decrementing the
277
- reference counts for other objects contained in the object if this is a compound
279
+ called. The deallocator is a function pointer contained in the object's type
280
+ structure. The type-specific deallocator takes care of releasing references
281
+ for other objects contained in the object if this is a compound
278
282
object type, such as a list, as well as performing any additional finalization
279
283
that's needed. There's no chance that the reference count can overflow; at
280
284
least as many bits are used to hold the reference count as there are distinct
281
285
memory locations in virtual memory (assuming ``sizeof(Py_ssize_t) >= sizeof(void*) ``).
282
286
Thus, the reference count increment is a simple operation.
283
287
284
- It is not necessary to increment an object's reference count for every local
285
- variable that contains a pointer to an object. In theory, the object's
288
+ It is not necessary to hold a :term: `strong reference ` (i.e. increment
289
+ the reference count) for every local variable that contains a pointer
290
+ to an object. In theory, the object's
286
291
reference count goes up by one when the variable is made to point to it and it
287
292
goes down by one when the variable goes out of scope. However, these two
288
293
cancel each other out, so at the end the reference count hasn't changed. The
289
294
only real reason to use the reference count is to prevent the object from being
290
295
deallocated as long as our variable is pointing to it. If we know that there
291
296
is at least one other reference to the object that lives at least as long as
292
- our variable, there is no need to increment the reference count temporarily.
297
+ our variable, there is no need to take a new :term: `strong reference `
298
+ (i.e. increment the reference count) temporarily.
293
299
An important situation where this arises is in objects that are passed as
294
300
arguments to C functions in an extension module that are called from Python;
295
301
the call mechanism guarantees to hold a reference to every argument for the
296
302
duration of the call.
297
303
298
304
However, a common pitfall is to extract an object from a list and hold on to it
299
- for a while without incrementing its reference count. Some other operation might
300
- conceivably remove the object from the list, decrementing its reference count
305
+ for a while without taking a new reference. Some other operation might
306
+ conceivably remove the object from the list, releasing that reference,
301
307
and possibly deallocating it. The real danger is that innocent-looking
302
308
operations may invoke arbitrary Python code which could do this; there is a code
303
309
path which allows control to flow back to the user from a :c:func: `Py_DECREF `, so
304
310
almost any operation is potentially dangerous.
305
311
306
312
A safe approach is to always use the generic operations (functions whose name
307
313
begins with ``PyObject_ ``, ``PyNumber_ ``, ``PySequence_ `` or ``PyMapping_ ``).
308
- These operations always increment the reference count of the object they return.
314
+ These operations always create a new :term: `strong reference `
315
+ (i.e. increment the reference count) of the object they return.
309
316
This leaves the caller with the responsibility to call :c:func: `Py_DECREF ` when
310
317
they are done with the result; this soon becomes second nature.
311
318
@@ -321,7 +328,7 @@ to objects (objects are not owned: they are always shared). "Owning a
321
328
reference" means being responsible for calling Py_DECREF on it when the
322
329
reference is no longer needed. Ownership can also be transferred, meaning that
323
330
the code that receives ownership of the reference then becomes responsible for
324
- eventually decref'ing it by calling :c:func: `Py_DECREF ` or :c:func: `Py_XDECREF `
331
+ eventually releasing it by calling :c:func: `Py_DECREF ` or :c:func: `Py_XDECREF `
325
332
when it's no longer needed---or passing on this responsibility (usually to its
326
333
caller). When a function passes ownership of a reference on to its caller, the
327
334
caller is said to receive a *new * reference. When no ownership is transferred,
@@ -379,9 +386,9 @@ For example, the above two blocks of code could be replaced by the following
379
386
380
387
It is much more common to use :c:func: `PyObject_SetItem ` and friends with items
381
388
whose references you are only borrowing, like arguments that were passed in to
382
- the function you are writing. In that case, their behaviour regarding reference
383
- counts is much saner, since you don't have to increment a reference count so you
384
- can give a reference away ("have it be stolen"). For example, this function
389
+ the function you are writing. In that case, their behaviour regarding references
390
+ is much saner, since you don't have to take a new reference just so you
391
+ can give that reference away ("have it be stolen"). For example, this function
385
392
sets all items of a list (actually, any mutable sequence) to a given item::
386
393
387
394
int
0 commit comments