Skip to content

Commit 163d21b

Browse files
committed
removed caller
1 parent 0ff7e8d commit 163d21b

1 file changed

Lines changed: 27 additions & 21 deletions

File tree

R/data.table.R

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -128,32 +128,29 @@ replace_dot_alias = function(e) {
128128
}
129129
}
130130

131-
.assign_in_parent_exact = function(name, x, env, caller) {
131+
.assign_in_parent = function(name, value, env, err_msg_len, err_msg_na) {
132132
k = eval(name[[2L]], env, env)
133133
if (is.list(k)) {
134134
origj = j = if (name[[1L]] == "$") as.character(name[[3L]]) else eval(name[[3L]], env, env)
135-
136-
if (caller == "[.data.table") {
137-
if (is.character(j)) {
138-
if (length(j) != 1L) stopf("Cannot assign to an under-allocated recursively indexed list -- L[[i]][,:=] syntax is only valid when i is length 1, but its length is %d", length(j))
139-
j = match(j, names(k))
140-
if (is.na(j)) internal_error("item '%s' not found in names of list", origj) # nocov
141-
}
142-
} else if (caller == "setDT") {
143-
if (length(j) == 1L) {
144-
if (is.character(j)) {
145-
j = match(j, names(k))
146-
if (is.na(j))
147-
stopf("Item '%s' not found in names of input list", origj)
135+
if (length(j) != 1L) {
136+
stopf(err_msg_len, length(j))
137+
}
138+
if (is.character(j)) {
139+
idx = match(j, names(k))
140+
if (is.na(idx)) {
141+
if (is.null(err_msg_na)) {
142+
internal_error("item '%s' not found in names of list", origj)
143+
} else {
144+
stopf(err_msg_na, origj)
148145
}
149146
}
147+
j = idx
150148
}
151-
152-
.Call(Csetlistelt, k, as.integer(j), x)
153-
} else if (is.environment(k) && exists(as.character(name[[3L]]), k)) {
154-
assign(as.character(name[[3L]]), x, k, inherits = FALSE)
149+
.Call(Csetlistelt, k, as.integer(j), value)
150+
} else if (is.environment(k) && exists(as.character(name[[3L]]), k, inherits = FALSE)) {
151+
assign(as.character(name[[3L]]), value, k, inherits = FALSE)
155152
} else if (isS4(k)) {
156-
.Call(CsetS4elt, k, as.character(name[[3L]]), x)
153+
.Call(CsetS4elt, k, as.character(name[[3L]]), value)
157154
}
158155
}
159156

@@ -1244,7 +1241,11 @@ replace_dot_alias = function(e) {
12441241
if (is.name(name)) {
12451242
assign(as.character(name),x,parent.frame(),inherits=TRUE)
12461243
} else if (.is_simple_extraction(name)) {
1247-
.assign_in_parent_exact(name, x, parent.frame(), caller = "[.data.table")
1244+
.assign_in_parent(
1245+
name, x, parent.frame(),
1246+
err_msg_len = "Cannot assign to an under-allocated recursively indexed list -- L[[i]][,:=] syntax is only valid when i is length 1, but its length is %d",
1247+
err_msg_na = NULL # Triggers internal_error for this case # nocov
1248+
)
12481249
} # TO DO: else if env$<- or list$<-
12491250
}
12501251
}
@@ -2988,7 +2989,12 @@ setDT = function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE) {
29882989
name = as.character(name)
29892990
assign(name, x, parent.frame(), inherits=TRUE)
29902991
} else if (.is_simple_extraction(name)) {
2991-
.assign_in_parent_exact(name, x, parent.frame(), caller = "setDT")
2992+
# common case is call from 'lapply()'
2993+
.assign_in_parent(
2994+
name, x, parent.frame(),
2995+
err_msg_len = "The index for recursive assignment must be length 1, but its length is %d.",
2996+
err_msg_na = "Item '%s' not found in names of input list"
2997+
)
29922998
} else if (name %iscall% "get") { # #6725
29932999
# edit 'get(nm, env)' call to be 'assign(nm, x, envir=env)'
29943000
name = match.call(get, name)

0 commit comments

Comments
 (0)