Skip to content

Commit 92ed46d

Browse files
committed
wip
1 parent 512cfe9 commit 92ed46d

File tree

4 files changed

+70
-41
lines changed

4 files changed

+70
-41
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ if(CMAKE_CXX_COMPILER_LAUNCHER)
4242
message(STATUS "Using C++ compiler launcher: ${CMAKE_CXX_COMPILER_LAUNCHER}")
4343
endif()
4444

45-
# Versionning
45+
# Versioning
4646
# ===========
4747

4848
set(sparrow_pycapsule_version_path "${SPARROW_PYCAPSULE_INCLUDE_DIR}/sparrow-pycapsule/config/sparrow_pycapsule_version.hpp")
@@ -61,7 +61,7 @@ set(CMAKE_PROJECT_VERSION
6161
message(STATUS "Building sparrow v${CMAKE_PROJECT_VERSION}")
6262

6363
# Binary version
64-
# See the following URL for explanations about the binary versionning
64+
# See the following URL for explanations about the binary versioning
6565
# https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html#Updating-version-info
6666
file(STRINGS "${sparrow_pycapsule_version_path}" sparrow_pycapsule_version_defines
6767
REGEX "constexpr int SPARROW_PYCAPSULE_BINARY_(CURRENT|REVISION|AGE)")

include/sparrow-pycapsule/pycapsule.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace sparrow::pycapsule
2424
*
2525
* @param capsule The PyCapsule containing an ArrowSchema pointer
2626
*/
27-
SPARROW_PYCAPSULE_API void ReleaseArrowSchemaPyCapsule(PyObject* capsule);
27+
SPARROW_PYCAPSULE_API void release_arrow_schema_pycapsule(PyObject* capsule);
2828

2929
/**
3030
* @brief Exports a sparrow array's schema to a PyCapsule.
@@ -35,15 +35,15 @@ namespace sparrow::pycapsule
3535
* @param arr The sparrow array to export (will be moved from)
3636
* @return A new PyCapsule containing the ArrowSchema, or nullptr on error
3737
*/
38-
SPARROW_PYCAPSULE_API PyObject* ExportArrowSchemaPyCapsule(array& arr);
38+
SPARROW_PYCAPSULE_API PyObject* export_arrow_schema_pycapsule(array& arr);
3939

4040
/**
4141
* @brief Retrieves the ArrowSchema pointer from a PyCapsule.
4242
*
4343
* @param capsule The PyCapsule to extract the schema from
4444
* @return Pointer to the ArrowSchema, or nullptr if the capsule is invalid (sets Python exception)
4545
*/
46-
SPARROW_PYCAPSULE_API ArrowSchema* GetArrowSchemaPyCapsule(PyObject* capsule);
46+
SPARROW_PYCAPSULE_API ArrowSchema* get_arrow_schema_pycapsule(PyObject* capsule);
4747

4848
/**
4949
* @brief Capsule destructor for ArrowArray PyCapsules.
@@ -53,7 +53,7 @@ namespace sparrow::pycapsule
5353
*
5454
* @param capsule The PyCapsule containing an ArrowArray pointer
5555
*/
56-
SPARROW_PYCAPSULE_API void ReleaseArrowArrayPyCapsule(PyObject* capsule);
56+
SPARROW_PYCAPSULE_API void release_arrow_array_pycapsule(PyObject* capsule);
5757

5858
/**
5959
* @brief Exports a sparrow array's data to a PyCapsule.
@@ -64,15 +64,15 @@ namespace sparrow::pycapsule
6464
* @param arr The sparrow array to export (will be moved from)
6565
* @return A new PyCapsule containing the ArrowArray, or nullptr on error
6666
*/
67-
SPARROW_PYCAPSULE_API PyObject* ExportArrowArrayPyCapsule(array& arr);
67+
SPARROW_PYCAPSULE_API PyObject* export_arrow_array_pycapsule(array& arr);
6868

6969
/**
7070
* @brief Retrieves the ArrowArray pointer from a PyCapsule.
7171
*
7272
* @param capsule The PyCapsule to extract the array from
7373
* @return Pointer to the ArrowArray, or nullptr if the capsule is invalid (sets Python exception)
7474
*/
75-
SPARROW_PYCAPSULE_API ArrowArray* GetArrowArrayPyCapsule(PyObject* capsule);
75+
SPARROW_PYCAPSULE_API ArrowArray* get_arrow_array_pycapsule(PyObject* capsule);
7676

7777
/**
7878
* @brief Imports a sparrow array from schema and array PyCapsules.

src/pycapsule.cpp

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <sparrow/array.hpp>
66
#include <sparrow/c_interface.hpp>
77

8-
98
namespace sparrow::pycapsule
109
{
1110
namespace
@@ -15,64 +14,94 @@ namespace sparrow::pycapsule
1514
constexpr std::string_view arrow_array_str = "arrow_array";
1615
}
1716

18-
void ReleaseArrowSchemaPyCapsule(PyObject* capsule)
17+
void release_arrow_schema_pycapsule(PyObject* capsule)
1918
{
19+
if (capsule == nullptr)
20+
{
21+
return;
22+
}
2023
auto schema = static_cast<ArrowSchema*>(PyCapsule_GetPointer(capsule, arrow_schema_str.data()));
24+
if (schema == nullptr)
25+
{
26+
return;
27+
}
2128
if (schema->release != nullptr)
2229
{
2330
schema->release(schema);
2431
}
2532
delete schema;
2633
}
2734

28-
PyObject* ExportArrowSchemaPyCapsule(array& arr)
35+
PyObject* export_arrow_schema_pycapsule(array& arr)
2936
{
3037
// Allocate a new ArrowSchema on the heap and extract (move) the schema
3138
ArrowSchema* arrow_schema_ptr = new ArrowSchema();
3239
*arrow_schema_ptr = extract_arrow_schema(std::move(arr));
3340

34-
return PyCapsule_New(arrow_schema_ptr, arrow_schema_str.data(), ReleaseArrowSchemaPyCapsule);
41+
PyObject* capsule_ptr = PyCapsule_New(arrow_schema_ptr, arrow_schema_str.data(), release_arrow_schema_pycapsule);
42+
if(capsule_ptr == nullptr)
43+
{
44+
arrow_schema_ptr->release(arrow_schema_ptr);
45+
delete arrow_schema_ptr;
46+
return nullptr;
47+
}
48+
return capsule_ptr;
3549
}
3650

37-
ArrowSchema* GetArrowSchemaPyCapsule(PyObject* capsule)
51+
ArrowSchema* get_arrow_schema_pycapsule(PyObject* capsule)
3852
{
3953
return static_cast<ArrowSchema*>(PyCapsule_GetPointer(capsule, arrow_schema_str.data()));
4054
}
4155

42-
void ReleaseArrowArrayPyCapsule(PyObject* capsule)
56+
void release_arrow_array_pycapsule(PyObject* capsule)
4357
{
58+
if (capsule == nullptr)
59+
{
60+
return;
61+
}
4462
auto array = static_cast<ArrowArray*>(PyCapsule_GetPointer(capsule, arrow_array_str.data()));
63+
if (array == nullptr)
64+
{
65+
return;
66+
}
4567
if (array->release != nullptr)
4668
{
4769
array->release(array);
4870
}
4971
delete array;
5072
}
5173

52-
PyObject* ExportArrowArrayPyCapsule(array& arr)
74+
PyObject* export_arrow_array_pycapsule(array& arr)
5375
{
5476
// Allocate a new ArrowArray on the heap and extract (move) the array
5577
ArrowArray* arrow_array_ptr = new ArrowArray();
5678
*arrow_array_ptr = extract_arrow_array(std::move(arr));
5779

58-
return PyCapsule_New(arrow_array_ptr, arrow_array_str.data(), ReleaseArrowArrayPyCapsule);
80+
PyObject* capsule_ptr = PyCapsule_New(arrow_array_ptr, arrow_array_str.data(), release_arrow_array_pycapsule);
81+
if(capsule_ptr == nullptr)
82+
{
83+
arrow_array_ptr->release(arrow_array_ptr);
84+
delete arrow_array_ptr;
85+
return nullptr;
86+
}
87+
return capsule_ptr;
5988
}
6089

61-
ArrowArray* GetArrowArrayPyCapsule(PyObject* capsule)
90+
ArrowArray* get_arrow_array_pycapsule(PyObject* capsule)
6291
{
6392
return static_cast<ArrowArray*>(PyCapsule_GetPointer(capsule, arrow_array_str.data()));
6493
}
6594

6695
array import_array_from_capsules(PyObject* schema_capsule, PyObject* array_capsule)
6796
{
68-
ArrowSchema* schema = GetArrowSchemaPyCapsule(schema_capsule);
97+
ArrowSchema* schema = get_arrow_schema_pycapsule(schema_capsule);
6998
if (schema == nullptr)
7099
{
71100
// Error already set by PyCapsule_GetPointer
72101
return array{};
73102
}
74103

75-
ArrowArray* arr = GetArrowArrayPyCapsule(array_capsule);
104+
ArrowArray* arr = get_arrow_array_pycapsule(array_capsule);
76105
if (arr == nullptr)
77106
{
78107
// Error already set by PyCapsule_GetPointer
@@ -101,8 +130,8 @@ namespace sparrow::pycapsule
101130
ArrowSchema* schema_ptr = new ArrowSchema(std::move(arrow_schema));
102131
ArrowArray* array_ptr = new ArrowArray(std::move(arrow_array));
103132

104-
PyObject* schema_capsule = PyCapsule_New(schema_ptr, arrow_schema_str.data(), ReleaseArrowSchemaPyCapsule);
105-
PyObject* array_capsule = PyCapsule_New(array_ptr, arrow_array_str.data(), ReleaseArrowArrayPyCapsule);
133+
PyObject* schema_capsule = PyCapsule_New(schema_ptr, arrow_schema_str.data(), release_arrow_schema_pycapsule);
134+
PyObject* array_capsule = PyCapsule_New(array_ptr, arrow_array_str.data(), release_arrow_array_pycapsule);
106135

107136
return {schema_capsule, array_capsule};
108137
}

test/test_pycapsule.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ namespace sparrow::pycapsule
7070
SUBCASE("creates_valid_capsule")
7171
{
7272
auto arr = make_test_array();
73-
// Note: ExportArrowSchemaPyCapsule moves from arr, so arr becomes invalid after
74-
PyObject* schema_capsule = ExportArrowSchemaPyCapsule(arr);
73+
// Note: export_arrow_schema_pycapsule moves from arr, so arr becomes invalid after
74+
PyObject* schema_capsule = export_arrow_schema_pycapsule(arr);
7575

7676
REQUIRE_NE(schema_capsule, nullptr);
7777
CHECK(PyCapsule_CheckExact(schema_capsule));
@@ -93,7 +93,7 @@ namespace sparrow::pycapsule
9393
SUBCASE("capsule_has_destructor")
9494
{
9595
auto arr = make_test_array();
96-
PyObject* schema_capsule = ExportArrowSchemaPyCapsule(arr);
96+
PyObject* schema_capsule = export_arrow_schema_pycapsule(arr);
9797

9898
REQUIRE_NE(schema_capsule, nullptr);
9999

@@ -119,7 +119,7 @@ namespace sparrow::pycapsule
119119
SUBCASE("creates_valid_capsule")
120120
{
121121
auto arr = make_test_array();
122-
PyObject* array_capsule = ExportArrowArrayPyCapsule(arr);
122+
PyObject* array_capsule = export_arrow_array_pycapsule(arr);
123123

124124
REQUIRE_NE(array_capsule, nullptr);
125125
CHECK(PyCapsule_CheckExact(array_capsule));
@@ -139,7 +139,7 @@ namespace sparrow::pycapsule
139139
SUBCASE("capsule_has_destructor")
140140
{
141141
auto arr = make_test_array();
142-
PyObject* array_capsule = ExportArrowArrayPyCapsule(arr);
142+
PyObject* array_capsule = export_arrow_array_pycapsule(arr);
143143

144144
REQUIRE_NE(array_capsule, nullptr);
145145

@@ -158,7 +158,7 @@ namespace sparrow::pycapsule
158158
SUBCASE("array_has_correct_length")
159159
{
160160
auto arr = make_test_array();
161-
PyObject* array_capsule = ExportArrowArrayPyCapsule(arr);
161+
PyObject* array_capsule = export_arrow_array_pycapsule(arr);
162162

163163
REQUIRE_NE(array_capsule, nullptr);
164164

@@ -177,9 +177,9 @@ namespace sparrow::pycapsule
177177
SUBCASE("returns_valid_schema_pointer")
178178
{
179179
auto arr = make_test_array();
180-
PyObject* schema_capsule = ExportArrowSchemaPyCapsule(arr);
180+
PyObject* schema_capsule = export_arrow_schema_pycapsule(arr);
181181

182-
ArrowSchema* schema = GetArrowSchemaPyCapsule(schema_capsule);
182+
ArrowSchema* schema = get_arrow_schema_pycapsule(schema_capsule);
183183
CHECK_NE(schema, nullptr);
184184
CHECK_NE(schema->release, nullptr);
185185

@@ -192,7 +192,7 @@ namespace sparrow::pycapsule
192192
int dummy = 42;
193193
PyObject* wrong_capsule = PyCapsule_New(&dummy, "wrong_name", nullptr);
194194

195-
ArrowSchema* schema = GetArrowSchemaPyCapsule(wrong_capsule);
195+
ArrowSchema* schema = get_arrow_schema_pycapsule(wrong_capsule);
196196
CHECK_EQ(schema, nullptr);
197197
CHECK_NE(PyErr_Occurred(), nullptr);
198198
PyErr_Clear();
@@ -204,7 +204,7 @@ namespace sparrow::pycapsule
204204
{
205205
PyObject* not_capsule = PyLong_FromLong(42);
206206

207-
ArrowSchema* schema = GetArrowSchemaPyCapsule(not_capsule);
207+
ArrowSchema* schema = get_arrow_schema_pycapsule(not_capsule);
208208
CHECK_EQ(schema, nullptr);
209209
CHECK_NE(PyErr_Occurred(), nullptr);
210210
PyErr_Clear();
@@ -220,9 +220,9 @@ namespace sparrow::pycapsule
220220
SUBCASE("returns_valid_array_pointer")
221221
{
222222
auto arr = make_test_array();
223-
PyObject* array_capsule = ExportArrowArrayPyCapsule(arr);
223+
PyObject* array_capsule = export_arrow_array_pycapsule(arr);
224224

225-
ArrowArray* array = GetArrowArrayPyCapsule(array_capsule);
225+
ArrowArray* array = get_arrow_array_pycapsule(array_capsule);
226226
CHECK_NE(array, nullptr);
227227
CHECK_NE(array->release, nullptr);
228228

@@ -235,7 +235,7 @@ namespace sparrow::pycapsule
235235
int dummy = 42;
236236
PyObject* wrong_capsule = PyCapsule_New(&dummy, "wrong_name", nullptr);
237237

238-
ArrowArray* array = GetArrowArrayPyCapsule(wrong_capsule);
238+
ArrowArray* array = get_arrow_array_pycapsule(wrong_capsule);
239239
CHECK_EQ(array, nullptr);
240240
CHECK_NE(PyErr_Occurred(), nullptr);
241241
PyErr_Clear();
@@ -247,7 +247,7 @@ namespace sparrow::pycapsule
247247
{
248248
PyObject* not_capsule = PyLong_FromLong(42);
249249

250-
ArrowArray* array = GetArrowArrayPyCapsule(not_capsule);
250+
ArrowArray* array = get_arrow_array_pycapsule(not_capsule);
251251
CHECK_EQ(array, nullptr);
252252
CHECK_NE(PyErr_Occurred(), nullptr);
253253
PyErr_Clear();
@@ -283,8 +283,8 @@ namespace sparrow::pycapsule
283283
auto arr = make_test_array();
284284
auto [schema_capsule, array_capsule] = export_array_to_capsules(arr);
285285

286-
ArrowSchema* schema = GetArrowSchemaPyCapsule(schema_capsule);
287-
ArrowArray* array = GetArrowArrayPyCapsule(array_capsule);
286+
ArrowSchema* schema = get_arrow_schema_pycapsule(schema_capsule);
287+
ArrowArray* array = get_arrow_array_pycapsule(array_capsule);
288288

289289
REQUIRE_NE(schema, nullptr);
290290
REQUIRE_NE(array, nullptr);
@@ -408,7 +408,7 @@ namespace sparrow::pycapsule
408408
ArrowSchema* schema = new ArrowSchema();
409409
schema->release = nullptr;
410410

411-
PyObject* capsule = PyCapsule_New(schema, "arrow_schema", ReleaseArrowSchemaPyCapsule);
411+
PyObject* capsule = PyCapsule_New(schema, "arrow_schema", release_arrow_schema_pycapsule);
412412

413413
// This should not crash
414414
Py_DECREF(capsule);
@@ -425,7 +425,7 @@ namespace sparrow::pycapsule
425425
ArrowArray* array = new ArrowArray();
426426
array->release = nullptr;
427427

428-
PyObject* capsule = PyCapsule_New(array, "arrow_array", ReleaseArrowArrayPyCapsule);
428+
PyObject* capsule = PyCapsule_New(array, "arrow_array", release_arrow_array_pycapsule);
429429

430430
// This should not crash
431431
Py_DECREF(capsule);
@@ -440,8 +440,8 @@ namespace sparrow::pycapsule
440440
{
441441
// Create capsules but never consume them
442442
auto arr = make_test_array();
443-
PyObject* schema_capsule = ExportArrowSchemaPyCapsule(arr);
444-
PyObject* array_capsule = ExportArrowArrayPyCapsule(arr);
443+
PyObject* schema_capsule = export_arrow_schema_pycapsule(arr);
444+
PyObject* array_capsule = export_arrow_array_pycapsule(arr);
445445

446446
// Just decref without consuming - destructors should clean up
447447
Py_DECREF(schema_capsule);

0 commit comments

Comments
 (0)