Skip to content

Commit 8bd23bd

Browse files
author
maechler
committed
no longer use require() in low-level code needing namespace
git-svn-id: https://svn.r-project.org/R/trunk@87247 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent fb616a1 commit 8bd23bd

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

doc/NEWS.Rd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@
103103
convert row names used for indexing using \code{I()}, which will
104104
lead to faster execution in cases where \code{sort = TRUE} and
105105
\code{all.x} and/or \code{all.y} are set to \code{TRUE}.
106+
107+
\item \pkg{methods} package internal \code{.requirePackage()} now
108+
calls \code{requireNamespace(p)} instead of \code{require(p)}, hence
109+
no longer adding packages to the \code{search()} path in cases
110+
methods or class definitions are needed. Consequently, previous
111+
workflows relying on the old behaviour will have to be amended by
112+
adding corresponding \code{library(p)} calls.
106113
}
107114
}
108115

src/library/methods/R/RClassUtils.R

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,46 +1807,41 @@ substituteFunctionArgs <-
18071807

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

18131813
## real version of .requirePackage
1814-
..requirePackage <- function(package, mustFind = TRUE) {
1815-
value <- package
1814+
..requirePackage <- function(package, mustFind = TRUE, quietly = FALSE) {
18161815
if(nzchar(package)) {
18171816
## lookup as lightning fast as possible:
1818-
if (.Internal(exists(package, .Internal(getNamespaceRegistry()),
1819-
"any", FALSE)))
1820-
value <- getNamespace(package)
1817+
if(!is.null(ns <-.Internal(getRegisteredNamespace(package))))
1818+
return(ns)
18211819
else {
18221820
if(identical(package, ".GlobalEnv"))
18231821
return(.GlobalEnv)
18241822
if(identical(package, "methods"))
18251823
return(topenv(parent.frame())) # booting methods
1824+
## else continue
18261825
}
18271826
}
1828-
if(is.environment(value))
1829-
return(value)
1830-
topEnv <- getOption("topLevelEnvironment")
1831-
if(is.null(topEnv))
1832-
topEnv <- .GlobalEnv
1827+
topEnv <- getOption("topLevelEnvironment", default = .GlobalEnv)
18331828
if(!is.null(pkgN <- get0(".packageName", topEnv, inherits=TRUE)) &&
18341829
.identC(package, pkgN))
18351830
return(topEnv) # kludge for source'ing package code
1831+
18361832
## If called from .findInheritedMethods which disables S4 primitive dispatch,
18371833
## allow it here, as namespace loading hooks may need it:
18381834
if(!.allowPrimitiveMethods(TRUE))
18391835
on.exit(.allowPrimitiveMethods(FALSE))
1840-
if(nzchar(package) && require(package, character.only = TRUE)) {}
1836+
if(nzchar(package) && requireNamespace(package, quietly=quietly))
1837+
getNamespace(package)
18411838
else {
18421839
if(mustFind)
18431840
stop(gettextf("unable to load required package %s",
18441841
sQuote(package)),
18451842
domain = NA)
1846-
else
1847-
return(NULL)
18481843
}
1849-
getNamespace(package)
1844+
## else return(NULL)
18501845
}
18511846

18521847
.classDefEnv <- function(classDef) {

0 commit comments

Comments
 (0)