Skip to content

Commit 233ec43

Browse files
committed
chore(python): add comments to conditional preprocessor branches, use Py_LIMITED_API_PRE_11
1 parent 4ac503f commit 233ec43

File tree

1 file changed

+52
-51
lines changed

1 file changed

+52
-51
lines changed

python/_brotli.c

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@
1111
#define PyInt_AsLong PyLong_AsLong
1212
#ifdef Py_LIMITED_API
1313
// macro functions are version specific and therefore not compatible with the stable api
14-
// therefore we map them here to the functions
14+
// due to that we map them here to the functions to not break compatibility
1515
#define PyList_GET_ITEM PyList_GetItem
1616
#define PyList_SET_ITEM PyList_SetItem
1717
#define PyBytes_AS_STRING PyBytes_AsString
18-
// as we want to support 3.6+ we need to define a dummy struct
19-
// to not break compatibility with Python 2
20-
// len is of type int, as Py_ssize_t clean is 3.7+
18+
#endif /* Py_LIMITED_API */
19+
#else /* PY_MAJOR_VERSION >= 3 */
20+
#define Py_ARRAY_LENGTH(array) (sizeof(array) / sizeof((array)[0]))
21+
#endif /* PY_MAJOR_VERSION >= 3 */
22+
23+
/* following macro indicates the use of the limited api earlier than 3.11 */
24+
#define Py_LIMITED_API_PRE_11 (Py_LIMITED_API && PY_VERSION_HEX < 0x030B0000)
2125

