@@ -252,60 +252,67 @@ true if (and only if) the object pointed to by *a* is a Python list.
252252Reference Counts
253253----------------
254254
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.")
265267
266268.. index ::
267269 single: Py_INCREF()
268270 single: Py_DECREF()
269271
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
273277is considerably more complex than the incref one, since it must check whether
274278the 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
278282object type, such as a list, as well as performing any additional finalization
279283that's needed. There's no chance that the reference count can overflow; at
280284least as many bits are used to hold the reference count as there are distinct
281285memory locations in virtual memory (assuming ``sizeof(Py_ssize_t) >= sizeof(void*) ``).
282286Thus, the reference count increment is a simple operation.
283287
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
286291reference count goes up by one when the variable is made to point to it and it
287292goes down by one when the variable goes out of scope. However, these two
288293cancel each other out, so at the end the reference count hasn't changed. The
289294only real reason to use the reference count is to prevent the object from being
290295deallocated as long as our variable is pointing to it. If we know that there
291296is 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.
293299An important situation where this arises is in objects that are passed as
294300arguments to C functions in an extension module that are called from Python;
295301the call mechanism guarantees to hold a reference to every argument for the
296302duration of the call.
297303
298304However, 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,
301307and possibly deallocating it. The real danger is that innocent-looking
302308operations may invoke arbitrary Python code which could do this; there is a code
303309path which allows control to flow back to the user from a :c:func: `Py_DECREF `, so
304310almost any operation is potentially dangerous.
305311
306312A safe approach is to always use the generic operations (functions whose name
307313begins 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.
309316This leaves the caller with the responsibility to call :c:func: `Py_DECREF ` when
310317they are done with the result; this soon becomes second nature.
311318
@@ -321,7 +328,7 @@ to objects (objects are not owned: they are always shared). "Owning a
321328reference" means being responsible for calling Py_DECREF on it when the
322329reference is no longer needed. Ownership can also be transferred, meaning that
323330the 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 `
325332when it's no longer needed---or passing on this responsibility (usually to its
326333caller). When a function passes ownership of a reference on to its caller, the
327334caller 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
379386
380387It is much more common to use :c:func: `PyObject_SetItem ` and friends with items
381388whose 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
385392sets all items of a list (actually, any mutable sequence) to a given item::
386393
387394 int
0 commit comments