<commit_before>diff --git a/bindings/pyroot/cppyy/CPyCppyy/src/CPyCppyyModule.cxx b/bindings/pyroot/cppyy/CPyCppyy/src/CPyCppyyModule.cxx
index 2f39146505..3653596167 100644
--- a/bindings/pyroot/cppyy/CPyCppyy/src/CPyCppyyModule.cxx
+++ b/bindings/pyroot/cppyy/CPyCppyy/src/CPyCppyyModule.cxx
@@ -756,12 +756,6 @@ static PyMethodDef gCPyCppyyMethods[] = {
     {nullptr, nullptr, 0, nullptr}
 };
 
-#define QuoteIdent(ident) #ident
-#define QuoteMacro(macro) QuoteIdent(macro)
-#define LIBCPPYY_NAME "libcppyy" QuoteMacro(PY_MAJOR_VERSION) "_" QuoteMacro(PY_MINOR_VERSION)
-
-#define CONCAT(a, b, c, d) a##b##c##d
-#define LIBCPPYY_INIT_FUNCTION(a, b, c, d) CONCAT(a, b, c, d)
 
 #if PY_VERSION_HEX >= 0x03000000
 struct module_state {
@@ -782,9 +776,10 @@ static int cpycppyymodule_clear(PyObject* m)
     return 0;
 }
 
