nealrichardson / httptest

A Test Environment for HTTP Requests in R
https://enpiar.com/r/httptest/
Other
79 stars 10 forks source link

safe_untrace() can NOT work with a function object as argument, nor with a scoped function name #78

Closed kforner closed 1 year ago

kforner commented 1 year ago

safe_untrace() silently (because of the try()) fails, if given a function as argument:

# as in stop_mocking()
safe_untrace(curl::form_file)

This can not work since:

Also, it could not work with safe_untrace("curl::form_file") since get() does not understand scoped function names. Instead, it should be called like:

>safe_untrace('form_file', where = getNamespace('curl'))
Untracing function "form_file" in package "namespace:curl"
[1] "form_file"

It is partially responsible for #66 .

For reference:

safe_untrace <- function(what, where = sys.frame()) {
  # If you attempt to untrace a function (1) that isn't exported from
  # whatever namespace it lives in and (2) that isn't currently traced,
  # it errors. This prevents that so that it's always safe to call `untrace`

  # untrace() and get() handle enviroments differently
  if (is.environment(where)) {
    env <- where
  } else {
    env <- environment(where)
  }
  if (inherits(try(get(what, env), silent = TRUE), "functionWithTrace")) {
    quietly(untrace(what, where = where))
  }
}
mlamarin commented 1 year ago

:1st_place_medal: