Skip to content

Commit edb044f

Browse files
selfref.ok() helper to close #7329 (#7332)
* selfref.ok() helper to close #7329 * rename to dot prefix * Update NEWS.md * re-emphasize technical nature of .selfref.ok --------- Co-authored-by: Michael Chirico <[email protected]>
1 parent 68d00f4 commit edb044f

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ S3method(format_list_item, data.frame)
215215
export(fdroplevels, setdroplevels)
216216
S3method(droplevels, data.table)
217217
export(frev)
218+
export(.selfref.ok)
218219

219220
# sort_by added in R 4.4.0, #6662, https://stat.ethz.ch/pipermail/r-announce/2024/000701.html
220221
if (getRversion() >= "4.4.0") S3method(sort_by, data.table)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@
311311
312312
6. Using a double vector in `set()`'s `i=` and/or `j=` no longer throws a warning about preferring integer, [#6594](https://github.com/Rdatatable/data.table/issues/6594). While it may improve efficiency to use integer, there's no guarantee it's an improvement and the difference is likely to be minimal. The coercion will still be reported under `datatable.verbose=TRUE`. For package/production use cases, static analyzers such as `lintr::implicit_integer_linter()` can also report when numeric literals should be rewritten as integer literals.
313313

314+
7. In rare situations a data.table object may lose its internal attribute that holds a self-reference. New helper function `.selfref.ok()` tests just that. It is only intended for technical use cases. See manual for examples.
315+
314316
## data.table [v1.17.8](https://github.com/Rdatatable/data.table/milestone/41) (6 July 2025)
315317

316318
1. Internal functions used to signal errors are now marked as non-returning, silencing a compiler warning about potentially unchecked allocation failure. Thanks to Prof. Brian D. Ripley for the report and @aitap for the fix, [#7070](https://github.com/Rdatatable/data.table/pull/7070).

R/helpers.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@ fctr = function(x, levels=unique(x), ..., sort=FALSE, rev=FALSE) {
1010
if (rev) levels = frev(levels)
1111
factor(x, levels=levels, ...)
1212
}
13+
14+
# add a function for validating data.tables that might need setDT #7329
15+
.selfref.ok = function(x) {
16+
if (!is.data.table(x))
17+
stopf(".selfref.ok expects data.table class object.")
18+
selfrefok(x, verbose=FALSE) > 0L
19+
}

inst/tests/tests.Rraw

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21668,3 +21668,20 @@ local({
2166821668
...123 = 'a'
2166921669
test(2339.11, DT[, .....123], DT)
2167021670
})
21671+
21672+
# Add a function for validating data.tables that might need setDT #7329
21673+
test(2340.00, .selfref.ok(data.frame(V1=1L)), error=".selfref.ok expects data.table class object")
21674+
d1 = structure(list(V1=1L), class=c("data.table","data.frame"))
21675+
test(2340.01, .selfref.ok(d1), FALSE)
21676+
setDT(d1)
21677+
test(2340.02, .selfref.ok(d1), TRUE)
21678+
saveRDS(d1, f<-tempfile())
21679+
d2 = readRDS(f)
21680+
test(2340.03, .selfref.ok(d2), FALSE)
21681+
invisible(file.remove(f))
21682+
setDT(d2)
21683+
test(2340.04, .selfref.ok(d2), TRUE)
21684+
d3 = unserialize(serialize(d2, NULL))
21685+
test(2340.05, .selfref.ok(d3), FALSE)
21686+
setDT(d3)
21687+
test(2340.06, .selfref.ok(d3), TRUE)

man/.selfref.ok.Rd

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
\name{.selfref.ok}
2+
\alias{.selfref.ok}
3+
\title{Tests self reference of a data.table}
4+
\description{
5+
In rare situations, as presented in examples below, a data.table object may lose its internal attribute that holds a self-reference. This function tests just that.
6+
7+
It is not expected that many end users will have need for this highly technical function about data.table internals.
8+
}
9+
\usage{
10+
.selfref.ok(x)
11+
}
12+
\arguments{
13+
\item{x}{ A data.table. }
14+
}
15+
\value{
16+
\code{TRUE} if self reference attribute is properly set, \code{FALSE} otherwise.
17+
}
18+
\examples{
19+
d1 = structure(list(V1=1L), class=c("data.table","data.frame"))
20+
.selfref.ok(d1)
21+
setDT(d1)
22+
.selfref.ok(d1)
23+
24+
saveRDS(d1, f<-tempfile())
25+
d2 = readRDS(f)
26+
.selfref.ok(d2)
27+
invisible(file.remove(f))
28+
setDT(d2)
29+
.selfref.ok(d2)
30+
31+
d3 = unserialize(serialize(d2, NULL))
32+
.selfref.ok(d3)
33+
setDT(d3)
34+
.selfref.ok(d3)
35+
}
36+
\keyword{ data }

0 commit comments

Comments
 (0)