Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error if get_namespace() fails #396

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# cpp11 (development version)

* `cpp11::package` now errors if given a package name that hasn't been loaded
yet. Previously it would cause R to hang indefinitely (#317).

* `cpp11::function` now protects its underlying function, for maximum safety
(#294).

Expand Down
4 changes: 4 additions & 0 deletions cpp11test/src/test-function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ context("function-C++") {

close(con);
}

test_that("unknown packages cause an error (#317)") {
expect_error_as(cpp11::package("definitely_not_a_package"), cpp11::unwind_exception);
}
}
2 changes: 1 addition & 1 deletion inst/include/cpp11/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class package {
return R_BaseEnv;
}
sexp name_sexp = safe[Rf_install](name);
return safe[Rf_findVarInFrame](R_NamespaceRegistry, name_sexp);
return safe[detail::r_env_get](R_NamespaceRegistry, name_sexp);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confident about this switch:

  • r_env_get() uses Rf_findVarInFrame3(env, sym, TRUE) on older R
  • Rf_findVarInFrame() is exactly a wrapper around Rf_findVarInFrame3(env, sym, TRUE)

So r_env_get() is basically Rf_findVarInFrame() with a few extra checks - like erroring if the object is not found, as we want here.

On newer R it is using the "blessed" R_getVar() with inherits = false, which is also what we want.

}

// Either base env or in namespace registry, so no protection needed
Expand Down
Loading