Skip to content

Commit d578dd1

Browse files
author
smeyer
committed
fixInNamespace("<S3method>", ns) gets generic from 'ns' (or parents)
git-svn-id: https://svn.r-project.org/R/trunk@88423 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 07f296d commit d578dd1

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

doc/NEWS.Rd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@
223223
\file{doc/index.html} page now also work for packages not installed
224224
in the default \code{.Library},
225225
thanks to a report by \I{Patrice Kiener}.
226+
227+
\item %\code{assignInNamespace("\var{S3method}")} and thus
228+
\code{fixInNamespace("\var{S3method}")} failed to update the S3
229+
methods table when the generic was not on the search path.
226230
}
227231
}
228232
}

src/library/utils/R/objects.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# File src/library/utils/R/objects.R
22
# Part of the R package, https://www.R-project.org
33
#
4-
# Copyright (C) 1995-2024 The R Core Team
4+
# Copyright (C) 1995-2025 The R Core Team
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -419,7 +419,7 @@ function(x, value)
419419
S3names <- S3[, 3L]
420420
if(x %in% S3names) {
421421
i <- match(x, S3names)
422-
genfun <- get(S3[i, 1L], mode = "function", envir = parent.frame())
422+
genfun <- get(S3[i, 1L], mode = "function", envir = ns)
423423
if(.isMethodsDispatchOn() && methods::is(genfun, "genericFunction"))
424424
genfun <- methods::slot(genfun, "default")@methods$ANY
425425
defenv <- .defenv_for_S3_registry(genfun)
@@ -479,7 +479,7 @@ function(x, value, ns, pos = -1, envir = as.environment(pos))
479479
S3names <- S3[, 3L]
480480
if(x %in% S3names) {
481481
i <- match(x, S3names)
482-
genfun <- get(S3[i, 1L], mode = "function", envir = parent.frame())
482+
genfun <- get(S3[i, 1L], mode = "function", envir = ns)
483483
if(.isMethodsDispatchOn() && methods::is(genfun, "genericFunction"))
484484
genfun <- methods::slot(genfun, "default")@methods$ANY
485485
defenv <- .defenv_for_S3_registry(genfun)

tests/reg-tests-1e.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,23 @@ stopifnot(identical(x, expression()),
20282028
identical(`length<-`(x, 2L), expression(NULL, NULL)))
20292029

20302030

2031+
## (fix|assign)InNamespace(<S3method>) when the generic is not in search()
2032+
try(detach("package:tools"), silent = TRUE) # just in case
2033+
assertValueIs <- function (value) stopifnot(exprs = {
2034+
identical(tools:::toRd.default(1), value)
2035+
identical(tools::toRd(1), value)
2036+
})
2037+
assertValueIs("1")
2038+
## modify the default method to return an empty string:
2039+
omethod <- tools:::toRd.default
2040+
assignInNamespace("toRd.default", `body<-`(omethod, value = ""), "tools")
2041+
## R <= 4.5.1 gave Error: object 'toRd' of mode 'function' was not found
2042+
assertValueIs("") # failed for tools::toRd(1): S3 table was not updated
2043+
## now restore the original definition:
2044+
assignInNamespace("toRd.default", omethod, "tools")
2045+
assertValueIs("1")
2046+
2047+
20312048

20322049
## keep at end
20332050
rbind(last = proc.time() - .pt,

0 commit comments

Comments
 (0)