Skip to content

Commit

Permalink
Closes #832. setattr erros on setting DT/DF class to non-list obj.
Browse files Browse the repository at this point in the history
  • Loading branch information
arunsrinivasan committed Feb 9, 2015
1 parent 92ca87f commit 559f58c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@

38. `as.datat.able.factor` redirects to `as.data.table.matrix` when input is a `matrix`, but also of type `factor`. Closes [#868](https://github.com/Rdatatable/data.table/issues/868). Thanks to @mgahan for the example.

39. `setattr` now returns an error when trying to set `data.table` and/or `data.frame` as class to a *non-list* type object (ex: `matrix`). Closes [#832](https://github.com/Rdatatable/data.table/issues/832). Thanks to @Rick for the minimal example.

#### NOTES

1. Clearer explanation of what `duplicated()` does (borrowed from base). Thanks to @matthieugomez for pointing out. Closes [#872](https://github.com/Rdatatable/data.table/issues/872).
Expand Down
5 changes: 5 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -5979,6 +5979,11 @@ ans2.2 = DT[Time==.POSIXct(0, tz="UTC")]
test(1486.1, as.data.frame(ans1.1), as.data.frame(ans1.2))
test(1486.2, as.data.frame(ans2.1), as.data.frame(ans2.1))

# Fix for #832
x <- matrix(1:9, ncol=3)
setattr(x, "names", paste("V", seq_len(length(x)), sep = ""))
test(1486.3, setattr(x, "class", c("data.table", "data.frame")), error="Internal structure doesn't seem to be a list")

##########################


Expand Down
5 changes: 5 additions & 0 deletions src/wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
SEXP setattrib(SEXP x, SEXP name, SEXP value)
{
if (TYPEOF(name) != STRSXP) error("Attribute name must be of type character");
if ( !isNewList(x) &&
strcmp(CHAR(STRING_ELT(name, 0)), "class") == 0 &&
isString(value) && (strcmp(CHAR(STRING_ELT(value, 0)), "data.table") == 0 ||
strcmp(CHAR(STRING_ELT(value, 0)), "data.frame") == 0) )
error("Internal structure doesn't seem to be a list. Can't set class to be 'data.table' or 'data.frame'. Use 'as.data.table()' or 'as.data.frame()' methods instead.");
setAttrib(x, name,
NAMED(value) ? duplicate(value) : value);
// duplicate is temp fix to restore R behaviour prior to R-devel change on 10 Jan 2014 (r64724).
Expand Down

0 comments on commit 559f58c

Please sign in to comment.