From maechler at stat.math.ethz.ch  Mon May 15 17:44:06 2017
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Mon, 15 May 2017 17:44:06 +0200
Subject: [Rd] stopifnot() does not stop at first non-TRUE argument
In-Reply-To: <477f9e1a-4f7c-a902-e744-da4a36eb5a4f@insa-toulouse.fr>
References: <64acdaeb-2857-9017-68c9-9da08e29a617@fredhutch.org>
 <E5DADF15-58C6-4F73-BC56-C6404896982F@gmail.com>
 <c2b3cb22-90bd-5372-e8b9-d1ea8b2ab828@fredhutch.org>
 <8945f7e4-99ce-53fa-46ee-cce64078a6a8@fredhutch.org>
 <22809.26921.473223.395550@stat.math.ethz.ch>
 <8fdb66b1-f785-ed55-6e40-a4ef8138e637@insa-toulouse.fr>
 <26bd9210-e40c-0967-7abb-5f724b09c034@insa-toulouse.fr>
 <22809.44830.153073.675527@stat.math.ethz.ch>
 <477f9e1a-4f7c-a902-e744-da4a36eb5a4f@insa-toulouse.fr>
Message-ID: <22809.52422.219712.66882@stat.math.ethz.ch>

>>>>> Serguei Sokol <sokol at insa-toulouse.fr>
>>>>>     on Mon, 15 May 2017 16:32:20 +0200 writes:

    > Le 15/05/2017 ? 15:37, Martin Maechler a ?crit :
    >>>>>>> Serguei Sokol <sokol at insa-toulouse.fr>
    >>>>>>> on Mon, 15 May 2017 13:14:34 +0200 writes:
    >> > I see in the archives that the attachment cannot pass.
    >> > So, here is the code:
    >> 
    >> [....... MM: I needed to reformat etc to match closely to
    >> the current source code which is in
    >> https://svn.r-project.org/R/trunk/src/library/base/R/stop.R
    >> or its corresponding github mirror
    >> https://github.com/wch/r-source/blob/trunk/src/library/base/R/stop.R
    >> ]
    >> 
    >> > Best,
    >> > Serguei.
    >> 
    >> Yes, something like that seems even simpler than Peter's
    >> suggestion...
    >> 
    >> It currently breaks 'make check' in the R sources,
    >> specifically in tests/reg-tests-2.R (lines 6574 ff),
    >> the new code now gives
    >> 
    >> > ## error messages from (C-level) evalList
    >> > tst <- function(y) { stopifnot(is.numeric(y)); y+ 1 }
    >> > try(tst())
    >> Error in eval(cl.i, pfr) : argument "y" is missing, with no default
    >> 
    >> whereas previously it gave
    >> 
    >> Error in stopifnot(is.numeric(y)) :
    >> argument "y" is missing, with no default
    >> 
    >> 
    >> But I think that change (of call stack in such an error case) is
    >> unavoidable and not a big problem.

    > It can be avoided but at price of customizing error() and warning() calls with something like:
    > wrn <- function(w) {w$call <- cl.i; warning(w)}
    > err <- function(e) {e$call <- cl.i; stop(e)}
    > ...
    > tryCatch(r <- eval(cl.i, pfr), warning=wrn, error=err)

    > Serguei.

Well, a good idea, but the 'warning' case is more complicated
(and the above incorrect): I do want the warning there, but
_not_ return the warning, but rather, the result of eval() :
So this needs even more sophistication, using  withCallingHandlers(.)
and maybe that really get's too sophisticated and no
more "readable" to 99.9% of the R users ... ?

I now do append my current version -- in case some may want to
comment or improve further.

Martin

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: stopifnot_new.R.~3~
URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20170515/4d595547/attachment.ksh>


[Rd] stopifnot() does not stop at first non-TRUE argument
Martin Maechler