[Rd] trace creates object in base namespace if called on function argument

Antoine Fabri @nto|ne@|@br| @end|ng |rom gm@||@com
Wed Aug 26 16:39:44 CEST 2020


Hi Tomas,

The doc indeed describes `what` as "the name, possibly quote()d, of a
function to be traced or untraced".
This is a good argument not to change the function and make it behave more
like debug.

However the doc also tells us "A call to trace allows you to insert
debugging code (e.g., a call to browser or recover) at chosen places in any
function" and "The trace function operates by constructing a revised
version of the function (or of the method, if signature is supplied), and
assigning the new object back where the original was found". In the issue I
report a function was created in the base environment, which is not "where
the function was found". It's not clear to me what "where it was found"
would mean in that case, but I would assume the execution environment of
trace2, or maybe the calling environment (global in that case), or parent
of foo (global here again), but not the base namespace.

It would make sense to me to either have a traced fun in the execution
environment, or to have an error.

Best,

Antoine

Le mer. 26 août 2020 à 14:17, Tomas Kalibera <tomas.kalibera using gmail.com> a
écrit :

> Please note that this is documented in ?trace. "fun" is matched to what,
> it is a _name_ of the function to be traced, which is traced in the
> top-level environment. I don't know why it was designed this way, but it
> is documented in detail, and hence the expected behavior.
>
> Debugging is often, and also in R, implemented in the core. Tracing is
> implemented on top without specific support, it thus cannot do some
> things debugging can do.
>
> Tomas
>
>
> On 8/26/20 3:31 AM, Antoine Fabri wrote:
> > Apologies there is one line missing in my last email, the code should be
> :
> >
> > foo <- function() "hello"
> > trace2 <- function(fun) trace(fun, quote(print("!!!")))
> > trace2(foo) # <- THIS LINE WAS MISSING
> > base::fun
> >
> > Best,
> >
> > Antoine
> >
> > Le mar. 25 août 2020 à 22:02, Antoine Fabri <antoine.fabri using gmail.com> a
> > écrit :
> >
> >> Dear R-devel,
> >>
> >> I don't think this is expected :
> >>
> >> foo <- function() "hello"
> >> trace2 <- function(fun) trace(fun, quote(print("!!!")))
> >> base::fun
> >> # Object with tracing code, class "functionWithTrace"
> >> # Original definition:
> >> # function() "hello"
> >> #
> >> # ## (to see the tracing code, look at body(object))
> >>
> >> `untrace()` has the same behavior.
> >>
> >> This is inconsistent with how debug works :
> >>
> >> foo <- function() "hello"
> >> debug2 <- function(fun) debug(fun)
> >> debug2(foo)
> >> isdebugged(foo)
> >> # [1] TRUE
> >>
> >> This can be worked around by defining :
> >>
> >> trace2 <- function(fun) eval.parent(substitute(trace(fun,
> >> quote(print("!!!")))
> >>
> >> but I believe the current behavior is undesired and it'd be better to
> make
> >> it behave as `debug()`, or to throw an error.
> >>
> >> Best,
> >>
> >> Antoine
> >>
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-devel using r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
>
>

	[[alternative HTML version deleted]]



More information about the R-devel mailing list