Skip to content

Commit

Permalink
Merge pull request #255 from klmr/fix/active-bindings
Browse files Browse the repository at this point in the history
Fix handling of active bindings defined in packages
  • Loading branch information
lionel- committed Sep 19, 2023
2 parents 1ea929e + 07c138a commit 28ab413
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# pkgload (development version)

* Fix handling of active bindings inside a package during unloading (#255, @klmr).


# pkgload 1.3.3

* pkgload now depends unconditionally on pkgbuild (#249).
Expand Down
7 changes: 6 additions & 1 deletion R/namespace-env.R
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,12 @@ unregister_namespace <- function(name = NULL) {
# unloaded, it might lead to decompress errors if unloaded or to
# inconsistencies if reloaded (the bindings are resolved in the new
# namespace).
eapply(ns_env(name), force, all.names = TRUE)
#
# We take precautions not to trigger active bindings in case these
# have side effects such as throwing an error.
ns <- ns_env(name)
active_bindings <- env_binding_are_active(ns)
env_get_list(ns, names(active_bindings)[!active_bindings])

# Remove the item from the registry
env_unbind(ns_registry_env(), name)
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-load.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ test_that("unloading or reloading forces bindings", {
)
})

test_that("unloading or reloading does not call active bindings", {
on.exit(unload("testActiveBindings"))

expect_no_error(load_all(test_path("testActiveBindings")))
})

test_that("reloading a package unloads deleted S3 methods", {
x <- structure(list(), class = "pkgload_foobar")

Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/testActiveBindings/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Package: testActiveBindings
Title: Test package with active bindings
License: MIT
Author: Konrad Rudolph <konrad.rudolph@gmail.com>
Maintainer: Konrad Rudolph <konrad.rudolph@gmail.com>
Version: 1.0
3 changes: 3 additions & 0 deletions tests/testthat/testActiveBindings/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(bar)
5 changes: 5 additions & 0 deletions tests/testthat/testActiveBindings/R/bindings.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

makeActiveBinding("foo", function() rlang::abort("foo"), environment())

#' @export
makeActiveBinding("bar", function() rlang::abort("bar"), environment())

0 comments on commit 28ab413

Please sign in to comment.