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