From cdcdc537c78019be6599b4f241045031e5a90dca Mon Sep 17 00:00:00 2001 From: Jonas Rembser Date: Fri, 8 Nov 2024 14:15:43 +0100 Subject: [PATCH] [PyROOT] Merge PyROOTWrapper with PyROOTModule The PyROOTWrapper is a small piece of code that is only used in PyROOTModuce.cxx, so I don't think it needs its own translation unit. --- bindings/pyroot/pythonizations/CMakeLists.txt | 1 - .../pythonizations/src/PyROOTModule.cxx | 27 ++++++++++-- .../pythonizations/src/PyROOTWrapper.cxx | 42 ------------------- .../pyroot/pythonizations/src/PyROOTWrapper.h | 24 ----------- 4 files changed, 23 insertions(+), 71 deletions(-) delete mode 100644 bindings/pyroot/pythonizations/src/PyROOTWrapper.cxx delete mode 100644 bindings/pyroot/pythonizations/src/PyROOTWrapper.h diff --git a/bindings/pyroot/pythonizations/CMakeLists.txt b/bindings/pyroot/pythonizations/CMakeLists.txt index 6521c568b496f..eecd7efd3d058 100644 --- a/bindings/pyroot/pythonizations/CMakeLists.txt +++ b/bindings/pyroot/pythonizations/CMakeLists.txt @@ -114,7 +114,6 @@ set(py_sources set(cpp_sources src/PyROOTModule.cxx - src/PyROOTWrapper.cxx src/RPyROOTApplication.cxx src/GenericPyz.cxx src/TClassPyz.cxx diff --git a/bindings/pyroot/pythonizations/src/PyROOTModule.cxx b/bindings/pyroot/pythonizations/src/PyROOTModule.cxx index 7ccd1d6d86d69..13873dfe2623f 100644 --- a/bindings/pyroot/pythonizations/src/PyROOTModule.cxx +++ b/bindings/pyroot/pythonizations/src/PyROOTModule.cxx @@ -11,8 +11,8 @@ // Bindings #include "PyROOTPythonize.h" -#include "PyROOTWrapper.h" #include "RPyROOTApplication.h" +#include "TMemoryRegulator.h" // Cppyy #include "CPyCppyy/API.h" @@ -34,7 +34,7 @@ namespace PyROOT { -PyObject *gRootModule = nullptr; +R__EXTERN PyObject *gRootModule = nullptr; PyObject *RegisterConverterAlias(PyObject * /*self*/, PyObject *args) { @@ -62,6 +62,20 @@ PyObject *RegisterExecutorAlias(PyObject * /*self*/, PyObject *args) } // namespace PyROOT +namespace { + +PyROOT::RegulatorCleanup &GetRegulatorCleanup() +{ + // The object is thread-local because it can happen that we call into + // C++ code (from the PyROOT CPython extension, from CPyCppyy or from cling) + // from different Python threads. A notable example is within a distributed + // RDataFrame application running on Dask. + thread_local PyROOT::RegulatorCleanup m; + return m; +} + +} // namespace + // Methods offered by the interface static PyMethodDef gPyROOTMethods[] = { {(char *)"AddCPPInstancePickling", (PyCFunction)PyROOT::AddCPPInstancePickling, METH_VARARGS, @@ -144,8 +158,13 @@ extern "C" PyObject *PyInit_libROOTPythonizations() // keep gRootModule, but do not increase its reference count even as it is borrowed, // or a self-referencing cycle would be created - // setup PyROOT - PyROOT::Init(); + // Initialize and acquire the GIL to allow for threading in ROOT +#if PY_VERSION_HEX < 0x03090000 + PyEval_InitThreads(); +#endif + + // Memory management + gROOT->GetListOfCleanups()->Add(&GetRegulatorCleanup()); // signal policy: don't abort interpreter in interactive mode CallContext::SetGlobalSignalPolicy(!gROOT->IsBatch()); diff --git a/bindings/pyroot/pythonizations/src/PyROOTWrapper.cxx b/bindings/pyroot/pythonizations/src/PyROOTWrapper.cxx deleted file mode 100644 index 26cd81d9d9ee3..0000000000000 --- a/bindings/pyroot/pythonizations/src/PyROOTWrapper.cxx +++ /dev/null @@ -1,42 +0,0 @@ -// Author: Enric Tejedor CERN 06/2018 -// Original PyROOT code by Wim Lavrijsen, LBL - -/************************************************************************* - * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. * - * All rights reserved. * - * * - * For the licensing terms see $ROOTSYS/LICENSE. * - * For the list of contributors see $ROOTSYS/README/CREDITS. * - *************************************************************************/ - -// Bindings -#include "PyROOTWrapper.h" -#include "TMemoryRegulator.h" - -// ROOT -#include "TROOT.h" - -namespace PyROOT { -R__EXTERN PyObject *gRootModule; -} - -PyROOT::RegulatorCleanup &GetRegulatorCleanup() -{ - // The object is thread-local because it can happen that we call into - // C++ code (from the PyROOT CPython extension, from CPyCppyy or from cling) - // from different Python threads. A notable example is within a distributed - // RDataFrame application running on Dask. - thread_local PyROOT::RegulatorCleanup m; - return m; -} - -void PyROOT::Init() -{ - // Initialize and acquire the GIL to allow for threading in ROOT -#if PY_VERSION_HEX < 0x03090000 - PyEval_InitThreads(); -#endif - - // Memory management - gROOT->GetListOfCleanups()->Add(&GetRegulatorCleanup()); -} diff --git a/bindings/pyroot/pythonizations/src/PyROOTWrapper.h b/bindings/pyroot/pythonizations/src/PyROOTWrapper.h deleted file mode 100644 index 2847bf6f404ad..0000000000000 --- a/bindings/pyroot/pythonizations/src/PyROOTWrapper.h +++ /dev/null @@ -1,24 +0,0 @@ -// Author: Enric Tejedor CERN 06/2018 -// Original PyROOT code by Wim Lavrijsen, LBL - -/************************************************************************* - * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. * - * All rights reserved. * - * * - * For the licensing terms see $ROOTSYS/LICENSE. * - * For the list of contributors see $ROOTSYS/README/CREDITS. * - *************************************************************************/ - -#ifndef PYROOT_ROOTWRAPPER_H -#define PYROOT_ROOTWRAPPER_H - -#include "Python.h" - -namespace PyROOT { - -// initialize ROOT -void Init(); - -} // namespace PyROOT - -#endif // !PYROOT_ROOTWRAPPER_H