Skip to content

Commit

Permalink
support integer keys in python dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Feb 14, 2017
1 parent cf6348c commit 21f6ba0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 9 additions & 3 deletions R/python.R
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,16 @@ dict <- function(...) {
frame <- parent.frame()
keys <- lapply(names, function(name) {
if (exists(name, envir = frame, inherits = TRUE))
get(name, envir = frame, inherits = TRUE)
else
name
key <- get(name, envir = frame, inherits = TRUE)
else {
if (grepl("[0-9]+", name))
name <- as.integer(name)
else
name
}
})



# construct dict
py_dict(keys, values)
Expand Down
6 changes: 4 additions & 2 deletions src/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,10 @@ SEXP py_to_r(PyObject* x) {
// iterate over dict
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(x, &pos, &key, &value))
list[as_std_string(key)] = py_to_r(value);
while (PyDict_Next(x, &pos, &key, &value)) {
PyObjectPtr str(PyObject_Str(key));
list[as_std_string(str)] = py_to_r(value);
}
return list;
}

Expand Down

0 comments on commit 21f6ba0

Please sign in to comment.