[Rd] bug in partial matching of attribute names

Tony Plate tplate at blackmesacapital.com
Mon Feb 12 22:56:51 CET 2007


There looks to be a bug in do_attr() (src/main/attrib.c): incorrect 
partial matches of attribute names can be returned when there are an odd 
number of partial matches.

E.g.:

 > x <- c(a=1,b=2)
 > attr(x, "abcdef") <- 99
 > attr(x, "ab")
[1] 99
 > attr(x, "abc") <- 100
 > attr(x, "ab") # correctly returns NULL because of ambig partial match
NULL
 > attr(x, "abcd") <- 101
 > attr(x, "ab") # incorrectly returns non-NULL for ambig partial match
[1] 101
 > names(attributes(x))
[1] "names"  "abcdef" "abc"    "abcd"
 >

The problem in do_attr() looks to be that after match is set to 
PARTIAL2, it can be set back to PARTIAL again.  I think a simple fix is 
to add a "break" in this block in do_attr():

     else if (match == PARTIAL) {
	/* this match is partial and we already have a partial match,
	   so the query is ambiguous and we return R_NilValue */
	match = PARTIAL2;
	break; /* <---- ADD BREAK HERE */
     } else {

However, if this is indeed a bug, would this be a good opportunity to 
get rid of partial matching on attribute names -- it was broken anyway 
-- so toss it out? :-)  Does anyone depend on partial matching for 
attribute names?  My view is that it's one of those things like partial 
matching of list and vector element names that seemed like a good idea 
at first, but turns out to be more trouble than it's worth.

On a related topic, partial matching does not seem to work for the 
"names" attribute (which I would regard as a good thing :-).  However, 
I'm puzzled why it doesn't work, because the code in do_attr() seems to 
try hard to make it work.  Can anybody explain why?

E.g.:
 > attr(x, "names")
[1] "a" "b"
 > attr(x, "nam")
NULL

 > sessionInfo()
R version 2.4.1 (2006-12-18)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United 
States.1252;LC_MONETARY=English_United 
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"
[7] "base"
 >

-- Tony Plate



More information about the R-devel mailing list