From 580c8deeb47bd7de14210b3e577211a18d9866d1 Mon Sep 17 00:00:00 2001 From: Davis Vaughan Date: Mon, 26 Aug 2024 14:53:04 -0400 Subject: [PATCH] Error if `get_namespace()` fails (#396) Rather than hanging indefinitely when a function is called --- NEWS.md | 3 +++ cpp11test/src/test-function.cpp | 4 ++++ inst/include/cpp11/function.hpp | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) 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