@@ -189,16 +189,14 @@ eng_python_initialize <- function(options, context, envir) {
189189 use_python(options $ engine.path [[1 ]])
190190
191191 if (options $ cache > 0 ) {
192- load_module <- tryCatch(
193- import(" dill" ),
194- error = function (c ) {
195- py_error <- py_last_error()
196- if (py_error $ type == " ImportError" && py_error $ value == " No module named dill" ) {
197- warning(" The Python module dill was not found. This module is needed for full cache functionality." )
198- " No dill"
199- }})
200- if (load_module != " No dill" )
201- py_run_string(" import dill" )
192+ module <- tryCatch(import(" dill" ), error = identity )
193+ if (inherits(module , " error" )) {
194+ if (module $ message == " ImportError: No module named dill" ) {
195+ warning(" The Python module dill was not found. This module is needed for full cache functionality." )
196+ } else {
197+ stop(module $ message )
198+ }
199+ }
202200 }
203201 eng_python_initialize_matplotlib(options , context , envir )
204202}
@@ -292,18 +290,20 @@ eng_python_wrap <- function(outputs, options) {
292290}
293291
294292save_python_session <- function (cache_path ) {
295- dill <- tryCatch(
296- import(" dill" ),
297- error = function (c ) {
298- py_error <- py_last_error()
299- if (py_error $ type == " ImportError" && py_error $ value == " No module named dill" ) {
300- " No dill"
301- }})
302- if (dill == " No dill" ) return ()
303-
304- py_run_string(" globals().pop('r')" )
305- py_run_string(" globals().pop('R')" )
306- dill $ dump_session(filename = paste0(cache_path , " .pkl" ), byref = TRUE )
293+ module <- tryCatch(import(" dill" ), error = identity )
294+ if (inherits(module , " error" )) {
295+ if (module $ message == " ImportError: No module named dill" ) return ()
296+ signalCondition(module $ message )
297+ }
298+
299+ r_objs_exist <- " all(r_obj in globals() for r_obj in ('r', 'R'))"
300+ r_is_R <- " isinstance(r, R)"
301+ if (py_eval(r_objs_exist ) && py_eval(r_is_R )) {
302+ py_run_string(" globals().pop('r')" )
303+ py_run_string(" globals().pop('R')" )
304+ }
305+
306+ module $ dump_session(filename = paste0(cache_path , " .pkl" ), byref = TRUE )
307307}
308308
309309# ' A reticulate cache engine for Knitr
@@ -327,15 +327,13 @@ save_python_session <- function(cache_path) {
327327# '
328328# ' @export
329329cache_eng_python <- function (cache_path ) {
330- dill <- tryCatch(
331- import(" dill" ),
332- error = function (c ) {
333- py_error <- py_last_error()
334- if (py_error $ type == " ImportError" && py_error $ value == " No module named dill" ) {
335- " No dill"
336- }})
337- if (dill == " No dill" ) return ()
338- dill $ load_session(filename = paste0(cache_path , " .pkl" ))
330+ module <- tryCatch(import(" dill" ), error = identity )
331+ if (inherits(module , " error" )) {
332+ if (module $ message == " ImportError: No module named dill" ) return ()
333+ stop(module $ message )
334+ }
335+
336+ module $ load_session(filename = paste0(cache_path , " .pkl" ))
339337}
340338
341339
0 commit comments