[Rd] trace() an S3-classed function in .GlobalEnv
Michael Chirico
m|ch@e|ch|r|co4 @end|ng |rom gm@||@com
Wed Jun 8 19:12:25 CEST 2022
Thanks, that seems like a reasonable assessment.
Also worth noting that tracing wipes out a function's attributes,
which is also not documented:
foo = function(x) {
invisible(1 + 1)
}
attr(foo, "bar") <- 2
trace(foo, quote(message('traced')))
names(attributes(foo))
# [1] "original" "source" "class"
PS one day I hope to master the dark art of choosing r-devel or
bugzilla for issues :)
On Wed, Jun 8, 2022 at 3:44 AM Martin Maechler
<maechler using stat.math.ethz.ch> wrote:
>
> >>>>> Michael Chirico
> >>>>> on Mon, 6 Jun 2022 23:09:29 -0700 writes:
>
> > Consider:
>
> > foo <- function() {
> > invisible(1 + 1)
> > }
> > trace(foo, quote(message("traced")), print=FALSE)
> > foo()
> > # traced
>
> > But with a minimal change:
>
> > class(foo) <- "bar"
> > trace(foo, quote(message("traced")), print=FALSE)
> > # Constructing traceable class “barWithTrace”
> > # Error in .classEnv(className) :
> > # unable to find an environment containing class “bar”
>
> > I don't see anything like this mentioned in ?trace (nor does a Google
> > search turn up more than a handful of references to this error),
> > and from trying to debug what trace() is doing, we arrive to the error line[1]:
>
> > .makeTraceClass(traceClass, class(original)) #
> > methods:::.makeTraceClass("barWithTrace", "bar")
>
> > I don't quite follow what's going on here, but it looks like trace()
> > is trying to determine an S4 class definition for "bar", but isS4(bar)
> > is FALSE.
>
> > I can (apparently -- not sure if there are as yet-unseen downstream
> > consequences) work around the issue by unsetting the class, tracing,
> > then re-setting:
>
> > class(foo) <- NULL
> > trace(foo, quote(message("traced")), print=FALSE)
> > class(foo) <- "bar"
>
> > But obviously this is a bit clunky. Is this a bug, or am I missing something?
>
> Just a short note of semi-confirmation:
>
> At the time S4 generics and methods were introduced into R,
> trace() was made much more flexible, notably to be able to trace
> S4 methods.
>
> It can well be that it originally also worked for functions with
> an explicit S3 class, but as such functions are very rare, it
> could well be you've found a bug, namely that trace() assumes
> that if a function has a non-trivial class, it must be an S4
> one.
>
> ... and I know you know how to report bugs ;-)
>
> Thank you in advance!
> Martin
>
> > Mike C
>
> > [1] https://github.com/r-devel/r-svn/blob/e2a64a4e14adbc4e9e8635eaa8cbd2835ce1d764/src/library/methods/R/trace.R#L240
>
> > ______________________________________________
> > R-devel using r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list