@@ -287,60 +287,67 @@ true if (and only if) the object pointed to by *a* is a Python list.
287
287
Reference Counts
288
288
----------------
289
289
290
- The reference count is important because today's computers have a finite (and
291
- often severely limited) memory size; it counts how many different places there
292
- are that have a reference to an object. Such a place could be another object,
293
- or a global (or static) C variable, or a local variable in some C function.
294
- When an object's reference count becomes zero, the object is deallocated. If
295
- it contains references to other objects, their reference count is decremented.
296
- Those other objects may be deallocated in turn, if this decrement makes their
297
- reference count become zero, and so on. (There's an obvious problem with
298
- objects that reference each other here; for now, the solution is "don't do
299
- that.")
290
+ The reference count is important because today's computers have a finite
291
+ (and often severely limited) memory size; it counts how many different
292
+ places there are that have a :term: `strong reference ` to an object.
293
+ Such a place could be another object, or a global (or static) C variable,
294
+ or a local variable in some C function.
295
+ When the last :term: `strong reference ` to an object is released
296
+ (i.e. its reference count becomes zero), the object is deallocated.
297
+ If it contains references to other objects, those references are released.
298
+ Those other objects may be deallocated in turn, if there are no more
299
+ references to them, and so on. (There's an obvious problem with
300
+ objects that reference each other here; for now, the solution
301
+ is "don't do that.")
300
302
301
303
.. index ::
302
304
single: Py_INCREF()
303
305
single: Py_DECREF()
304
306
305
- Reference counts are always manipulated explicitly. The normal way is to use
306
- the macro :c:func: `Py_INCREF ` to increment an object's reference count by one,
307
- and :c:func: `Py_DECREF ` to decrement it by one. The :c:func: `Py_DECREF ` macro
307
+ Reference counts are always manipulated explicitly. The normal way is
308
+ to use the macro :c:func: `Py_INCREF ` to take a new reference to an
309
+ object (i.e. increment its reference count by one),
310
+ and :c:func: `Py_DECREF ` to release that reference (i.e. decrement the
311
+ reference count by one). The :c:func: `Py_DECREF ` macro
308
312
is considerably more complex than the incref one, since it must check whether
309
313
the reference count becomes zero and then cause the object's deallocator to be
310
- called. The deallocator is a function pointer contained in the object's type
311
- structure. The type-specific deallocator takes care of decrementing the
312
- reference counts for other objects contained in the object if this is a compound
314
+ called. The deallocator is a function pointer contained in the object's type
315
+ structure. The type-specific deallocator takes care of releasing references
316
+ for other objects contained in the object if this is a compound
313
317
object type, such as a list, as well as performing any additional finalization
314
318
that's needed. There's no chance that the reference count can overflow; at
315
319
least as many bits are used to hold the reference count as there are distinct
316
320
memory locations in virtual memory (assuming ``sizeof(Py_ssize_t) >= sizeof(void*) ``).
317
321
Thus, the reference count increment is a simple operation.
318
322
319
- It is not necessary to increment an object's reference count for every local
320
- variable that contains a pointer to an object. In theory, the object's
323
+ It is not necessary to hold a :term: `strong reference ` (i.e. increment
324
+ the reference count) for every local variable that contains a pointer
325
+ to an object. In theory, the object's
321
326
reference count goes up by one when the variable is made to point to it and it
322
327
goes down by one when the variable goes out of scope. However, these two
323
328
cancel each other out, so at the end the reference count hasn't changed. The
324
329
only real reason to use the reference count is to prevent the object from being
325
330
deallocated as long as our variable is pointing to it. If we know that there
326
331
is at least one other reference to the object that lives at least as long as
327
- our variable, there is no need to increment the reference count temporarily.
332
+ our variable, there is no need to take a new :term: `strong reference `
333
+ (i.e. increment the reference count) temporarily.
328
334
An important situation where this arises is in objects that are passed as
329
335
arguments to C functions in an extension module that are called from Python;
330
336
the call mechanism guarantees to hold a reference to every argument for the
331
337
duration of the call.
332
338
333
339
However, a common pitfall is to extract an object from a list and hold on to it
334
- for a while without incrementing its reference count. Some other operation might
335
- conceivably remove the object from the list, decrementing its reference count
340
+ for a while without taking a new reference. Some other operation might
341
+ conceivably remove the object from the list, releasing that reference,
336
342
and possibly deallocating it. The real danger is that innocent-looking
337
343
operations may invoke arbitrary Python code which could do this; there is a code
338
344
path which allows control to flow back to the user from a :c:func: `Py_DECREF `, so
339
345
almost any operation is potentially dangerous.
340
346
341
347
A safe approach is to always use the generic operations (functions whose name
342
348
begins with ``PyObject_ ``, ``PyNumber_ ``, ``PySequence_ `` or ``PyMapping_ ``).
343
- These operations always increment the reference count of the object they return.
349
+ These operations always create a new :term: `strong reference `
350
+ (i.e. increment the reference count) of the object they return.
344
351
This leaves the caller with the responsibility to call :c:func: `Py_DECREF ` when
345
352
they are done with the result; this soon becomes second nature.
346
353
@@ -356,7 +363,7 @@ to objects (objects are not owned: they are always shared). "Owning a
356
363
reference" means being responsible for calling Py_DECREF on it when the
357
364
reference is no longer needed. Ownership can also be transferred, meaning that
358
365
the code that receives ownership of the reference then becomes responsible for
359
- eventually decref'ing it by calling :c:func: `Py_DECREF ` or :c:func: `Py_XDECREF `
366
+ eventually releasing it by calling :c:func: `Py_DECREF ` or :c:func: `Py_XDECREF `
360
367
when it's no longer needed---or passing on this responsibility (usually to its
361
368
caller). When a function passes ownership of a reference on to its caller, the
362
369
caller is said to receive a *new * reference. When no ownership is transferred,
@@ -414,9 +421,9 @@ For example, the above two blocks of code could be replaced by the following
414
421
415
422
It is much more common to use :c:func: `PyObject_SetItem ` and friends with items
416
423
whose references you are only borrowing, like arguments that were passed in to
417
- the function you are writing. In that case, their behaviour regarding reference
418
- counts is much saner, since you don't have to increment a reference count so you
419
- can give a reference away ("have it be stolen"). For example, this function
424
+ the function you are writing. In that case, their behaviour regarding references
425
+ is much saner, since you don't have to take a new reference just so you
426
+ can give that reference away ("have it be stolen"). For example, this function
420
427
sets all items of a list (actually, any mutable sequence) to a given item::
421
428
422
429
int
0 commit comments