Skip to content

Commit

Permalink
no longer use require() in low-level code needing namespace
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.r-project.org/R/trunk@87247 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
maechler committed Oct 19, 2024
1 parent fb616a1 commit 8bd23bd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
7 changes: 7 additions & 0 deletions doc/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@
convert row names used for indexing using \code{I()}, which will
lead to faster execution in cases where \code{sort = TRUE} and
\code{all.x} and/or \code{all.y} are set to \code{TRUE}.

\item \pkg{methods} package internal \code{.requirePackage()} now
calls \code{requireNamespace(p)} instead of \code{require(p)}, hence
no longer adding packages to the \code{search()} path in cases
methods or class definitions are needed. Consequently, previous
workflows relying on the old behaviour will have to be amended by
adding corresponding \code{library(p)} calls.
}
}

Expand Down
25 changes: 10 additions & 15 deletions src/library/methods/R/RClassUtils.R
Original file line number Diff line number Diff line change
Expand Up @@ -1807,46 +1807,41 @@ substituteFunctionArgs <-

## bootstrap version: all classes and methods must be in the version of the methods
## package being built in the toplevel environment: MUST avoid require("methods") !
.requirePackage <- function(package, mustFind = TRUE)
.requirePackage <- function(package, mustFind = TRUE, quietly = FALSE)
topenv(parent.frame())

## real version of .requirePackage
..requirePackage <- function(package, mustFind = TRUE) {
value <- package
..requirePackage <- function(package, mustFind = TRUE, quietly = FALSE) {
if(nzchar(package)) {
## lookup as lightning fast as possible:
if (.Internal(exists(package, .Internal(getNamespaceRegistry()),
"any", FALSE)))
value <- getNamespace(package)
if(!is.null(ns <-.Internal(getRegisteredNamespace(package))))
return(ns)
else {
if(identical(package, ".GlobalEnv"))
return(.GlobalEnv)
if(identical(package, "methods"))
return(topenv(parent.frame())) # booting methods
## else continue
}
}
if(is.environment(value))
return(value)
topEnv <- getOption("topLevelEnvironment")
if(is.null(topEnv))
topEnv <- .GlobalEnv
topEnv <- getOption("topLevelEnvironment", default = .GlobalEnv)
if(!is.null(pkgN <- get0(".packageName", topEnv, inherits=TRUE)) &&
.identC(package, pkgN))
return(topEnv) # kludge for source'ing package code

## If called from .findInheritedMethods which disables S4 primitive dispatch,
## allow it here, as namespace loading hooks may need it:
if(!.allowPrimitiveMethods(TRUE))
on.exit(.allowPrimitiveMethods(FALSE))
if(nzchar(package) && require(package, character.only = TRUE)) {}
if(nzchar(package) && requireNamespace(package, quietly=quietly))
getNamespace(package)
else {
if(mustFind)
stop(gettextf("unable to load required package %s",
sQuote(package)),
domain = NA)
else
return(NULL)
}
getNamespace(package)
## else return(NULL)
}

.classDefEnv <- function(classDef) {
Expand Down

0 comments on commit 8bd23bd

Please sign in to comment.