Repository navigation
Description
Per capi-workgroup/problems#11, we need terms for managing ownership.
IMO, it would be useful to use similar terms both for references and to non-refcounted resources -- typically, allocated memory.
Arguments
By default, ownership of arguments is not transferred. After a call:
- the caller is still responsible for decrefing/freeing any arguments it passed in.
- the callee must not assume the argument stays valid.
The caller must guarantee that the arguments stay valid for the duration of the call.
“Output” arguments (where the callee gets a *result pointer and fills it) follow rules for return values.
We should use shorter, context-specific terms for transferring ownership (the non-default-behaviour):
- Steal: take ownership of
PyObject*or other refcounted/GCd data (i.e. other references might exist). Used for performance optimizations or ergonomics. - Consume: used for non-refcounted data (e.g. array of
PyObject*), e.g. string builder finalization API would consume the scratch data and return a new object - Clear: Destroy a reference and mark it invalid (i.e. set the
PyObject*toNULL)- conflicts with the term for emptying a container
- Decref, Close, Free, Dealloc: specific API that only destroys the reference/data
I'm aware that Steal is pejorative; but IMO it's better than the alternative (Take, Give, Move)
Another useful exception is:
- Static: The argument is static and immutable; it does not need to be freed and is always valid.
Return values
By default, ownership of the return value is transferred. After a call:
- the caller is responsible for decrefing/freeing the return value.
- the callee must not assume the return value stays valid
The opposite is:
-
Borrow: the caller must not decref/free the return value, but is responsible for ensuring that it is not accessed after its lifetime ends. Docs for the called function need to indicate what that lifetime is.
-
Static is, conceptually, borrowing from the interpreter itself.
Activity
davidhewitt commentedon Oct 12, 2023
I wonder, even though there is already use of Steal, does there need to be a difference for non-refcounted data? Why not use Consume for both types of data?
encukou commentedon Oct 18, 2023
To me, “consume” sounds like it'll destroy the object, not just the reference to it.
vstinner commentedon Dec 11, 2023
I dislike "steal" term, it's negative and I don't think that it's easy to get its meaning.
I prefer "move" or "transfer". In python/cpython#111489 I propose to make
_PyList_FromArraySteal()public. I would prefer to replace the "Steal" suffix with "Transfer" or "Move". It would be more explicit that the ownership is moved/transfered from somewhere to somewhere else. It describes better the action and that the old place becomes invalid.vstinner commentedon Dec 11, 2023
I wrote python/cpython#112975 which adds fuctions:
PyTuple_FromArray()PyTuple_FromArrayMoveRef()PyList_FromArrayMoveRef()I chose
MoveRefsuffix, similar toRefinPyDict_GetItemRef, and using verb +Refsuch asPy_SETREF().Maybe with a concrete PR, it may be easier to decide on the naming convention for functions which move/transfer reference ownership.
The current implementation leaves the source array unchanged.
gvanrossum commentedon Dec 11, 2023
FWIW I still prefer “steal”. So this is not settled.
vstinner commentedon Dec 12, 2023
@serhiy-storchaka wrote:
encukou commentedon Dec 12, 2023
IMO, the proper term, which should be used in documentation, is transfer ownership. However, that's too long for an API name suffix. Using just
Movewould be unclear: what is being moved?Stealis short and unique. I, unlike Victor, think it is easy to get its meaning.vstinner commentedon Dec 14, 2023
@gvanrossum:
Creating a PR doesn't imply that anything is settled. As I wrote, I proposed a PR to help to better see which name fits better.
vstinner commentedon Dec 14, 2023
In terms of "ownership", Rust has many concepts and tools for that, and ownership is a key concept in Rust. In Rust, the action of transferring ownership is called "move": https://doc.rust-lang.org/rust-by-example/scope/move.html
C++11 introduced
std::move(obj)which is similar similar: https://en.cppreference.com/w/cpp/utility/movevstinner commentedon Oct 7, 2025
I created capi-workgroup/decisions#78 to ask the C API Working Group to decide on the "take ownership" term (Steal or Move).