Skip to content

Commit 46f6060

Browse files
committed
[compat] numpy 1.x and 2.x compatibility macros
1 parent 9384f2a commit 46f6060

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

src/decoder.c

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@
2626
#include "decoder.h"
2727
#include "python_funcs.h"
2828

29+
/******************************************************************************/
30+
/* NumPy 1.x/2.x compatibility macros */
31+
/******************************************************************************/
32+
33+
#ifndef PyDataType_ELSIZE
34+
#define PyDataType_ELSIZE(d) ((d)->elsize)
35+
#endif
36+
37+
#ifndef PyDataType_TYPE_NUM
38+
#define PyDataType_TYPE_NUM(d) ((d)->type_num)
39+
#endif
40+
2941
/******************************************************************************/
3042

3143
#define RECURSE_AND_RETURN_OR_BAIL(action, recurse_msg) {\
@@ -150,9 +162,6 @@ static _soa_schema_t* _decode_soa_schema(_bjdata_decoder_buffer_t* buffer);
150162

151163
/******************************************************************************/
152164

153-
/* Returns new decoder buffer or NULL on failure (an exception will be set). Input must either support buffer interface
154-
* or be callable. Currently only increases reference count for input parameter.
155-
*/
156165
_bjdata_decoder_buffer_t* _bjdata_decoder_buffer_create(_bjdata_decoder_prefs_t* prefs, PyObject* input,
157166
PyObject* seek) {
158167
_bjdata_decoder_buffer_t* buffer;
@@ -198,26 +207,19 @@ _bjdata_decoder_buffer_t* _bjdata_decoder_buffer_create(_bjdata_decoder_prefs_t*
198207
return NULL;
199208
}
200209

201-
// Returns non-zero if buffer cleanup/finalisation failed and no other exception was set already
202210
int _bjdata_decoder_buffer_free(_bjdata_decoder_buffer_t** buffer) {
203211
int failed = 0;
204212

205213
if (NULL != buffer && NULL != *buffer) {
206214
if ((*buffer)->view_set) {
207-
// In buffered mode, rewind to position in stream up to which actually read (rather than buffered)
208215
if (NULL != (*buffer)->seek && (*buffer)->view.len > (*buffer)->pos) {
209216
PyObject* type, *value, *traceback, *seek_result;
210217

211-
// preserve the previous exception, if set
212218
PyErr_Fetch(&type, &value, &traceback);
213219
seek_result = PyObject_CallFunction((*buffer)->seek, "nn",
214220
((*buffer)->pos - (*buffer)->view.len), IO_SEEK_CUR);
215221
Py_XDECREF(seek_result);
216222

217-
/* Blindly calling PyErr_Restore would clear any exception raised by seek call. If however already had
218-
* an error before freeing buffer (this function), propagate that instead. (I.e. this behaves like a
219-
* nested try-except block.
220-
*/
221223
if (NULL != type) {
222224
PyErr_Restore(type, value, traceback);
223225
} else if (NULL == seek_result) {
@@ -243,15 +245,6 @@ int _bjdata_decoder_buffer_free(_bjdata_decoder_buffer_t** buffer) {
243245
return failed;
244246
}
245247

246-
/* Tries to read len bytes from input, returning read chunk. Len is updated to how many bytes were actually read.
247-
* If not NULL, dst_buffer can be an existing buffer to output len bytes into.
248-
* Returns NULL if either no input is left (len is set to zero) or an error occurs (len is non-zero). The caller must
249-
* NOT modify or free the returned chunk unless they specified out_buffer (in which case that is returned). When this
250-
* function is called again, the previously returned output is no longer valid (unless was created by caller).
251-
*
252-
* This function reads from a fixed buffer (single byte array)
253-
*/
254-
255248
static const char* _decoder_buffer_read_fixed(_bjdata_decoder_buffer_t* buffer, Py_ssize_t* len, char* dst_buffer) {
256249
Py_ssize_t old_pos;
257250

@@ -2169,4 +2162,4 @@ int _bjdata_decoder_init(void) {
21692162
void _bjdata_decoder_cleanup(void) {
21702163
Py_CLEAR(DecoderException);
21712164
Py_CLEAR(PyDec_Type);
2172-
}
2165+
}

src/encoder.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@
2727
#include "encoder.h"
2828
#include "python_funcs.h"
2929

30+
/******************************************************************************/
31+
/* NumPy 1.x/2.x compatibility macros */
32+
/******************************************************************************/
33+
34+
#ifndef PyDataType_ELSIZE
35+
#define PyDataType_ELSIZE(d) ((d)->elsize)
36+
#endif
37+
38+
#ifndef PyDataType_TYPE_NUM
39+
#define PyDataType_TYPE_NUM(d) ((d)->type_num)
40+
#endif
41+
3042
/******************************************************************************/
3143

3244
static char bytes_array_prefix[] = {ARRAY_START, CONTAINER_TYPE, TYPE_BYTE, CONTAINER_COUNT};
@@ -326,7 +338,7 @@ static inline int _is_string_type(int type_num) {
326338

327339
/* Recursively check if a dtype is supported for SOA encoding */
328340
static int _is_soa_compatible_dtype(PyArray_Descr* fd) {
329-
int type_num = fd->type_num;
341+
int type_num = PyDataType_TYPE_NUM(fd);
330342

331343
/* String types are supported */
332344
if (_is_string_type(type_num)) {
@@ -468,7 +480,7 @@ static int _encode_field_name(PyObject* name, _bjdata_encoder_buffer_t* buffer)
468480

469481
/* Write schema for a field recursively (handles nested structs) */
470482
static int _write_field_schema_recursive(PyArray_Descr* fd, _bjdata_encoder_buffer_t* buffer) {
471-
int type_num = fd->type_num;
483+
int type_num = PyDataType_TYPE_NUM(fd);
472484
Py_ssize_t i;
473485

474486
/* Handle NPY_VOID: could be sub-array or nested struct */
@@ -488,7 +500,7 @@ static int _write_field_schema_recursive(PyArray_Descr* fd, _bjdata_encoder_buff
488500
}
489501
}
490502

491-
int base_type = base_dtype->type_num;
503+
int base_type = PyDataType_TYPE_NUM(base_dtype);
492504
Py_DECREF(subdtype);
493505

494506
/* Write sub-array schema: [TTT...] */
@@ -576,7 +588,7 @@ static int _write_field_schema_recursive(PyArray_Descr* fd, _bjdata_encoder_buff
576588
/* String types - write NUMPY BYTE SIZE (not character count) */
577589
if (_is_string_type(type_num)) {
578590
WRITE_CHAR_OR_BAIL(TYPE_STRING);
579-
BAIL_ON_NONZERO(_encode_longlong(fd->elsize, buffer));
591+
BAIL_ON_NONZERO(_encode_longlong(PyDataType_ELSIZE(fd), buffer));
580592
return 0;
581593
}
582594

@@ -916,9 +928,9 @@ static int _encode_soa(PyArrayObject* arr, _bjdata_encoder_buffer_t* buffer, int
916928

917929
PyArray_Descr* fd = (PyArray_Descr*)PyTuple_GET_ITEM(info, 0);
918930
field_offset[i] = PyLong_AsSsize_t(PyTuple_GET_ITEM(info, 1));
919-
field_itemsize[i] = fd->elsize;
931+
field_itemsize[i] = PyDataType_ELSIZE(fd);
920932

921-
int type_num = fd->type_num;
933+
int type_num = PyDataType_TYPE_NUM(fd);
922934

923935
if (type_num == NPY_BOOL) {
924936
field_type[i] = 1;

0 commit comments

Comments
 (0)