[Rcpp-devel] RCPP_USE_UNWIND_PROTECT by default

Dirk Eddelbuettel edd at debian.org
Sun Feb 6 18:31:10 CET 2022


On 6 February 2022 at 18:18, Jeroen Ooms wrote:
| Well not really, this kind of misses my point that it the
| unwind-protect makes it impossible to meaningfully catch the R error
| in C++, handle it, and continue running the C++ code, without aborting
| the entire mission and throwing the user back in the console.

I see. Well if we wanted to catch an R error on the R side, adding tryCatch
still works. Here are your initial R function, renamed, and a new one:

  callbackWithoutTry <- function() {
    stop("Ouch from R")
  }
  
  callback <- function() {
      tryCatch(stop("Ouch from R"),
               error = function(e) { cat("Well that went south\n") },
               finally = function(e) { cat("Normal end\n") } )
  }

This (under UNWIND_PROTECT) behaves:

  edd at rob:/tmp/uptest(master)$ install.r .
  * installing *source* package ‘uptest’ ...
  ** using staged installation
  ** libs
  rm -f uptest.so RcppExports.o callback.o
  ccache g++-11 -I"/usr/share/R/include" -DNDEBUG -DRCPP_USE_UNWIND_PROTECT -I'/usr/local/lib/R/site-library/Rcpp/include'    -fpic  -g -O3 -Wall -pipe -pedantic  -c RcppExports.cpp -o RcppExports.o
  ccache g++-11 -I"/usr/share/R/include" -DNDEBUG -DRCPP_USE_UNWIND_PROTECT -I'/usr/local/lib/R/site-library/Rcpp/include'    -fpic  -g -O3 -Wall -pipe -pedantic  -c callback.cpp -o callback.o
  ccache g++-11 -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -flto=auto -Wl,-z,relro -o uptest.so RcppExports.o callback.o -L/usr/lib/R/lib -lR
  installing to /usr/local/lib/R/site-library/00LOCK-uptest/00new/uptest/libs
  ** R
  ** byte-compile and prepare package for lazy loading
  ** help
  *** installing help indices
  ** building package indices
  ** testing if installed package can be loaded from temporary location
  ** checking absolute paths in shared objects and dynamic libraries
  ** testing if installed package can be loaded from final location
  ** testing if installed package keeps a record of temporary installation path
  * DONE (uptest)
  edd at rob:/tmp/uptest(master)$ R -q
  > uptest::uptest()
  Well that went south
  [1] 42
  > 

So this catches the stop(), enters a designates block and resumes in the C++
function that called it all still returning the 42 we desired as The Answer.

Dirk

-- 
https://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list