+
 static struct PyModuleDef moduledef = {
     PyModuleDef_HEAD_INIT,
-    LIBCPPYY_NAME,
+    "libcppyy",
     nullptr,
     sizeof(struct module_state),
     gCPyCppyyMethods,
@@ -797,10 +792,10 @@ static struct PyModuleDef moduledef = {
 
 //----------------------------------------------------------------------------
 #define CPYCPPYY_INIT_ERROR return nullptr
-LIBCPPYY_INIT_FUNCTION(extern "C" PyObject* PyInit_libcppyy, PY_MAJOR_VERSION, _, PY_MINOR_VERSION) ()
+extern "C" PyObject* PyInit_libcppyy()
 #else
 #define CPYCPPYY_INIT_ERROR return
-LIBCPPYY_INIT_FUNCTION(extern "C" void initlibcppyy, PY_MAJOR_VERSION, _, PY_MINOR_VERSION) ()
+extern "C" void initlibcppyy()
 #endif
 {
 // Initialization of extension module libcppyy.
@@ -829,7 +824,7 @@ LIBCPPYY_INIT_FUNCTION(extern "C" void initlibcppyy, PY_MAJOR_VERSION, _, PY_MIN
 #if PY_VERSION_HEX >= 0x03000000
     gThisModule = PyModule_Create(&moduledef);
 #else
-    gThisModule = Py_InitModule(const_cast<char*>(LIBCPPYY_NAME), gCPyCppyyMethods);
+    gThisModule = Py_InitModule(const_cast<char*>("libcppyy"), gCPyCppyyMethods);
 #endif
     if (!gThisModule)
         CPYCPPYY_INIT_ERROR;
diff --git a/bindings/pyroot/cppyy/cppyy/python/cppyy/__init__.py b/bindings/pyroot/cppyy/cppyy/python/cppyy/__init__.py
index 1558c3269c..b3b1b84251 100644
--- a/bindings/pyroot/cppyy/cppyy/python/cppyy/__init__.py
+++ b/bindings/pyroot/cppyy/cppyy/python/cppyy/__init__.py
@@ -49,72 +49,23 @@ __all__ = [
 from ._version import __version__
 
 import os, sys, sysconfig, warnings
-import importlib
-
-# import libcppyy with Python version number
-major, minor = sys.version_info[0:2]
-py_version_str = '{}_{}'.format(major, minor)
-libcppyy_mod_name = 'libcppyy' + py_version_str
-
-try:                                                                                                                                                                          
-    importlib.import_module(libcppyy_mod_name)
-except ImportError:
-    raise ImportError(
-            'Failed to import {}. Please check that ROOT has been built for Python {}.{}'.format(
-                libcppyy_mod_name, major, minor))
-
-# ensure 'import libcppyy' will find the versioned module
-sys.modules['libcppyy'] = sys.modules[libcppyy_mod_name]
-
-# tell cppyy that libcppyy_backend is versioned
-def _check_py_version(lib_name, cbl_var):
-    import re
-    if re.match('^libcppyy_backend\d_\d$', lib_name):
-       # library name already has version
-       if lib_name.endswith(py_version_str):
-           return ''
-       else:
-           raise RuntimeError('CPPYY_BACKEND_LIBRARY variable ({})'
-                              ' does not match Python version {}.{}'
-                              .format(cbl_var, major, minor))
-    else:
-       return py_version_str
-
-if 'CPPYY_BACKEND_LIBRARY' in os.environ:
-    clean_cbl = False
-    cbl_var = os.environ['CPPYY_BACKEND_LIBRARY']
-    start = 0
-    last_sep = cbl_var.rfind(os.path.sep)
-    if last_sep >= 0:
-        start = last_sep + 1
-    first_dot = cbl_var.find('.', start)
-    if first_dot >= 0:
-        # lib_name = [/path/to/]libcppyy_backend[py_version_str]
-        # suffix = so | ...
-        lib_name = cbl_var[:first_dot]
-        suff = cbl_var[first_dot+1:]
-        ver = _check_py_version(lib_name[start:], cbl_var)
-        os.environ['CPPYY_BACKEND_LIBRARY'] = '.'.join([
-            lib_name + ver, suff])
-    else:
-        ver = _check_py_version(cbl_var[start:], cbl_var)
-        os.environ['CPPYY_BACKEND_LIBRARY'] += ver
-else:
-    clean_cbl = True
-    os.environ['CPPYY_BACKEND_LIBRARY'] = 'libcppyy_backend' + py_version_str
 
 if not 'CLING_STANDARD_PCH' in os.environ:
     local_pch = os.path.join(os.path.dirname(__file__), 'allDict.cxx.pch')
     if os.path.exists(local_pch):
         os.putenv('CLING_STANDARD_PCH', local_pch)
         os.environ['CLING_STANDARD_PCH'] = local_pch

try:
    import __pypy__
    del __pypy__
    ispypy = True
except ImportError:
    ispypy = False

# import separately instead of in the above try/except block for easier to
# understand tracebacks
if ispypy:
    from ._pypy_cppyy import *
else:
    from ._cpython_cppyy import *
-if clean_cbl:
-    del os.environ['CPPYY_BACKEND_LIBRARY'] # we set it, so clean it<commit_msg>[PyROOT] Redo patch for multi-python<commit_after>diff --git a/bindings/pyroot/cppyy/CPyCppyy/src/CPyCppyyModule.cxx b/bindings/pyroot/cppyy/CPyCppyy/src/CPyCppyyModule.cxx
index b177e90..2f39146 100644
--- a/bindings/pyroot/cppyy/CPyCppyy/src/CPyCppyyModule.cxx
+++ b/bindings/pyroot/cppyy/CPyCppyy/src/CPyCppyyModule.cxx
@@ -756,6 +756,12 @@ static PyMethodDef gCPyCppyyMethods[] = {
     {nullptr, nullptr, 0, nullptr}
 };
 
+#define QuoteIdent(ident) #ident
+#define QuoteMacro(macro) QuoteIdent(macro)
+#define LIBCPPYY_NAME "libcppyy" QuoteMacro(PY_MAJOR_VERSION) "_" QuoteMacro(PY_MINOR_VERSION)
+
+#define CONCAT(a, b, c, d) a##b##c##d
+#define LIBCPPYY_INIT_FUNCTION(a, b, c, d) CONCAT(a, b, c, d)
 
 #if PY_VERSION_HEX >= 0x03000000
 struct module_state {
@@ -778,7 +784,7 @@ static int cpycppyymodule_clear(PyObject* m)
 
 static struct PyModuleDef moduledef = {
     PyModuleDef_HEAD_INIT,
-    "libcppyy",
+    LIBCPPYY_NAME,
     nullptr,
     sizeof(struct module_state),
     gCPyCppyyMethods,
@@ -791,10 +797,10 @@ static struct PyModuleDef moduledef = {
 
 //----------------------------------------------------------------------------
 #define CPYCPPYY_INIT_ERROR return nullptr
-extern "C" PyObject* PyInit_libcppyy()
+LIBCPPYY_INIT_FUNCTION(extern "C" PyObject* PyInit_libcppyy, PY_MAJOR_VERSION, _, PY_MINOR_VERSION) ()
 #else
 #define CPYCPPYY_INIT_ERROR return
-extern "C" void initlibcppyy()
+LIBCPPYY_INIT_FUNCTION(extern "C" void initlibcppyy, PY_MAJOR_VERSION, _, PY_MINOR_VERSION) ()
 #endif
 {
 // Initialization of extension module libcppyy.
@@ -823,7 +829,7 @@ extern "C" void initlibcppyy()
 #if PY_VERSION_HEX >= 0x03000000
     gThisModule = PyModule_Create(&moduledef);
 #else
-    gThisModule = Py_InitModule(const_cast<char*>("libcppyy"), gCPyCppyyMethods);
+    gThisModule = Py_InitModule(const_cast<char*>(LIBCPPYY_NAME), gCPyCppyyMethods);
 #endif
     if (!gThisModule)
         CPYCPPYY_INIT_ERROR;
diff --git a/bindings/pyroot/cppyy/cppyy/python/cppyy/__init__.py b/bindings/pyroot/cppyy/cppyy/python/cppyy/__init__.py
index b3b1b84..0d2fa35 100644
--- a/bindings/pyroot/cppyy/cppyy/python/cppyy/__init__.py
+++ b/bindings/pyroot/cppyy/cppyy/python/cppyy/__init__.py
@@ -50,6 +50,60 @@ from ._version import __version__
 
 import os, sys, sysconfig, warnings
 
+import importlib
+
+# import libcppyy with Python version number
+major, minor = sys.version_info[0:2]
+py_version_str = '{}_{}'.format(major, minor)
+libcppyy_mod_name = 'libcppyy' + py_version_str
+
+try:
+    importlib.import_module(libcppyy_mod_name)
+except ImportError:
+    raise ImportError(
+            'Failed to import {}. Please check that ROOT has been built for Python {}.{}'.format(
+                libcppyy_mod_name, major, minor))
+
+# ensure 'import libcppyy' will find the versioned module
+sys.modules['libcppyy'] = sys.modules[libcppyy_mod_name]
+
+# tell cppyy that libcppyy_backend is versioned
+def _check_py_version(lib_name, cbl_var):
+    import re
+    if re.match('^libcppyy_backend\d_\d$', lib_name):
+       # library name already has version
+       if lib_name.endswith(py_version_str):
+           return ''
+       else:
+           raise RuntimeError('CPPYY_BACKEND_LIBRARY variable ({})'
+                              ' does not match Python version {}.{}'
+                              .format(cbl_var, major, minor))
+    else:
+       return py_version_str
+
+if 'CPPYY_BACKEND_LIBRARY' in os.environ:
+    clean_cbl = False
+    cbl_var = os.environ['CPPYY_BACKEND_LIBRARY']
+    start = 0
+    last_sep = cbl_var.rfind(os.path.sep)
+    if last_sep >= 0:
+        start = last_sep + 1
+    first_dot = cbl_var.find('.', start)
+    if first_dot >= 0:
+        # lib_name = [/path/to/]libcppyy_backend[py_version_str]
+        # suffix = so | ...
+        lib_name = cbl_var[:first_dot]
+        suff = cbl_var[first_dot+1:]
+        ver = _check_py_version(lib_name[start:], cbl_var)
+        os.environ['CPPYY_BACKEND_LIBRARY'] = '.'.join([
+            lib_name + ver, suff])
+    else:
+        ver = _check_py_version(cbl_var[start:], cbl_var)
+        os.environ['CPPYY_BACKEND_LIBRARY'] += ver
+else:
+    clean_cbl = True
+    os.environ['CPPYY_BACKEND_LIBRARY'] = 'libcppyy_backend' + py_version_str
+
 if not 'CLING_STANDARD_PCH' in os.environ:
     local_pch = os.path.join(os.path.dirname(__file__), 'allDict.cxx.pch')
     if os.path.exists(local_pch):
@@ -69,6 +123,8 @@ if ispypy:
     from ._pypy_cppyy import *
 else:
     from ._cpython_cppyy import *
+if clean_cbl:
+    del os.environ['CPPYY_BACKEND_LIBRARY'] # we set it, so clean it


 #- allow importing from gbl --------------------------------------------------
{'fraction_non_alphanumeric': 0.14521384928716904, 'fraction_numerical': 0.02270875763747454, 'mean_word_length': 3.1023391812865495, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2009532', 'n_tokens_mistral': 3818, 'n_tokens_neox': 3626, 'n_words': 673}