Skip to content

Commit

Permalink
TESTS: add test for name clashing of globals in local() environments [#…
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed May 8, 2022
1 parent 968ec7f commit 121566e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: future
Version: 1.25.0-9013
Version: 1.25.0-9014
Title: Unified Parallel and Distributed Processing in R for Everyone
Imports:
digest,
Expand Down
2 changes: 1 addition & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: future
===============

Version: 1.25.0-9013 [2022-05-07]
Version: 1.25.0-9014 [2022-05-07]

SIGNIFICANT CHANGES:

Expand Down
2 changes: 2 additions & 0 deletions R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ getGlobalsAndPackages <- function(expr, envir = parent.frame(), tweak = tweakExp
globals <- globalsOf(
## Passed to globals::findGlobals()
expr, envir = envir, substitute = FALSE, tweak = tweak,
## Requires globals (>= 0.14.0.9004); ignored otherwise
locals = getOption("future.globals.globalsOf.locals", FALSE),
## Passed to globals::findGlobals() via '...'
dotdotdot = "return",
method = globals.method,
Expand Down
3 changes: 3 additions & 0 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ update_package_options <- function(debug = FALSE) {
## Prototyping in future 1.23.0:
update_package_option("future.output.windows.reencode", mode = "logical", debug = debug)

## Prototyping in future 1.26.0:
update_package_option("future.globals.globalsOf.locals", mode = "logical", debug = debug)

## SETTINGS USED FOR DEPRECATING FEATURES
## future 1.22.0:
update_package_option("future.globals.keepWhere", mode = "logical", debug = debug)
Expand Down
43 changes: 43 additions & 0 deletions tests/globals,locals.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
source("incl/start.R")
library("listenv")
oopts <- c(oopts, options(
future.globals.resolve = TRUE,
future.globals.onMissing = "error"
))

okeep <- list()

message("*** Globals inside local() environments ...")

for (strategy in supportedStrategies()) {
message(sprintf("- plan('%s') ...", strategy))
plan(strategy)

## Closures with local globals of the same name
g <- local({ a <- 1; function() a })
h <- local({ a <- 2; function() a })
truth <- h() + g()
print(truth)

## Fixed in future (>= 1.25.0-9013) with globals (>= 0.14.0.9004)
## Previously, 'a' of h() would overwrite 'a' of g(), resulting
## in g() == 2, rather than g() == 1.
## https://github.com/HenrikBengtsson/future/issues/608
if (is.null(getOption("future.globals.keepWhere")) && packageVersion("globals") >= "0.14.0.9004") {
okeep <- options(future.globals.keepWhere = TRUE)
}
f <- future(h() + g())
v <- value(f)
print(v)

if (!strategy %in% c("sequential", "multicore") || (packageVersion("globals") >= "0.14.0.9004") && getOption("future.globals.keepWhere", FALSE)) {
stopifnot(identical(v, truth))
options(okeep)
} else {
stopifnot(identical(v, h() + h()))
}
} ## for (strategy ...)

message("*** Globals inside local() environments ... DONE")

source("incl/end.R")

0 comments on commit 121566e

Please sign in to comment.