Skip to content

[python] Improve CPyCppyy heuristic for Clone methods #18423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025

Conversation

vepadulano
Copy link
Member

The heuristic checks if the token "Clone" is present in the method name being called and gives Python the ownership of the returned object. This is valid behaviour for methods such as TObject::Clone where the ownership of the returned value is given to the caller. But the CPPOverload::Set method gets as the method name being called the actual function name concatenated with the full template argument list. For example, given the function

template <typename T>
void foo();

When it passes through this method its name will be foo<T>. This behaviour has an undesired side-effect. When T is a type with the token Clone in its name, then the heuristic will trigger and Python will take ownership of the return value. This is the root cause of the problem seen at #16725, where T is TClonesArray.

This commit improves on the heuristic by stripping the template argument list from the method name before checking for the presence of the token.

Fixes #16725
Hopefully supersedes #18416

@vepadulano vepadulano force-pushed the cppyy-improve-heuristic branch from a27bcfa to f2f417f Compare April 16, 2025 09:51
Copy link
Contributor

@guitargeek guitargeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for making this PR and explaining the problem in the commit message!

@vepadulano vepadulano force-pushed the cppyy-improve-heuristic branch from f2f417f to 67087bd Compare April 16, 2025 11:27
The heuristic checks if the token "Clone" is present in the method name being
called and gives Python the ownership of the returned object. This is valid
behaviour for methods such as `TObject::Clone` where the ownership of the
returned value is given to the caller. But the `CPPOverload::Set` method gets as
the method name being called the actual function name concatenated with the full
template argument list. For example, given the function

```
template <typename T>
void foo();
```

When it passes through this method its name will be `foo<T>`. This behaviour has
an undesired side-effect. When `T` is a type with the token `Clone` in its name,
then the heuristic will trigger and Python will take ownership of the return
value. This is the root cause of the problem seen at
root-project#16725, where `T` is `TClonesArray`.

This commit improves on the heuristic by stripping the template argument list
from the method name before checking for the presence of the token.

Co-authored-by: Jonas Rembser <[email protected]>
Copy link

Test Results

    18 files      18 suites   4d 9h 38m 20s ⏱️
 2 738 tests  2 738 ✅ 0 💤 0 ❌
47 613 runs  47 613 ✅ 0 💤 0 ❌

Results for commit 091a5ca.

@dpiparo dpiparo merged commit 9cccc58 into root-project:master Apr 16, 2025
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pyroot crashes reading TClonesArray in a TTree
3 participants