Skip to content

[Python] Compiler annotations for ownership semantics #21618

@guitargeek

Description

@guitargeek

Feature description

It would be great if ownership semantics can be communicated in the C++ source code via annotations, so we don't have to write Pythonizations by hand just for this, and carefully keep them in sync with the behavior of the C++ code.

This is relevant for both arguments and return values.

For arguments, we need something to indicate ownership adoption (_transfer_ownership is just a placeholder name to illustrate the idea):

void foo(TObject * _transfer_ownership obj) {
   std::unique_ptr<TObject>{obj};
}

For return values, we need something to indicate ownership on the caller side (_caller_owns is again just a placeholder):

TObject *_caller_owns bar() {
   return new TObject{};
}

Object ownership problems come up often on the forum (example post), so having a robust way to convey the correct ownership semantics would be a game changer for the robustness of the ROOT Python interfaces.

Alternatives considered

  • Manual Pythonizations like in ee79dfd, but this doesn't scale well.
  • Migrating C++ interfaces to smart pointers, but this would break backwards compatibility.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions