Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kalinowski committed Mar 19, 2024
1 parent 82048e9 commit 11fc42d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -519,6 +518,8 @@ SEXP current_env(void) {
UNPROTECT(3);
}

// Rf_PrintValue(get_r_trace(false, false));

return Rf_eval(call, R_BaseEnv);
}

Expand All @@ -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) {
Expand Down
13 changes: 12 additions & 1 deletion tests/testthat/test-python-objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down

0 comments on commit 11fc42d

Please sign in to comment.