Dear R-devel community,
There appears to be an inconsistency in R C API about the exceptions
that can be raised from C code.
Mapping of R C funs to corresponding R functions is as follows.
error -> stop
warning -> warning
REprintf -> message
Rprintf -> cat
Rprint/cat is of course not an exception, I listed it just for completeness.
The inconsistency I would like to report is about REprintf. It cannot
be caught by tryCatch(message). Warnings are errors are being caught
as expected.
Is there any chance to "fix"/"improve" REprintf so
tryCatch(message)
can catch it?
So in the example below catch(Cmessage()) would behave consistently to
R's catch(message("a"))?
Regards,
Jan Gorecki
catch = function(expr) {
tryCatch(expr,
message=function(m) cat("caught message\n"),
warning=function(w) cat("caught warning\n"),
error=function(e) cat("caught error\n")
)
}
library(inline)
Cstop = cfunction(c(), 'error("%s\\n","a"); return
R_NilValue;')
Cwarning = cfunction(c(), 'warning("%s\\n","a"); return
R_NilValue;')
Cmessage = cfunction(c(), 'REprintf("%s\\n","a"); return
R_NilValue;')
catch(stop("a"))
#caught error
catch(warning("a"))
#caught warning
catch(message("a"))
#caught message
catch(Cstop())
#caught error
catch(Cwarning())
#caught warning
catch(Cmessage())
#a
#NULL
Tierney, Luke
2019-Sep-15 15:25 UTC
[Rd] [External] REprintf could be caught by tryCatch(message)
On Sun, 15 Sep 2019, Jan Gorecki wrote:> Dear R-devel community, > > There appears to be an inconsistency in R C API about the exceptions > that can be raised from C code. > Mapping of R C funs to corresponding R functions is as follows. > > error -> stop > warning -> warning > REprintf -> messageThis is wrong: REpintf is like cat with file = stderr(). If this claim is made somewhere in R documentation please report it a a bug.> Rprintf -> cat > > Rprint/cat is of course not an exception, I listed it just for completeness. > The inconsistency I would like to report is about REprintf. It cannot > be caught by tryCatch(message). Warnings are errors are being caught > as expected. > > Is there any chance to "fix"/"improve" REprintf so tryCatch(message) > can catch it?No: this is behaving as intended. Best, luke> So in the example below catch(Cmessage()) would behave consistently to > R's catch(message("a"))? > > Regards, > Jan Gorecki > > catch = function(expr) { > tryCatch(expr, > message=function(m) cat("caught message\n"), > warning=function(w) cat("caught warning\n"), > error=function(e) cat("caught error\n") > ) > } > library(inline) > Cstop = cfunction(c(), 'error("%s\\n","a"); return R_NilValue;') > Cwarning = cfunction(c(), 'warning("%s\\n","a"); return R_NilValue;') > Cmessage = cfunction(c(), 'REprintf("%s\\n","a"); return R_NilValue;') > > catch(stop("a")) > #caught error > catch(warning("a")) > #caught warning > catch(message("a")) > #caught message > > catch(Cstop()) > #caught error > catch(Cwarning()) > #caught warning > catch(Cmessage()) > #a > #NULL > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Luke Tierney Ralph E. Wareham Professor of Mathematical Sciences University of Iowa Phone: 319-335-3386 Department of Statistics and Fax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke-tierney at uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
Jan Gorecki
2019-Sep-15 17:09 UTC
[Rd] [External] REprintf could be caught by tryCatch(message)
Thank you Luke for prompt reply. Is it possible then to request a new function to R C API "message" that would equivalent to R "message" function? Similarly as we now have C "warning" and C "error" functions. Best, Jan On Sun, Sep 15, 2019 at 5:25 PM Tierney, Luke <luke-tierney at uiowa.edu> wrote:> > On Sun, 15 Sep 2019, Jan Gorecki wrote: > > > Dear R-devel community, > > > > There appears to be an inconsistency in R C API about the exceptions > > that can be raised from C code. > > Mapping of R C funs to corresponding R functions is as follows. > > > > error -> stop > > warning -> warning > > REprintf -> message > > This is wrong: REpintf is like cat with file = stderr(). If this claim > is made somewhere in R documentation please report it a a bug. > > > Rprintf -> cat > > > > Rprint/cat is of course not an exception, I listed it just for completeness. > > The inconsistency I would like to report is about REprintf. It cannot > > be caught by tryCatch(message). Warnings are errors are being caught > > as expected. > > > > Is there any chance to "fix"/"improve" REprintf so tryCatch(message) > > can catch it? > > No: this is behaving as intended. > > Best, > > luke > > > So in the example below catch(Cmessage()) would behave consistently to > > R's catch(message("a"))? > > > > Regards, > > Jan Gorecki > > > > catch = function(expr) { > > tryCatch(expr, > > message=function(m) cat("caught message\n"), > > warning=function(w) cat("caught warning\n"), > > error=function(e) cat("caught error\n") > > ) > > } > > library(inline) > > Cstop = cfunction(c(), 'error("%s\\n","a"); return R_NilValue;') > > Cwarning = cfunction(c(), 'warning("%s\\n","a"); return R_NilValue;') > > Cmessage = cfunction(c(), 'REprintf("%s\\n","a"); return R_NilValue;') > > > > catch(stop("a")) > > #caught error > > catch(warning("a")) > > #caught warning > > catch(message("a")) > > #caught message > > > > catch(Cstop()) > > #caught error > > catch(Cwarning()) > > #caught warning > > catch(Cmessage()) > > #a > > #NULL > > > > ______________________________________________ > > R-devel at r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel > > > > -- > Luke Tierney > Ralph E. Wareham Professor of Mathematical Sciences > University of Iowa Phone: 319-335-3386 > Department of Statistics and Fax: 319-335-3017 > Actuarial Science > 241 Schaeffer Hall email: luke-tierney at uiowa.edu > Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu