Skip to content

Commit ccd9ee6

Browse files
committed
drop attributes except names, class and levels
1 parent 7d6aea9 commit ccd9ee6

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

inst/tests/tests.Rraw

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18430,8 +18430,12 @@ test(2251.41, frev(x), rev(x))
1843018430
test(2251.42, setrev(x), x)
1843118431
# attributes
1843218432
x = structure(1:10, class = c("IDate", "Date"), att = 1L)
18433-
test(2251.51, attr(frev(x), "att"), 1L)
18434-
test(2251.52, attr(setrev(x), "att"), 1L)
18433+
test(2251.51, attr(frev(x), "att"), attr(rev(x), "att"))
18434+
test(2251.52, class(frev(x)), class(rev(x)))
18435+
test(2251.53, attr(setrev(x), "att"), 1L)
18436+
test(2251.54, class(setrev(x)), c("IDate", "Date"))
18437+
x = structure(integer(0), att = 1L)
18438+
test(2251.55, attr(frev(x), "att"), attr(rev(x), "att"))
1843518439
# errors
1843618440
test(2251.61, frev(data.table()), error="should not be data.frame or data.table")
1843718441
test(2251.62, frev(expression(1)), error="is not supported by frev")

src/utils.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ SEXP startsWithAny(const SEXP x, const SEXP y, SEXP start) {
425425
}
426426

427427
SEXP frev(SEXP x, SEXP copyArg) {
428+
SEXP names, klass, levels;
428429
if (INHERITS(x, char_dataframe))
429430
error(_("'x' should not be data.frame or data.table."));
430431
if (!isNull(getAttrib(x, R_DimSymbol)))
@@ -513,7 +514,16 @@ SEXP frev(SEXP x, SEXP copyArg) {
513514
default:
514515
error(_("Type '%s' is not supported by frev"), type2char(TYPEOF(x)));
515516
}
516-
SEXP names = getAttrib(x, R_NamesSymbol);
517+
names = PROTECT(getAttrib(x, R_NamesSymbol));
518+
klass = PROTECT(getAttrib(x, R_ClassSymbol));
519+
levels = PROTECT(getAttrib(x, R_LevelsSymbol));
520+
nprotect += 3;
521+
if (copy) {
522+
SET_ATTRIB(x, R_NilValue);
523+
setAttrib(x, R_NamesSymbol, names);
524+
setAttrib(x, R_ClassSymbol, klass);
525+
setAttrib(x, R_LevelsSymbol, levels);
526+
}
517527
if (!isNull(names)) {
518528
frev(names, ScalarLogical(FALSE));
519529
}

0 commit comments

Comments
 (0)