22-
#ifdef Py_LIMITED_API && PY_VERSION_HEX < 0x030B0000
26+
#if Py_LIMITED_API_PRE_11
2327
// Py_buffer is only available in stable api 3.11+
28+
// to keep the original behavior we define a dummy here
2429
typedef struct {
2530
void *buf;
2631
int len;
@@ -29,11 +34,7 @@ static void PyBuffer_Release(Py_buffer *view) {
2934
view->buf = NULL;
3035
view->len = -1;
3136
}
32-
#endif
33-
#endif
34-
#else
35-
#define Py_ARRAY_LENGTH(array) (sizeof(array) / sizeof((array)[0]))
36-
#endif
37+
#endif /* Py_LIMITED_API_PRE_11 */
3738

3839
static PyObject *BrotliError;
3940

@@ -394,12 +395,12 @@ static void brotli_Compressor_dealloc(brotli_Compressor* self) {
394395
#if PY_MAJOR_VERSION >= 3
395396
#ifdef Py_LIMITED_API
396397
PyObject_Del((PyObject*)self);
397-
#else
398+
#else /* Py_LIMITED_API */
398399
Py_TYPE(self)->tp_free((PyObject*)self);
399-
#endif
400-
#else
400+
#endif /* Py_LIMITED_API */
401+
#else /* PY_MAJOR_VERSION >= 3 */
401402
self->ob_type->tp_free((PyObject*)self);
402-
#endif
403+
#endif /* PY_MAJOR_VERSION >= 3 */
403404
}
404405

405406
#ifndef Py_LIMITED_API
@@ -413,7 +414,7 @@ static PyObject* brotli_Compressor_new(PyTypeObject *type, PyObject *args, PyObj
413414

414415
return (PyObject *)self;
415416
}
416-
#endif
417+
#endif /* ndef Py_LIMITED_API */
417418

418419
static int brotli_Compressor_init(brotli_Compressor *self, PyObject *args, PyObject *keywds) {
419420
#ifdef Py_LIMITED_API
@@ -422,7 +423,7 @@ static int brotli_Compressor_init(brotli_Compressor *self, PyObject *args, PyObj
422423
PyErr_SetString(BrotliError, "Unable to create BrotliEncoderState");
423424
return -1;
424425
}
425-
#endif
426+
#endif /* Py_LIMITED_API */
426427
BrotliEncoderMode mode = (BrotliEncoderMode) -1;
427428
int quality = -1;
428429
int lgwin = -1;
@@ -480,14 +481,14 @@ static PyObject* brotli_Compressor_process(brotli_Compressor *self, PyObject *ar
480481
int ok;
481482

482483
#if PY_MAJOR_VERSION >= 3
483-
#ifdef Py_LIMITED_API && PY_VERSION_HEX < 0x030B0000
484+
#if PY_LIMITED_API_PRE_11
484485
ok = PyArg_ParseTuple(args, "y#:process", &input.buf, &input.len);
485-
#else
486+
#else /* PY_LIMITED_API_PRE_11 */
486487
ok = PyArg_ParseTuple(args, "y*:process", &input);
487-
#endif
488-
#else
488+
#endif /* PY_LIMITED_API_PRE_11 */
489+
#else /* PY_MAJOR_VERSION >= 3 */
489490
ok = PyArg_ParseTuple(args, "s*:process", &input);
490-
#endif
491+
#endif /* PY_MAJOR_VERSION >= 3 */
491492

492493
if (!ok) {
493494
return NULL;
@@ -617,14 +618,14 @@ static PyType_Spec brotli_Compressor_Spec = {
617618
Py_TPFLAGS_DEFAULT, // unsigned int flags;
618619
brotli_Compressor_slots, // PyType_Slot *slots;
619620
};
620-
#else
621+
#else /* Py_LIMITED_API */
621622
static PyTypeObject brotli_CompressorType = {
622623
#if PY_MAJOR_VERSION >= 3
623624
PyVarObject_HEAD_INIT(NULL, 0)
624-
#else
625+
#else /* PY_MAJOR_VERSION >= 3 */
625626
PyObject_HEAD_INIT(NULL)
626627
0, /* ob_size*/
627-
#endif
628+
#endif /* PY_MAJOR_VERSION >= 3 */
628629
"brotli.Compressor", /* tp_name */
629630
sizeof(brotli_Compressor), /* tp_basicsize */
630631
0, /* tp_itemsize */
@@ -663,7 +664,7 @@ static PyTypeObject brotli_CompressorType = {
663664
0, /* tp_alloc */
664665
brotli_Compressor_new, /* tp_new */
665666
};
666-
#endif
667+
#endif /* Py_LIMITED_API */
667668

668669
PyDoc_STRVAR(brotli_Decompressor_doc,
669670
"An object to decompress a byte string.\n"
@@ -688,12 +689,12 @@ static void brotli_Decompressor_dealloc(brotli_Decompressor* self) {
688689
#if PY_MAJOR_VERSION >= 3
689690
#ifdef Py_LIMITED_API
690691
PyObject_Del((PyObject*)self);
691-
#else
692+
#else /* Py_LIMITED_API */
692693
Py_TYPE(self)->tp_free((PyObject*)self);
693-
#endif
694-
#else
694+
#endif /* Py_LIMITED_API */
695+
#else /* PY_MAJOR_VERSION >= 3 */
695696
self->ob_type->tp_free((PyObject*)self);
696-
#endif
697+
#endif /* PY_MAJOR_VERSION >= 3 */
697698
}
698699

699700
#ifndef Py_LIMITED_API
@@ -710,7 +711,7 @@ static PyObject* brotli_Decompressor_new(PyTypeObject *type, PyObject *args, PyO
710711

711712
return (PyObject *)self;
712713
}
713-
#endif
714+
#endif /* ndef Py_LIMITED_API */
714715

715716
static int brotli_Decompressor_init(brotli_Decompressor *self, PyObject *args, PyObject *keywds) {
716717
#ifdef Py_LIMITED_API
@@ -721,7 +722,7 @@ static int brotli_Decompressor_init(brotli_Decompressor *self, PyObject *args, P
721722
}
722723
self->unconsumed_data = NULL;
723724
self->unconsumed_data_length = 0;
724-
#endif
725+
#endif /* Py_LIMITED_API */
725726
int ok;
726727

727728
static const char *kwlist[] = {NULL};
@@ -845,14 +846,14 @@ static PyObject* brotli_Decompressor_process(brotli_Decompressor *self, PyObject
845846
static char* kwlist[] = { "", "max_output_length", NULL };
846847

847848
#if PY_MAJOR_VERSION >= 3
848-
#ifdef Py_LIMITED_API && PY_VERSION_HEX < 0x030B0000
849+
#if PY_LIMITED_API_PRE_11
849850
ok = PyArg_ParseTupleAndKeywords(args, keywds, "y#|n:process", kwlist, &input.buf, &input.len, &max_output_length);
850-
#else
851+
#else /* PY_LIMITED_API_PRE_11 */
851852
ok = PyArg_ParseTupleAndKeywords(args, keywds, "y*|n:process", kwlist, &input, &max_output_length);
852-
#endif
853-
#else
853+
#endif /* PY_LIMITED_API_PRE_11 */
854+
#else /* PY_MAJOR_VERSION >= 3 */
854855
ok = PyArg_ParseTupleAndKeywords(args, keywds, "s*|n:process", kwlist, &input, &max_output_length);
855-
#endif
856+
#endif /* PY_MAJOR_VERSION >= 3 */
856857

857858
if (!ok) {
858859
return NULL;
@@ -968,14 +969,14 @@ static PyType_Spec brotli_Decompressor_Spec = {
968969
Py_TPFLAGS_DEFAULT, // unsigned int flags;
969970
brotli_Decompressor_slots, // PyType_Slot *slots;
970971
};
971-
#else
972+
#else /* Py_LIMITED_API */
972973
static PyTypeObject brotli_DecompressorType = {
973974
#if PY_MAJOR_VERSION >= 3
974975
PyVarObject_HEAD_INIT(NULL, 0)
975-
#else
976+
#else /* PY_MAJOR_VERSION >= 3 */
976977
PyObject_HEAD_INIT(NULL)
977978
0, /* ob_size*/
978-
#endif
979+
#endif /* PY_MAJOR_VERSION >= 3 */
979980
"brotli.Decompressor", /* tp_name */
980981
sizeof(brotli_Decompressor), /* tp_basicsize */
981982
0, /* tp_itemsize */
@@ -1014,7 +1015,7 @@ static PyTypeObject brotli_DecompressorType = {
10141015
0, /* tp_alloc */
10151016
brotli_Decompressor_new, /* tp_new */
10161017
};
1017-
#endif
1018+
#endif /* Py_LIMITED_API */
10181019

10191020
PyDoc_STRVAR(brotli_decompress__doc__,
10201021
"Decompress a compressed byte string.\n"
@@ -1048,17 +1049,17 @@ static PyObject* brotli_decompress(PyObject *self, PyObject *args, PyObject *key
10481049
int ok;
10491050

10501051
#if PY_MAJOR_VERSION >= 3
1051-
#ifdef Py_LIMITED_API && PY_VERSION_HEX < 0x030B0000
1052+
#if PY_LIMITED_API_PRE_11
10521053
ok = PyArg_ParseTupleAndKeywords(args, keywds, "y#|:decompress",
10531054
(char**) kwlist, &input.buf, &input.len);
1054-
#else
1055+
#else /* PY_LIMITED_API_PRE_11 */
10551056
ok = PyArg_ParseTupleAndKeywords(args, keywds, "y*|:decompress",
10561057
(char**) kwlist, &input);
1057-
#endif
1058-
#else
1058+
#endif /* PY_LIMITED_API_PRE_11 */
1059+
#else /* PY_MAJOR_VERSION >= 3 */
10591060
ok = PyArg_ParseTupleAndKeywords(args, keywds, "s*|:decompress",
10601061
(char**) kwlist, &input);
1061-
#endif
1062+
#endif /* PY_MAJOR_VERSION >= 3 */
10621063

10631064
if (!ok) {
10641065
return NULL;
@@ -1135,12 +1136,12 @@ static struct PyModuleDef brotli_module = {
11351136
NULL, /* m_clear */
11361137
NULL /* m_free */
11371138
};
1138-
#else
1139+
#else /* PY_MAJOR_VERSION >= 3 */
11391140
#define INIT_BROTLI init_brotli
11401141
#define CREATE_BROTLI Py_InitModule3("_brotli", brotli_methods, brotli_doc)
11411142
#define RETURN_BROTLI return
11421143
#define RETURN_NULL return
1143-
#endif
1144+
#endif /* PY_MAJOR_VERSION >= 3 */
11441145

11451146
PyMODINIT_FUNC INIT_BROTLI(void) {
11461147
PyObject *m = CREATE_BROTLI;
@@ -1153,9 +1154,9 @@ PyMODINIT_FUNC INIT_BROTLI(void) {
11531154

11541155
#ifdef Py_LIMITED_API
11551156
PyTypeObject* brotli_CompressorType_ref = (PyTypeObject *) PyType_FromSpec(&brotli_Compressor_Spec);
1156-
#else
1157+
#else /* Py_LIMITED_API */
11571158
PyTypeObject* brotli_CompressorType_ref = &brotli_CompressorType;
1158-
#endif
1159+
#endif /* Py_LIMITED_API */
11591160
if (PyType_Ready(brotli_CompressorType_ref) < 0) {
11601161
RETURN_NULL;
11611162
}
@@ -1164,9 +1165,9 @@ PyMODINIT_FUNC INIT_BROTLI(void) {
11641165

11651166
#ifdef Py_LIMITED_API
11661167
PyTypeObject* brotli_DecompressorType_ref = (PyTypeObject *) PyType_FromSpec(&brotli_Decompressor_Spec);
1167-
#else
1168+
#else /* Py_LIMITED_API */
11681169
PyTypeObject* brotli_DecompressorType_ref = &brotli_DecompressorType;
1169-
#endif
1170+
#endif /* Py_LIMITED_API */
11701171
if (PyType_Ready(brotli_DecompressorType_ref) < 0) {
11711172
RETURN_NULL;
11721173
}

0 commit comments

Comments
 (0)