C stack error are not caught
I don't know if this is possible or not but currently we have this
> evaluate::evaluate("stop('a')")
[[1]]
$src
[1] "stop('a')"
attr(,"class")
[1] "source"
[[2]]
<simpleError in eval(expr, envir, enclos): a>
> evaluate::evaluate('e <- new.env()
+ attr(e, "e") <- e
+ object.size(e)')
Error: C stack usage 15922884 is too close to the limit
This was reported in https://github.com/tidyverse/reprex/issues/388
try() works in these two cases:
try(object.size(e), silent = T)
try(withCallingHandlers(object.size(e)), silent = T)
but not in this one when an error handler is present:
try(withCallingHandlers(object.size(e), error = function(e) NULL), silent = T)
I don't know why yet.
Saw this today: https://twitter.com/R_dev_news/status/1457626456301846533
Stack overflow errors are now signaled as errors inheriting from class ‘stackOverflowError’. See ‘?stackOverflowError’ for more details.
Great! It seems we can just wait for the next release of R. Thanks for the info!
While stackoverflow errors are now caught:
e <- new.env()
attr(e, "e") <- e
tryCatch(object.size(e), error = function(err) err)
#> <CStackOverflowError: C stack usage 7953636 is too close to the limit>
Created on 2024-01-11 with reprex v2.0.2.9000
And evaluate now "works", it still doesn't actually capture the error:
evaluate::evaluate('
e <- new.env()
attr(e, "e") <- e
object.size(e)'
)
#> [[1]]
#> $src
#> [1] "\n"
#>
#> attr(,"class")
#> [1] "source"
#>
#> [[2]]
#> $src
#> [1] " e <- new.env()\n"
#>
#> attr(,"class")
#> [1] "source"
#>
#> [[3]]
#> $src
#> [1] " attr(e, \"e\") <- e\n"
#>
#> attr(,"class")
#> [1] "source"
#>
#> [[4]]
#> $src
#> [1] " object.size(e)"
#>
#> attr(,"class")
#> [1] "source"
Created on 2024-01-11 with reprex v2.0.2.9000
This is likely due to the special nature of stackoverflow errors (because the stack is "full", it's hard to run any additional code wrapped around the error, so will likely require some careful extra handling).
I'm pretty sure it's not possible to capture this error in evaluate itself