Skip to content

Commit 5bd6e3c

Browse files
author
maechler
committed
on.exit() now matches named arguments as intended
git-svn-id: https://svn.r-project.org/R/trunk@78634 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 1d208b1 commit 5bd6e3c

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

doc/NEWS.Rd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@
109109

110110
\item \code{boxplot()} now also accepts \code{call}s for labels such
111111
as \code{ylab}, the same as \code{plot()}. Reported by Marius Hofert.
112+
113+
\item \code{on.exit()} now correctly matches named arguments, thanks
114+
to \PR{17815} including patch by Brodie Gaslam.
112115
}
113116
}
114117
}

src/library/base/man/on.exit.Rd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% File src/library/base/man/on.exit.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2014 R Core Team
3+
% Copyright 1995-2020 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{on.exit}
@@ -42,7 +42,7 @@ on.exit(expr = NULL, add = FALSE, after = TRUE)
4242
then all expressions will be run even if one signals an error.
4343

4444
This is a \sQuote{special} \link{primitive} function: it only
45-
evaluates the argument \code{add}.
45+
evaluates the arguments \code{add} and \code{after}.
4646
}
4747
\value{
4848
Invisible \code{NULL}.

src/main/builtin.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ SEXP attribute_hidden do_onexit(SEXP call, SEXP op, SEXP args, SEXP rho)
148148
else code = CAR(argList);
149149

150150
if (CADR(argList) != R_MissingArg) {
151-
addit = asLogical(PROTECT(eval(CADR(args), rho)));
151+
addit = asLogical(PROTECT(eval(CADR(argList), rho)));
152152
UNPROTECT(1);
153153
if (addit == NA_INTEGER)
154154
errorcall(call, _("invalid '%s' argument"), "add");
155155
}
156156
if (CADDR(argList) != R_MissingArg) {
157-
after = asLogical(PROTECT(eval(CADDR(args), rho)));
157+
after = asLogical(PROTECT(eval(CADDR(argList), rho)));
158158
UNPROTECT(1);
159159
if (after == NA_INTEGER)
160160
errorcall(call, _("invalid '%s' argument"), "lifo");

tests/reg-tests-1d.R

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pdf("reg-tests-1d.pdf", encoding = "ISOLatin1.enc")
44
.pt <- proc.time()
55
tryCid <- function(expr) tryCatch(expr, error = identity)
66
identCO <- function(x,y, ...) identical(capture.output(x), capture.output(y), ...)
7+
assertErrV <- function(...) tools::assertError(..., verbose=TRUE)
78
onWindows <- .Platform$OS.type == "windows"
89

910
## body() / formals() notably the replacement versions
@@ -611,7 +612,7 @@ stopifnot(exprs = {
611612
identical(c(1L, 3L), seq.int(1L, 3L, length.out=2))
612613
})
613614
## the first was missing(.), the others "double" in R < 3.4.0
614-
tools::assertError(seq(1,7, by = 1:2))# gave warnings in R < 3.4.0
615+
assertErrV(seq(1,7, by = 1:2))# gave warnings in R < 3.4.0
615616
## seq() for <complex> / <integer>
616617
stopifnot(exprs = {
617618
all.equal(seq(1+1i, 9+2i, length.out = 9) -> sCplx,
@@ -837,8 +838,8 @@ if(englishMsgs)
837838
stopifnot(grepl("indexing '...' with .* index 0", mt0),
838839
identical("the ... list contains fewer than 2 elements", mt2.0),
839840
identical(mt2.0, mt2.1))
840-
tools::assertError(t0(1))
841-
tools::assertError(t0(1, 2))
841+
assertErrV(t0(1))
842+
assertErrV(t0(1, 2))
842843
## the first gave a different error msg, the next gave no error in R < 3.5.0
843844

844845

@@ -1078,9 +1079,9 @@ stopifnot(exprs = {
10781079
## invalid user device function options(device = *) -- PR#15883
10791080
graphics.off() # just in case
10801081
op <- options(device=function(...){}) # non-sense device
1081-
tools::assertError(plot.new(), verbose = TRUE)
1082+
assertErrV(plot.new())
10821083
if(no.grid <- !("grid" %in% loadedNamespaces())) requireNamespace("grid")
1083-
tools::assertError(grid::grid.newpage(), verbose = TRUE)
1084+
assertErrV(grid::grid.newpage())
10841085
if(no.grid) unloadNamespace("grid") ; options(op)
10851086
## both errors gave segfaults in R <= 3.4.1
10861087

@@ -1280,7 +1281,7 @@ stopifnot(exprs = {
12801281
if(no.splines <- !("splines" %in% loadedNamespaces())) requireNamespace("splines")
12811282
x <- (0:8)/8
12821283
aKnots <- c(rep(0, 4), c(0.3, 0.5, 0.6), rep(1, 4))
1283-
tools::assertError(splines::splineDesign(aKnots, x, derivs = 4), verbose = TRUE)
1284+
assertErrV(splines::splineDesign(aKnots, x, derivs = 4))
12841285
## gave seg.fault in R <= 3.4.1
12851286

12861287

@@ -2661,7 +2662,7 @@ stopifnot(exprs = {
26612662

26622663

26632664
## Failed to work after r76382--8:
2664-
tools::assertError(formula("3"), verbose=TRUE)
2665+
assertErrV(formula("3"))
26652666
stopifnot(exprs = {
26662667
## New formula(<character>) specs:
26672668
## These give deprecation warnings:
@@ -3368,7 +3369,7 @@ stopifnot(exprs = {
33683369
NextMethod("[")
33693370
}
33703371
noC <- structure(datasets::trees, class = c("noCol", "data.frame"))
3371-
tools::assertError( noC[1,2], verbose=TRUE) # fails indeed
3372+
assertErrV( noC[1,2]) # fails indeed
33723373
stopifnot(exprs = {
33733374
identical(head(noC), noC[1:6,])
33743375
identical(head(noC, 1), noC[1, ])
@@ -3380,8 +3381,8 @@ stopifnot(exprs = {
33803381
str(Alis <- lapply(1:4, function(n) {d <- 1+(1:n); array(seq_len(prod(d)), d) }))
33813382
h2 <- lapply(Alis, head, 2)
33823383
t2 <- lapply(Alis, head, 2)
3383-
tools::assertError( head(Alis[[1]], c(1, NA)), verbose=TRUE)
3384-
tools::assertError( tail(1:5, c(1, NA)), verbose=TRUE)
3384+
assertErrV( head(Alis[[1]], c(1, NA)))
3385+
assertErrV( tail(1:5, c(1, NA)))
33853386
h1 <- lapply(Alis, head, 1)
33863387
t1 <- lapply(Alis, tail, 1)
33873388
dh1 <- lapply(h1, dim)
@@ -3390,7 +3391,7 @@ Alis2p <- Alis[-1]
33903391
h1N <- lapply(Alis2p, head, c(1, NA))
33913392
t1N <- lapply(Alis2p, tail, c(1, NA))
33923393
Foolis <- lapply(Alis, `class<-`, "foo")
3393-
tools::assertError( head(Foolis[[1]], c(1, NA)), verbose=TRUE)
3394+
assertErrV( head(Foolis[[1]], c(1, NA)))
33943395
h1F <- lapply(Foolis, head, 1)
33953396
h2F <- lapply(Foolis, head, 2)
33963397
t1F <- lapply(Foolis, tail, 1)
@@ -3531,8 +3532,8 @@ stopifnot(exprs = {
35313532
all.equal(attributes(x), list(tsp = c(2.5, 107.5, 0.2), class = "ts"))
35323533
all.equal(wx, structure(c(0.5, 0.6), .Tsp = c(22.5, 27.5, 0.2), class = "ts"))
35333534
})
3534-
tools::assertError(cbind(ts(1:2, start = 0.5, end = 1.5),
3535-
ts(1:2, start = 0 , end = 1)), verbose=TRUE)
3535+
assertErrV(cbind(ts(1:2, start = 0.5, end = 1.5),
3536+
ts(1:2, start = 0 , end = 1)))
35363537
## Wrong results in R < 4.0.0
35373538
## New checks needed tweaks :
35383539
## -- 1 --
@@ -3809,10 +3810,9 @@ stopifnot(is.integer(y1), is.integer(y2), y1[-3] == y2[-3],
38093810

38103811

38113812
## stopifnot() custom message now via <named> args:
3812-
e <- tools::assertError(stopifnot("ehmm, you must be kidding!" = 1 == 0), verbose=TRUE)
3813+
e <- assertErrV(stopifnot("ehmm, you must be kidding!" = 1 == 0))
38133814
stopifnot(grepl("must be kidding!", e[[1]]$message))
3814-
e2 <- tools::assertError(
3815-
stopifnot("2 is not approximately 2.1" = all.equal(2, 2.1)), verbose=TRUE)
3815+
e2 <- assertErrV(stopifnot("2 is not approximately 2.1" = all.equal(2, 2.1)))
38163816
stopifnot(grepl("not approximately", e2[[1]]$message))
38173817
## did not work in original stopifnot(<named>) patch
38183818
CHK <- function(...) stopifnot(...)
@@ -4030,7 +4030,6 @@ cat("Case 2 : round(x=1.12345,2): ", round(x=1.12345, 2),"\n")
40304030
cat("Case 3 : round(x=1.12345,digits=2): ", round(x=1.12345, digits=2),"\n")
40314031
cat("Case 4 : round(digits=2,x=1.12345): ", round(digits=2, x=1.12345),"\n")
40324032
cat("Case 4b: round(digits=2,1.12345): ", round(digits=2,1.12345),"\n")
4033-
assertErrV <- function(...) tools::assertError(..., verbose=TRUE)
40344033
## R <= 4.0.0 does not produce error in cases 5,6 but should :
40354034
cat("Case 5: round(digits=x): \n")
40364035
assertErrV(cat("round(digits=99.23456): ", round(digits=99.23456)))
@@ -4058,6 +4057,14 @@ boxplot(cbind(x = 1:10, y = c(16,9:1)), xlab = quote(x^{y[2]}), ylab = quote(X[t
40584057
## failed in R <= 4.0.1
40594058

40604059

4060+
## on.exit() argument matching -- PR#17815
4061+
f <- function() { on.exit(add=FALSE, expr=cat('bar\n')) ; 'foo' }
4062+
stopifnot(identical(f(), 'foo')) # and write 'bar' line
4063+
g <- function() { on.exit(add=stop('boom'), expr={cat('bar\n'); FALSE}) ; "foo" }
4064+
assertErrV(g())
4065+
## f() :> "Error in on.exit(....): invalid 'add' argument" and no error for g() in R <= 4.0.1
4066+
4067+
40614068

40624069
## keep at end
40634070
rbind(last = proc.time() - .pt,

0 commit comments

Comments
 (0)