Skip to content
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

[Exp PyROOT] Add item setting pythonisation for TClonesArray #3423

Merged
merged 7 commits into from
Feb 19, 2019

Conversation

etejedor
Copy link
Contributor

The pythonisation proposed in this PR injects a __setitem__ implementation into TClonesArray that customizes the setting of an item.

The __setitem__ pythonization that TClonesArray inherits from TSeqCollection does not apply in this case and a redefinition is required. The reason is TClonesArray sets objects by constructing them in-place, which is impossible to support as the Python object given as value must exist a priori. It can, however, be memcpy'd and stolen, which is the approach used in this redefinition. This is also the reason why this pythonisation needs to be implemented in C++.

@etejedor etejedor self-assigned this Feb 13, 2019
@phsft-bot
Copy link
Collaborator

Starting build on ROOT-performance-centos7-multicore/default, ROOT-fedora27/noimt, ROOT-fedora29/python3, ROOT-ubuntu16/rtcxxmod, mac1014/cxx17, windows10/default
How to customize builds

Copy link
Member

@pcanal pcanal left a comment

Choose a reason for hiding this comment

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

see comment

TObject *object = (*cla)[index];
pyobj->CppOwns();
MemoryRegulator::RegisterPyObject(pyobj, object);
memcpy((void *)object, pyobj->GetObject(), cla->GetClass()->Size());
Copy link
Member

Choose a reason for hiding this comment

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

In general this is not a safe way of copying the objects. [It might work in many cases but many other will leave dangling pointers in the target]. See TObject::Copy which might be the solution here.

See also TClonesArray::ConstructedAt and TClonesArray::Absorb (i.e. or maybe we need a special case for pyroot added to TClonesArray).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm trying to use TObject::Copy instead of memcpy but it seems to have no effect, the destination object remains null. I tried a quick test:

root [0] TObjString o("a");
root [1] TObjString o2("b");
root [2] cout << o.GetName() << endl;
a
root [3] cout << o2.GetName() << endl;
b
root [4] o.Copy(o2)
root [5] cout << o.GetName() << endl;
a
root [6] cout << o2.GetName() << endl;
b

I would expect o to be copied into o2 but that does not seem the case. What I am missing? Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

that TObjString does not overload Copy .. :( ... so indeed we can't rely on it ....

Copy link
Member

Choose a reason for hiding this comment

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

The 'solution' is to use/implement a variant of TDirectory::CloneObject that replace the first statement (calling New()) with an input parameter ....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @pcanal, following your suggestion I modified the solution to use a helper method inspired on TDirectory::CloneObject, so that the cloning is done with streamers. Please review the corresponding commit b869970

@phsft-bot
Copy link
Collaborator

Starting build on ROOT-performance-centos7-multicore/default, ROOT-fedora27/noimt, ROOT-fedora29/python3, ROOT-ubuntu16/rtcxxmod, mac1014/cxx17, windows10/default
How to customize builds

Copy link
Contributor

@stwunsch stwunsch left a comment

Choose a reason for hiding this comment

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

Nice! :) I've pointed out some places where the reference counting is not completely clear to me.

Py_INCREF(obj);
PyObject *result = PyObject_CallMethod(obj, const_cast<char *>(meth), const_cast<char *>("O"), arg1);
PyObject* result = PyObject_CallMethod(obj, const_cast<char*>(meth), const_cast<char*>(""));
Py_DECREF(obj);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need the increment and decrement of the reference count around the call method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I don't think we do!

// To know the capacity of a TClonesArray, we need to invoke GetSize
PyObject *pysize = CallPyObjMethod(self, "GetSize");
if (!pysize) {
PyErr_Clear();
Copy link
Contributor

Choose a reason for hiding this comment

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

Which error is thrown if you clear the error here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will set a custom error message for it, thanks!

return nullptr;
}

PyObject *pyindex = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need to increment the count here? You could just do a pass-through?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The caller of PyStyleIndex is responsible for decrefing the return value (pyindex). pyindex can be either index (refcount 2) or the result of PyLong_FromSsize_t (refcount 1). In either case, the caller (SetItem) will decref, so everything is fine. I will add a comment about this!

@phsft-bot
Copy link
Collaborator

Starting build on ROOT-performance-centos7-multicore/default, ROOT-fedora27/noimt, ROOT-fedora29/python3, ROOT-ubuntu16/rtcxxmod, mac1014/cxx17, windows10/default
How to customize builds

@etejedor etejedor merged commit 1cd0b68 into root-project:master Feb 19, 2019
@root-project root-project deleted a comment Feb 19, 2019
guitargeek added a commit to guitargeek/root that referenced this pull request Jan 30, 2024
The `TClonesArray.__setitem__` pythonization was introduced in commit
67db9e5 with PR root-project#3423.

However, this pythonization is quite unnatural, because a `TObjArray`
should not adopt external memory. In fact, on in the C++ side, all
`TObjArray`-inherited methods that take a `TObject *` and that would
adopt these objects are flagged as `MayNotUse` for good reasons!

Supporting this element setting only on the Python side therefore comes
with a very high cost of hacking deep into CPyCppyy, and the benefit of
this is not clear because the PR that introduced this feature didn't
refer to any user request.

Hence, this commit suggests to remove this pythonization, with the
ultimate goal of relying less on the usage of CPyCppyy internals.
guitargeek added a commit to guitargeek/root that referenced this pull request Jan 30, 2024
The `TClonesArray.__setitem__` pythonization was introduced in commit
67db9e5 with PR root-project#3423.

However, this pythonization is quite unnatural, because a `TObjArray`
should not adopt external memory. In fact, on in the C++ side, all
`TObjArray`-inherited methods that take a `TObject *` and that would
adopt these objects are flagged as `MayNotUse` for good reasons!

Supporting this element setting only on the Python side therefore comes
with a very high cost of hacking deep into CPyCppyy, and the benefit of
this is not clear because the PR that introduced this feature didn't
refer to any user request.

Hence, this commit suggests to remove this pythonization, with the
ultimate goal of relying less on the usage of CPyCppyy internals.
guitargeek added a commit to guitargeek/root that referenced this pull request Jan 30, 2024
The `TClonesArray.__setitem__` pythonization was introduced in commit
67db9e5 with PR root-project#3423.

However, this pythonization is quite unnatural, because a `TObjArray`
should not adopt external memory. In fact, on in the C++ side, all
`TObjArray`-inherited methods that take a `TObject *` and that would
adopt these objects are flagged as `MayNotUse` for good reasons!

Supporting this element setting only on the Python side therefore comes
with a very high cost of hacking deep into CPyCppyy, and the benefit of
this is not clear because the PR that introduced this feature didn't
refer to any user request.

Hence, this commit suggests to remove this pythonization, with the
ultimate goal of relying less on the usage of CPyCppyy internals.
guitargeek added a commit that referenced this pull request Jan 30, 2024
The `TClonesArray.__setitem__` pythonization was introduced in commit
67db9e5 with PR #3423.

However, this pythonization is quite unnatural, because a `TObjArray`
should not adopt external memory. In fact, on in the C++ side, all
`TObjArray`-inherited methods that take a `TObject *` and that would
adopt these objects are flagged as `MayNotUse` for good reasons!

Supporting this element setting only on the Python side therefore comes
with a very high cost of hacking deep into CPyCppyy, and the benefit of
this is not clear because the PR that introduced this feature didn't
refer to any user request.

Hence, this commit suggests to remove this pythonization, with the
ultimate goal of relying less on the usage of CPyCppyy internals.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants