@@ -28,14 +28,20 @@ using namespace Rcpp;
2828
2929using namespace reticulate ::libpython;
3030
31+ SEXP ns_reticulate;
32+
33+ inline SEXP reticulate_get_ns_var (const char * name) {
34+ return reticulate_get_var (Rf_install (name), ns_reticulate);
35+ }
36+
3137int _Py_Check (PyObject* o) {
3238 // default impl we assign to some Python api functions until we've initialized Python;
3339 return 0 ;
3440}
3541
3642
3743PyGILState_STATE _initialize_python_and_PyGILState_Ensure () {
38- Function initialize_python = Environment::namespace_env ( " reticulate " )[ " ensure_python_initialized" ] ;
44+ Function initialize_python ( reticulate_get_ns_var ( " ensure_python_initialized" )) ;
3945 initialize_python ();
4046 return PyGILState_Ensure ();
4147}
@@ -45,14 +51,14 @@ SEXP sym_py_object;
4551SEXP sym_simple;
4652SEXP sym_convert;
4753
48- SEXP ns_reticulate;
49-
5054SEXP r_func_py_filter_classes;
5155SEXP r_func_get_r_trace;
5256SEXP r_func_py_callable_as_function;
5357SEXP r_func_r_to_py;
5458SEXP r_func_py_to_r;
5559SEXP r_func_py_to_r_wrapper;
60+ SEXP r_func_r_convert_dataframe_column;
61+ SEXP r_func_py_resolve_module_proxy;
5662
5763tthread::thread::id s_main_thread = 0 ;
5864
@@ -69,14 +75,16 @@ void reticulate_init(DllInfo *dll) {
6975 sym_convert = Rf_install (" convert" );
7076 sym_pyobj = Rf_install (" pyobj" );
7177
72- ns_reticulate = reticulate_get_var_in_frame (R_NamespaceRegistry, Rf_install ( " reticulate" ) );
78+ ns_reticulate = reticulate_find_namespace ( " reticulate" );
7379
74- r_func_py_filter_classes = reticulate_get_var (Rf_install (" py_filter_classes" ), ns_reticulate);
75- r_func_py_callable_as_function = reticulate_get_var (Rf_install (" py_callable_as_function" ), ns_reticulate);
76- r_func_r_to_py = reticulate_get_var (Rf_install (" r_to_py" ), ns_reticulate);
77- r_func_py_to_r = reticulate_get_var (Rf_install (" py_to_r" ), ns_reticulate);
78- r_func_py_to_r_wrapper = reticulate_get_var (Rf_install (" py_to_r_wrapper" ), ns_reticulate);
79- r_func_get_r_trace = reticulate_get_var (Rf_install (" get_r_trace" ), ns_reticulate);
80+ r_func_py_filter_classes = reticulate_get_ns_var (" py_filter_classes" );
81+ r_func_py_callable_as_function = reticulate_get_ns_var (" py_callable_as_function" );
82+ r_func_r_to_py = reticulate_get_ns_var (" r_to_py" );
83+ r_func_py_to_r = reticulate_get_ns_var (" py_to_r" );
84+ r_func_py_to_r_wrapper = reticulate_get_ns_var (" py_to_r_wrapper" );
85+ r_func_get_r_trace = reticulate_get_ns_var (" get_r_trace" );
86+ r_func_r_convert_dataframe_column = reticulate_get_ns_var (" r_convert_dataframe_column" );
87+ r_func_py_resolve_module_proxy = reticulate_get_ns_var (" py_resolve_module_proxy" );
8088
8189 s_main_thread = tthread::this_thread::get_id ();
8290}
@@ -924,8 +932,7 @@ bool option_is_true(const std::string& name) {
924932}
925933
926934bool traceback_enabled () {
927- Environment pkgEnv = Environment::namespace_env (" reticulate" );
928- Function func = pkgEnv[" traceback_enabled" ];
935+ Function func (reticulate_get_ns_var (" traceback_enabled" ));
929936 return as<bool >(func ());
930937}
931938
@@ -1150,8 +1157,7 @@ std::string conditionMessage_from_py_exception(PyObject* exc) {
11501157 oss << as_std_string (PyList_GetItem (formatted, i));
11511158
11521159 static std::string hint = []() {
1153- Environment pkg_env (Environment::namespace_env (" reticulate" ));
1154- Function hint_fn = pkg_env[" .py_last_error_hint" ];
1160+ Function hint_fn (reticulate_get_ns_var (" .py_last_error_hint" ));
11551161 CharacterVector r_result = hint_fn ();
11561162 return Rcpp::as<std::string>(r_result[0 ]);
11571163 }();
@@ -3898,16 +3904,17 @@ PyObjectRef py_module_import(const std::string& module, bool convert) {
38983904
38993905// [[Rcpp::export]]
39003906void py_module_proxy_import (PyObjectRef proxy) {
3901- Rcpp::Environment refenv = proxy.get_refenv ();
3902- if (refenv.exists (" module" )) {
3907+ SEXP refenv = proxy.get_refenv ();
3908+ SEXP module_sym = Rf_install (" module" );
3909+ SEXP r_module = reticulate_get_var_or_null (refenv, module_sym);
3910+ if (r_module != NULL ) {
39033911 GILScope _gil;
3904- Rcpp::RObject r_module = refenv.get (" module" );
3905- std::string module = as<std::string>(r_module);
3912+ std::string module = as<std::string>(Rcpp::RObject (r_module));
39063913 PyObject* pModule = py_import (module );
39073914 if (pModule == NULL )
39083915 throw PythonException (py_fetch_error ());
39093916 proxy.set (pModule);
3910- refenv. remove ( " module " );
3917+ R_removeVarFromFrame (module_sym, refenv );
39113918 }// else, if !exists("module", <refenv>),
39123919 // then we're unwinding a recursive py_resolve_module_proxy() call, e.g.:
39133920 // -> py_resolve_module_proxy() -> import() -> py_module_onload() ->
@@ -4417,8 +4424,7 @@ PyObject* r_to_py_pandas_nullable_series (const RObject& column, const bool conv
44174424// [[Rcpp::export]]
44184425PyObjectRef r_convert_dataframe (RObject dataframe, bool convert) {
44194426 GILScope _gil;
4420- Function r_convert_dataframe_column =
4421- Environment::namespace_env (" reticulate" )[" r_convert_dataframe_column" ];
4427+ Function r_convert_dataframe_column (r_func_r_convert_dataframe_column);
44224428
44234429 PyObjectPtr dict (PyDict_New ());
44244430
@@ -4871,8 +4877,7 @@ SEXP py_iterate(PyObjectRef x, Function f, bool simplify = true) {
48714877
48724878
48734879bool try_py_resolve_module_proxy (SEXP proxy) {
4874- Rcpp::Environment pkgEnv = Rcpp::Environment::namespace_env (" reticulate" );
4875- Rcpp::Function py_resolve_module_proxy = pkgEnv[" py_resolve_module_proxy" ];
4880+ Rcpp::Function py_resolve_module_proxy (r_func_py_resolve_module_proxy);
48764881 return py_resolve_module_proxy (proxy);
48774882}
48784883
0 commit comments