From 11fc42d644cb61f697d6eed16d7273eabfa706bd Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Tue, 19 Mar 2024 11:30:13 -0400 Subject: [PATCH] more tests --- src/python.cpp | 5 ++++- tests/testthat/test-python-objects.R | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/python.cpp b/src/python.cpp index 2223d67a6..736c096df 100644 --- a/src/python.cpp +++ b/src/python.cpp @@ -488,7 +488,6 @@ class PyErrorScopeGuard { PyErr_Restore(er_type, er_value, er_traceback); } }; - // copied directly from purrr; used to call rlang::trace_back() in // py_fetch_error() in such a way that it doesn't introduce a new // frame in returned traceback @@ -519,6 +518,8 @@ SEXP current_env(void) { UNPROTECT(3); } + // Rf_PrintValue(get_r_trace(false, false)); + return Rf_eval(call, R_BaseEnv); } @@ -542,6 +543,8 @@ SEXP eval_call_in_userenv(SEXP r_func, SEXP arg) { SEXP cl = Rf_lang2(r_func, arg); RObject cl_(cl); // protect return Rf_eval(cl, current_env()); + // this sometimes returns the reticulate ns env + // we need a new func, current_user_env(), that walks the frames, skipping reticulate ns frames. } SEXP eval_call_in_userenv(SEXP r_func, SEXP arg1, SEXP arg2) { diff --git a/tests/testthat/test-python-objects.R b/tests/testthat/test-python-objects.R index e42d75ee0..8b978eabc 100644 --- a/tests/testthat/test-python-objects.R +++ b/tests/testthat/test-python-objects.R @@ -180,7 +180,18 @@ def py_new_callback_caller(callback): callback_caller(od) # conversion via user S3 method - py_to_r.__main__.MyFoo <- function(x) list(a = 42) + # ideally we would test by just defining `py_to_r.__main__.MyFoo` in the calling + # env like this: + # (function() { + # py_to_r.__main__.MyFoo <- function(x) list(a = 42) + # callback_caller(new_MyFoo()) + # })() + # + # unfortunately, our ability to infer the userenv needs a little work. + # we register the S3 method directly as an alternative. + new_MyFoo <- py_eval("type('MyFoo', (), {})") + registerS3method("py_to_r", "__main__.MyFoo", function(x) list(a = 42), + asNamespace("reticulate")) new_MyFoo <- py_eval("type('MyFoo', (), {})") callback_caller(new_MyFoo())