From 817c988880139cc676d3d00ab4bf6b3bc73b2f5a Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Mon, 16 Sep 2024 15:54:13 -0400 Subject: [PATCH 1/4] fix running under radian closes #1668 --- src/python.cpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/python.cpp b/src/python.cpp index 483a561d..33fa171f 100644 --- a/src/python.cpp +++ b/src/python.cpp @@ -2844,12 +2844,10 @@ SEXP main_process_python_info_unix() { return R_NilValue; } - - if (PyGILState_Ensure == NULL) - loadSymbol(pLib, "PyGILState_Ensure", (void**)&PyGILState_Ensure); - - if (PyGILState_Release == NULL) + if (PyGILState_Release == NULL) { loadSymbol(pLib, "PyGILState_Release", (void**)&PyGILState_Release); + loadSymbol(pLib, "PyGILState_Ensure", (void**)&PyGILState_Ensure); + } GILScope scope; @@ -2930,6 +2928,19 @@ void py_initialize(const std::string& python, if (Py_IsInitialized()) { // if R is embedded in a python environment, rpycall has to be loaded as a regular // module. + + // We don't currently support running embedded on windows? + #ifndef _WIN32 + void* pLib = NULL; + pLib = ::dlopen(NULL, RTLD_NOW | RTLD_GLOBAL); + // replace symbols that we initialized with dummy functions + // in reticulate_init + loadSymbol(pLib, "PyIter_Check", (void**) &PyIter_Check); + loadSymbol(pLib, "PyCallable_Check", (void**) &PyCallable_Check); + loadSymbol(pLib, "PyGILState_Ensure", (void**) &PyGILState_Ensure); + ::dlclose(pLib); + #endif + GILScope scope; PyImport_AddModule("rpycall"); PyDict_SetItemString(PyImport_GetModuleDict(), "rpycall", initializeRPYCall()); From 69f0d759b5e924995f00ae4213968ba63b58d0ba Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Mon, 16 Sep 2024 17:44:58 -0400 Subject: [PATCH 2/4] better fix --- src/python.cpp | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/src/python.cpp b/src/python.cpp index 33fa171f..53210d53 100644 --- a/src/python.cpp +++ b/src/python.cpp @@ -99,18 +99,6 @@ bool is_interactive() { return s_isInteractive; } -// a simplified version of loadSymbol adopted from libpython.cpp -void loadSymbol(void* pLib, const std::string& name, void** ppSymbol) -{ - *ppSymbol = NULL; -#ifdef _WIN32 - *ppSymbol = (void*) ::GetProcAddress((HINSTANCE)pLib, name.c_str()); -#else - *ppSymbol = ::dlsym(pLib, name.c_str()); -#endif -} - - // track whether we have required numpy std::string s_numpy_load_error; bool haveNumPy() { @@ -2811,6 +2799,13 @@ SEXP main_process_python_info_win32() { #else +// a simplified version of loadSymbol adopted from libpython.cpp +void loadSymbol(void* pLib, const std::string& name, void** ppSymbol) +{ + *ppSymbol = NULL; + *ppSymbol = ::dlsym(pLib, name.c_str()); +} + SEXP main_process_python_info_unix() { // bail early if we already know that Python symbols are not available @@ -2845,6 +2840,7 @@ SEXP main_process_python_info_unix() { } if (PyGILState_Release == NULL) { + // PyGILState_Ensure is always not NULL, since we set it in reticulate_init() loadSymbol(pLib, "PyGILState_Release", (void**)&PyGILState_Release); loadSymbol(pLib, "PyGILState_Ensure", (void**)&PyGILState_Ensure); } @@ -2929,17 +2925,6 @@ void py_initialize(const std::string& python, // if R is embedded in a python environment, rpycall has to be loaded as a regular // module. - // We don't currently support running embedded on windows? - #ifndef _WIN32 - void* pLib = NULL; - pLib = ::dlopen(NULL, RTLD_NOW | RTLD_GLOBAL); - // replace symbols that we initialized with dummy functions - // in reticulate_init - loadSymbol(pLib, "PyIter_Check", (void**) &PyIter_Check); - loadSymbol(pLib, "PyCallable_Check", (void**) &PyCallable_Check); - loadSymbol(pLib, "PyGILState_Ensure", (void**) &PyGILState_Ensure); - ::dlclose(pLib); - #endif GILScope scope; PyImport_AddModule("rpycall"); From 3e54e9de0cf1cb4039bc6be92d0a12413cf9d458 Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Mon, 16 Sep 2024 17:47:25 -0400 Subject: [PATCH 3/4] whitespace --- src/python.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/python.cpp b/src/python.cpp index 53210d53..8768073f 100644 --- a/src/python.cpp +++ b/src/python.cpp @@ -2800,8 +2800,7 @@ SEXP main_process_python_info_win32() { #else // a simplified version of loadSymbol adopted from libpython.cpp -void loadSymbol(void* pLib, const std::string& name, void** ppSymbol) -{ +void loadSymbol(void* pLib, const std::string& name, void** ppSymbol) { *ppSymbol = NULL; *ppSymbol = ::dlsym(pLib, name.c_str()); } @@ -2840,8 +2839,8 @@ SEXP main_process_python_info_unix() { } if (PyGILState_Release == NULL) { - // PyGILState_Ensure is always not NULL, since we set it in reticulate_init() loadSymbol(pLib, "PyGILState_Release", (void**)&PyGILState_Release); + // PyGILState_Ensure is always not NULL, since we set it in reticulate_init() loadSymbol(pLib, "PyGILState_Ensure", (void**)&PyGILState_Ensure); } @@ -2924,8 +2923,6 @@ void py_initialize(const std::string& python, if (Py_IsInitialized()) { // if R is embedded in a python environment, rpycall has to be loaded as a regular // module. - - GILScope scope; PyImport_AddModule("rpycall"); PyDict_SetItemString(PyImport_GetModuleDict(), "rpycall", initializeRPYCall()); From f248072158610a14a84af7ad292d387acbbfc05e Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Mon, 16 Sep 2024 17:49:29 -0400 Subject: [PATCH 4/4] add NEWS --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index fdb0ef4a..7d7ba883 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # reticulate (development version) +- Fixed error when using reticulate with radian (#1668, #1670). + - Fixed segfault encountered when running Python finalizer (#1663, #1664) - Fixed segfault encountered in RStudio when rapidly switching