Skip to content

Commit 21c4d4b

Browse files
ARROW-7988: [R] Fix on.exit calls in reticulate bindings
I missed this/didn't closely review after the refactoring that pulled the schema out of the array object. `on.exit()` overwrites the existing finalizer (more recent versions of R have added an `add = TRUE` argument that lets you append, but we still support older versions) so we should only call it once in each function. As the code currently stands, we wouldn't actually be calling `delete_arrow_schema(schema_ptr)`. Closes #6519 from nealrichardson/reticulate-followup and squashes the following commits: 4336e2a <Neal Richardson> Fix on.exit calls Authored-by: Neal Richardson <[email protected]> Signed-off-by: Neal Richardson <[email protected]>
1 parent c707d8b commit 21c4d4b

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

r/R/py-to-r.R

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@
1717

1818
py_to_r.pyarrow.lib.Array <- function(x, ...) {
1919
schema_ptr <- allocate_arrow_schema()
20-
on.exit(delete_arrow_schema(schema_ptr))
2120
array_ptr <- allocate_arrow_array()
22-
on.exit(delete_arrow_array(array_ptr))
21+
on.exit({
22+
delete_arrow_schema(schema_ptr)
23+
delete_arrow_array(array_ptr)
24+
})
2325

2426
x$`_export_to_c`(array_ptr, schema_ptr)
2527
Array$create(ImportArray(array_ptr, schema_ptr))
2628
}
2729

2830
r_to_py.Array <- function(x, convert = FALSE) {
2931
schema_ptr <- allocate_arrow_schema()
30-
on.exit(delete_arrow_schema(schema_ptr))
3132
array_ptr <- allocate_arrow_array()
32-
on.exit(delete_arrow_array(array_ptr))
33+
on.exit({
34+
delete_arrow_schema(schema_ptr)
35+
delete_arrow_array(array_ptr)
36+
})
3337

3438
pa <- reticulate::import("pyarrow", convert = convert)
3539
ExportArray(x, array_ptr, schema_ptr)
@@ -38,20 +42,24 @@ r_to_py.Array <- function(x, convert = FALSE) {
3842

3943
py_to_r.pyarrow.lib.RecordBatch <- function(x, ...) {
4044
schema_ptr <- allocate_arrow_schema()
41-
on.exit(delete_arrow_schema(schema_ptr))
4245
array_ptr <- allocate_arrow_array()
43-
on.exit(delete_arrow_array(array_ptr))
46+
on.exit({
47+
delete_arrow_schema(schema_ptr)
48+
delete_arrow_array(array_ptr)
49+
})
4450

4551
x$`_export_to_c`(array_ptr, schema_ptr)
4652
shared_ptr(RecordBatch, ImportRecordBatch(array_ptr, schema_ptr))
4753
}
4854

4955
r_to_py.RecordBatch <- function(x, convert = FALSE) {
5056
schema_ptr <- allocate_arrow_schema()
51-
on.exit(delete_arrow_schema(schema_ptr))
5257
array_ptr <- allocate_arrow_array()
53-
on.exit(delete_arrow_array(array_ptr))
54-
58+
on.exit({
59+
delete_arrow_schema(schema_ptr)
60+
delete_arrow_array(array_ptr)
61+
})
62+
5563
pa <- reticulate::import("pyarrow", convert = convert)
5664
ExportRecordBatch(x, array_ptr, schema_ptr)
5765
pa$RecordBatch$`_import_from_c`(array_ptr, schema_ptr)

0 commit comments

Comments
 (0)