Skip to content

Commit 7445df7

Browse files
Guard against infinite S3 dispatch loop (#6744)
* Guard against infinite S3 dispatch loop * Test of simpler class reversal * fix tests * nocov for apparently spurious miss
1 parent cffb4a1 commit 7445df7

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

R/as.data.table.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ as.data.table.list = function(x,
214214
}
215215

216216
as.data.table.data.frame = function(x, keep.rownames=FALSE, key=NULL, ...) {
217+
if (is.data.table(x)) return(as.data.table.data.table(x)) # S3 is weird, #6739. Also # nocov; this is tested in 2302.{2,3}, not sure why it doesn't show up in coverage.
217218
if (!identical(class(x), "data.frame")) return(as.data.table(as.data.frame(x)))
218219
if (!isFALSE(keep.rownames)) {
219220
# can specify col name to keep.rownames, #575; if it's the same as key,

inst/tests/tests.Rraw

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20748,15 +20748,19 @@ test(2301.3, DT[order(a, -b, decreasing=c(TRUE, TRUE))], error="Mixing '-' with
2074820748
test(2301.4, DT[order(a, b, decreasing=c(TRUE, TRUE, FALSE))], error="decreasing= has length 3")
2074920749

2075020750
# as.data.table should remove extra attributes from extended data.frames #5699
20751-
x = data.frame(a=c(1,5,3), b=c(2,4,6))
20751+
x = data.frame(a=c(1, 5, 3), b=c(2, 4, 6))
2075220752
class(x) = c("tbl", "data.frame")
2075320753
attr(x, "t1") = "a"
2075420754
as.data.frame.tbl = function(x) {
2075520755
attr(x, "t1") = NULL
2075620756
class(x) = "data.frame"
2075720757
x
2075820758
}
20759-
test(2302, attr(as.data.table(y), "t1"), attr(as.data.frame(y), "t1"))
20759+
test(2302.1, attr(as.data.table(y), "t1"), attr(as.data.frame(y), "t1"))
20760+
class(x) = c("data.frame", "data.table", "data.frame") # #6739
20761+
test(2302.2, attr(as.data.table(y), "t1"), attr(as.data.frame(y), "t1"))
20762+
class(x) = c("data.frame", "data.table") # duplicate class is overkill
20763+
test(2302.3, attr(as.data.table(y), "t1"), attr(as.data.frame(y), "t1"))
2076020764

2076120765
# by=foo(KEY) does not retain key (no way to guarantee monotonic transformation), #5583
2076220766
DT = data.table(a=1:2, key='a')

0 commit comments

Comments
 (0)