diff --git a/NEWS.md b/NEWS.md index a586ef2c..ce6d6715 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/cpp11test/src/test-function.cpp b/cpp11test/src/test-function.cpp index 678a5ee2..68bbf17e 100644 --- a/cpp11test/src/test-function.cpp +++ b/cpp11test/src/test-function.cpp @@ -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); + } } diff --git a/inst/include/cpp11/function.hpp b/inst/include/cpp11/function.hpp index 066ac667..42fbdcf6 100644 --- a/inst/include/cpp11/function.hpp +++ b/inst/include/cpp11/function.hpp @@ -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); } // Either base env or in namespace registry, so no protection needed