Skip to content

Commit 91e4f7a

Browse files
author
lawrence
committed
Start making the methods package more robust to not being on the search path
git-svn-id: https://svn.r-project.org/R/trunk@85659 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 71326e2 commit 91e4f7a

File tree

9 files changed

+32
-22
lines changed

9 files changed

+32
-22
lines changed

doc/NEWS.Rd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,9 @@
353353
354354
\subsection{BUG FIXES}{
355355
\itemize{
356+
\item The methods package is more robust to not being attached to
357+
the search path. More work needs to be done.
358+
356359
\item \code{pairwise.t.test()} misbehaved when subgroups had 0 DF
357360
for variance, even with \code{pool.sd=TRUE} \PR{18594} (Jack Berry).
358361

src/library/methods/R/BasicClasses.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
## not defined at the time these are done).
197197
setMethod("coerce", c("ANY", "ts"), function (from, to, strict = TRUE)
198198
{
199-
value <- as.ts(from)
199+
value <- stats::as.ts(from)
200200
if(strict) {
201201
attrs <- attributes(value)
202202
if(length(attrs) > 2)

src/library/methods/R/ClassExtensions.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
## these cases is less likely but needs to be tested (below) and a suitable
4242
## replace function inserted.
4343
.simpleExtReplace <- function(from, to, value){
44-
for(what in .InhSlotNames(to))
45-
slot(from, what) <- slot(value, what)
44+
for(what in methods:::.InhSlotNames(to))
45+
methods::slot(from, what) <- methods::slot(value, what)
4646
from
4747
}
4848
## slot names for inheritance (to be used in replace methods). Extends slots to implicit
@@ -61,7 +61,7 @@
6161
},
6262

6363
f2 = function(from, to, value){
64-
from@.Data <- as(value, THISCLASS, strict = FALSE)
64+
from@.Data <- methods::as(value, THISCLASS, strict = FALSE)
6565
from
6666
},
6767

@@ -135,12 +135,12 @@ S3Part <- function(object, strictS3 = FALSE, S3Class) {
135135
.S3replace <-
136136
list(e1 =
137137
quote( {
138-
S3Part(from, needClass = NEED) <- value
138+
methods::S3Part(from, needClass = NEED) <- value
139139
from
140140
}),
141141
e2 = quote( {
142-
if(is(value, CLASS)) {
143-
S3Part(from, needClass = NEED) <- value
142+
if(methods::is(value, CLASS)) {
143+
methods::S3Part(from, needClass = NEED) <- value
144144
from
145145
}
146146
else
@@ -152,7 +152,7 @@ S3Part <- function(object, strictS3 = FALSE, S3Class) {
152152
)
153153

154154
.S3coerce <- function(from, to) {
155-
S3Part(from)
155+
methods::S3Part(from)
156156
}
157157

158158
.ErrorReplace <- function(from, to, value)
@@ -207,7 +207,7 @@ makeExtends <- function(Class,
207207
## allNames <- names(slots)
208208
body(coerce, envir = packageEnv) <-
209209
substitute({
210-
if(strict) S3Part(from, S3Class = S3CLASS)
210+
if(strict) methods::S3Part(from, S3Class = S3CLASS)
211211
else from
212212
}, list(S3CLASS = to))
213213
}
@@ -262,7 +262,7 @@ makeExtends <- function(Class,
262262
S3Class <- to
263263
body(replace, envir = packageEnv) <-
264264
quote({
265-
S3Part(from) <- value
265+
methods::S3Part(from) <- value
266266
from
267267
})
268268
}

src/library/methods/R/RClassUtils.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,12 +1596,14 @@ setDataPart <- function(object, value, check = TRUE) {
15961596
skipExt <- skipDef@contains[[to]]
15971597
if (!is.null(skipExt)) {
15981598
body(f, envir = environment(f)) <-
1599-
call("as", body(skipExt@replace), byExt@subClass)
1599+
substitute(methods::as(BODY, TO),
1600+
list(BODY = body(skipExt@replace),
1601+
TO = byExt@subClass))
16001602
}
16011603
} else {
16021604
expr <- substitute({
1603-
.value <- as(from, BY, STRICT)
1604-
as(.value, TO) <- value
1605+
.value <- methods::as(from, BY, STRICT)
1606+
methods::as(.value, TO) <- value
16051607
value <- .value
16061608
BYEXPR
16071609
}, list(BY=by, TO = to, BYEXPR = byExpr, STRICT = strictBy))
@@ -1659,9 +1661,9 @@ setDataPart <- function(object, value, check = TRUE) {
16591661
}
16601662
}
16611663
else {
1662-
substitute({ value <- new(CLASS)
1664+
substitute({ value <- methods::new(CLASS)
16631665
for(what in TOSLOTS)
1664-
slot(value, what) <- slot(from, what)
1666+
methods::slot(value, what) <- methods::slot(from, what)
16651667
value },
16661668
list(CLASS = chClass, TOSLOTS = toSlots))
16671669
}
@@ -1671,7 +1673,7 @@ setDataPart <- function(object, value, check = TRUE) {
16711673
toSlots <- names(toDef@slots)
16721674
substitute({
16731675
for(what in TOSLOTS)
1674-
slot(from, what) <- slot(value, what)
1676+
methods::slot(from, what) <- methods::slot(value, what)
16751677
from
16761678
}, list(TOSLOTS = toSlots))
16771679
}

src/library/methods/R/as.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,11 @@ setAs <-
320320
}, list(AS = as.name(paste0("as.", what)))),
321321
##
322322
ts = body(method, envir = environment(method)) <- quote({
323-
value <- as.ts(from)
323+
value <- stats::as.ts(from)
324324
if(strict) {
325325
attributes(value) <- NULL
326326
class(value) <- class(new("ts"))
327-
tsp(value) <- tsp(from)
327+
stats::tsp(value) <- stats::tsp(from)
328328
}
329329
value
330330
}),

src/library/methods/R/zzz.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
function(libname, pkgname)
2323
{
2424
where <- environment(sys.function()) # the namespace
25+
coerceVars <- c("as", "as<-", "is", "new", "S3Part", "S3Part<-", "slot",
26+
"slot<-")
27+
namespaceExport(where, coerceVars)
2528
initMethodDispatch(where)
2629
## temporary empty reference to the package's own namespace
2730
assign(".methodsNamespace", new.env(), envir = where)
@@ -84,6 +87,7 @@
8487
.InitS3Classes, .InitSpecialTypesAndClasses, .InitTraceFunctions,
8588
.InitRefClasses, .initImplicitGenerics,
8689
envir = where)
90+
rm(list = coerceVars, envir = .getNamespaceInfo(where, "exports"))
8791
## unlock some bindings that must be modifiable
8892
unlockBinding(".BasicFunsList", where)
8993
assign(".saveImage", TRUE, envir = where)

src/library/tools/R/QC.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6221,7 +6221,8 @@ function(package, dir, lib.loc = NULL)
62216221
(imp3f %notin% c(".class1",
62226222
".missingMethod",
62236223
".selectDotsMethod",
6224-
".setDummyField"))]
6224+
".setDummyField",
6225+
".InhSlotNames"))]
62256226
imp3 <- names(imp3f)
62266227
}
62276228
imp3 <- unique(imp3)

tests/reg-S4.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ stopifnot(identical(alsofirstclass, class.list[[1]]))
869869
setClass("A", slots = c(foo = "numeric"))
870870
setClass("Ap", contains = "A", slots = c(p = "character"))
871871
cd <- getClassDef("Ap")
872-
body(cd@contains[["A"]]@coerce)[[2]] ## >> value <- new("A")
872+
body(cd@contains[["A"]]@coerce)[[2]] ## >> value <- methods::new("A")
873873
## was ... <- new(structure("A", package = ".GlobalEnv"))
874874
## for a few days in R-devel (Nov.2017)
875875

tests/reg-S4.Rout.save

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,8 +1165,8 @@ Also defined by 'package2'
11651165
> setClass("A", slots = c(foo = "numeric"))
11661166
> setClass("Ap", contains = "A", slots = c(p = "character"))
11671167
> cd <- getClassDef("Ap")
1168-
> body(cd@contains[["A"]]@coerce)[[2]] ## >> value <- new("A")
1169-
value <- new("A")
1168+
> body(cd@contains[["A"]]@coerce)[[2]] ## >> value <- methods::new("A")
1169+
value <- methods::new("A")
11701170
> ## was ... <- new(structure("A", package = ".GlobalEnv"))
11711171
> ## for a few days in R-devel (Nov.2017)
11721172
>

0 commit comments

Comments
 (0)