-
Notifications
You must be signed in to change notification settings - Fork 63
feat: move Pythonizations implementations to source files #908
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
Changes from all commits
495b4f5
21d8d85
e0c8c89
05877e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -61,6 +61,7 @@ SET(core_sources | |||||||||||||||||||||
| MurmurHash3.cpp | ||||||||||||||||||||||
| SchemaEvolution.cc | ||||||||||||||||||||||
| Glob.cc | ||||||||||||||||||||||
| Pythonizations.cc | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| SET(core_headers | ||||||||||||||||||||||
|
|
@@ -79,6 +80,7 @@ SET(core_headers | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| PODIO_ADD_LIB_AND_DICT(podio "${core_headers}" "${core_sources}" selection.xml) | ||||||||||||||||||||||
| target_compile_options(podio PRIVATE -pthread) | ||||||||||||||||||||||
| target_link_libraries(podio PRIVATE Python3::Python) | ||||||||||||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, this probably should have been in the podio/cmake/podioConfig.cmake.in Lines 28 to 32 in 5d82748
and in the PODIO_GENERATE_DATAMODEL module:Line 158 in 5d82748
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then again, the package that is found is Lines 197 to 200 in 5d82748
|
||||||||||||||||||||||
| # For Frame.h | ||||||||||||||||||||||
| if (ROOT_VERSION VERSION_LESS 6.36) | ||||||||||||||||||||||
| target_compile_definitions(podio PUBLIC PODIO_ROOT_OLDER_6_36=1) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| #include "podio/detail/Pythonizations.h" | ||
|
|
||
| #include <cstring> | ||
| #include <stdexcept> | ||
|
|
||
| namespace podio::detail::pythonizations { | ||
|
|
||
| // Callback function for the subscript pythonization | ||
| // Calls the `at` method and change exception type to IndexError if the index is out of range | ||
tmadlener marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| PyObject* subscript(PyObject* self, PyObject* index) { | ||
| PyObject* result = PyObject_CallMethod(self, "at", "O", index); | ||
| if (!result) { | ||
| PyObject* exc = PyErr_Occurred(); | ||
| // Check if the exception is `cppyy.gbl.std.out_of_range` | ||
| // Since PyImport_ImportModule("cppyy") fails, this workaround checks the exception name | ||
| if (exc && PyObject_HasAttrString(exc, "__name__")) { | ||
| PyObject* exc_name = PyObject_GetAttrString(exc, "__name__"); | ||
| if (exc_name) { | ||
| const char* name_cstr = PyUnicode_AsUTF8(exc_name); | ||
| if (name_cstr && strcmp(name_cstr, "out_of_range") == 0) { | ||
| PyErr_Clear(); | ||
| PyErr_SetString(PyExc_IndexError, "Index out of range"); | ||
| } | ||
| Py_DECREF(exc_name); | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| // Helper to register the subscript pythonization callback as `__getitem__` method | ||
tmadlener marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| void pythonize_subscript(PyObject* klass, const std::string& name) { | ||
| static PyMethodDef ml = {"subscript_pythonization", subscript, METH_VARARGS, R"( | ||
| Raise an `IndexError` exception if an index is invalid. | ||
| The `__getitem__` will return immutable datatype objects instead of the mutable ones. | ||
| )"}; | ||
| auto* func = PyCFunction_New(&ml, klass); | ||
| if (!func) { | ||
| throw std::runtime_error("Failed to create Python subscript function for class " + name); | ||
| } | ||
| auto* method = PyInstanceMethod_New(func); | ||
| if (!method) { | ||
| Py_DECREF(func); | ||
| throw std::runtime_error("Failed to create Python instance method for subscript for class " + name); | ||
| } | ||
| if (0 != PyObject_SetAttrString(klass, "__getitem__", method)) { | ||
| Py_DECREF(method); | ||
| Py_DECREF(func); | ||
| throw std::runtime_error("Failed to set __getitem__ attribute on class " + name); | ||
| } | ||
| Py_DECREF(func); | ||
| Py_DECREF(method); | ||
| } | ||
|
|
||
| } // namespace podio::detail::pythonizations | ||
Uh oh!
There was an error while loading. Please reload this page.