Skip to content

Commit

Permalink
Fix a bug (sic!) in bug fix #515 + add debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Jul 29, 2021
1 parent 956d1b3 commit be9c95c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion R/UniprocessFuture-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ run.UniprocessFuture <- function(future, ...) {
## Assign globals to separate "globals" enclosure environment?
globals <- future$globals
if (length(globals) > 0) {
envir <- assign_globals(envir, globals = globals)
envir <- assign_globals(envir, globals = globals, debug = debug)
}

## Run future
Expand Down
28 changes: 23 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,32 +145,50 @@ inherits_from_namespace <- function(env) {
## for functions. If they are functions of namespaces/packages
## and exclude == "namespace", then the globals are not assigned
## Reference: https://github.com/HenrikBengtsson/future/issues/515
assign_globals <- function(envir, globals, exclude = getOption("future.assign_globals.exclude", c("namespace"))) {
assign_globals <- function(envir, globals, exclude = getOption("future.assign_globals.exclude", c("namespace")), debug = getOption("future.debug", FALSE)) {
stop_if_not(is.environment(envir), is.list(globals))
if (length(globals) == 0L) return(envir)

if (debug) {
mdebug("assign_globals() ...")
mstr(globals)
}

exclude_namespace <- ("namespace" %in% exclude)

names <- names(globals)
where <- attr(globals, "where")
for (name in names) {
global <- globals[[name]]

if (exclude_namespace) {
e <- environment(global)
if (!inherits_from_namespace(e)) {
where <- where[[name]]
w <- where[[name]]
## FIXME: Can we remove this?
## Here I'm just being overly conservative ## /HB 2021-06-15
if (identical(where, emptyenv())) {
if (identical(w, emptyenv())) {
environment(global) <- envir
if (debug) {
mdebugf("- reassign environment for %s", sQuote(name))
where[[name]] <- envir
globals[[name]] <- global
}
}
}
}

envir[[name]] <- global
if (debug) mdebugf("- copied %s to environment", sQuote(name))
}



if (debug) {
attr(globals, "where") <- where
mstr(globals)
mdebug("assign_globals() ... done")
}

invisible(envir)
}

Expand Down

0 comments on commit be9c95c

Please sign in to comment.