Re: [Rd] validObject throws non-caught error when slot doesn't exist

From: John Chambers <jmc_at_r-project.org>
Date: Fri, 19 Jun 2009 12:31:48 -0400

A fix for this and related problems is now committed (r48803).

Some of the interesting ways that validObject() could fail previously are in the attached regression-test file.

John

John Chambers wrote:
> Patrick,
>
> Thanks for the bug report and the proposed patch.
>
> There are some related ways that validObject can fail (e.g., if the
> revised class definition extends one of the object types or if the
> object is an S3 object), so the patch needs to be a little more
> general, but I will commit something to the 2.10 development version
> and send mail).
>
> John
>
> Patrick Aboyoun wrote:
>> I have been retooling an S4 class definition to include another slot
>> and have found that the methods::validObject function (defined in
>> methods/R/SClasses.R) in R-devel throws an error that isn't caught
>> internally (and thus not controllable by 'test' argument) when
>> retrieving a non-existent slot. The offending line of code is shown
>> below:
>>
>> > validObject
>> function (object, test = FALSE, complete = FALSE)
>> {
>> ...
>> for (i in seq_along(slotTypes)) {
>> classi <- slotTypes[[i]]
>> sloti <- slot(object, slotNames[[i]]) # offending line of code
>>
>> One potential patch is to substitute the offending line with
>>
>> sloti <- try(slot(object, slotNames[[i]]), silent = TRUE)
>> if (class(sloti) == "try-error") {
>> errors <- c(errors, paste("missing slot \"", slotNames[[i]],
>> "\"", sep = ""))
>> next
>> }
>>
>> Here is a reproduce and an example using vaildObject2 that
>> substitutes the offending line with the code given above:
>>
>> > setClass("Foo", representation(bar = "character"))
>> [1] "Foo"
>> > a <- new("Foo", bar = letters)
>> > setClass("Foo", representation(bar = "character", star = "numeric"))
>> [1] "Foo"
>> > validObject(a, test = TRUE)
>> Error in slot(object, slotNames[[i]]) :
>> no slot of name "star" for this object of class "Foo"
>> > validObject2(a, test = TRUE)
>> [1] "missing slot \"star\""
>> > sessionInfo()
>> R version 2.10.0 Under development (unstable) (2009-06-12 r48755)
>> i386-apple-darwin9.6.0
>>
>> locale:
>> [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
>>
>> attached base packages:
>> [1] stats graphics grDevices utils datasets methods base
>>
>> Patrick
>>
>> ______________________________________________
>> R-devel_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>

setClass("Foo", representation(bar = "character"))
a <- new("Foo", bar = letters)
setClass("Foo", representation(bar = "character", star = "numeric", flag = "logical"))
stopifnot(is.character(validObject(a, test = TRUE))) setClass("Foo", representation(bar = "POSIXt")) stopifnot(is.character(validObject(a, test = TRUE))) setClass("Foo", contains = "character", representation(bar = "character")) stopifnot(is.character(validObject(a, test = TRUE))) setClass("Foo", contains = "lm", representation(bar = "character")) stopifnot(is.character(validObject(a, test = TRUE)))

 setClass("myEnv", contains = "environment", representation(bar = "character")) mvv = new("myEnv", new.env(), bar = "test")

setClass("c1", representation(x="numeric")) setClass("c2", representation(y = "numeric", xx = "c1")) xx <- new("c2", y = 1, xx = new("c1", x = 2)) removeClass("c1")
stopifnot(is.character(validObject(xx, test = TRUE)))



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri 19 Jun 2009 - 16:43:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 20 Jun 2009 - 01:30:59 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive