18
18
#define Py_ARRAY_LENGTH (array ) (sizeof(array) / sizeof((array)[0]))
19
19
#endif
20
20
21
- /* Forward declaration for module definition */
22
- #if PY_MAJOR_VERSION >= 3
23
- static struct PyModuleDef brotli_module ;
24
- #endif
25
-
26
- /* Module state for multi-phase initialization */
27
- typedef struct {
28
- PyObject * BrotliError ;
29
- } brotli_module_state ;
30
-
31
- #if PY_MAJOR_VERSION >= 3
32
- static brotli_module_state * get_brotli_state (PyObject * module ) {
33
- void * state = PyModule_GetState (module );
34
- assert (state != NULL );
35
- return (brotli_module_state * )state ;
36
- }
37
- #endif
38
-
39
- static void set_brotli_exception (const char * message ) {
40
- #if PY_MAJOR_VERSION >= 3
41
- PyObject * module = PyImport_ImportModule ("_brotli" );
42
- if (module == NULL ) {
43
- PyErr_SetString (PyExc_RuntimeError , "Module not found" );
44
- return ;
45
- }
46
- brotli_module_state * state = get_brotli_state (module );
47
- PyErr_SetString (state -> BrotliError , message );
48
- #else
49
- /* For Python 2, use static global */
50
- static PyObject * brotli_error = NULL ;
51
- if (brotli_error == NULL ) {
52
- PyObject * module_dict = PyImport_GetModuleDict ();
53
- PyObject * module = PyDict_GetItemString (module_dict , "_brotli" );
54
- if (module != NULL ) {
55
- brotli_error = PyObject_GetAttrString (module , "error" );
56
- }
57
- }
58
- if (brotli_error != NULL ) {
59
- PyErr_SetString (brotli_error , message );
60
- } else {
61
- PyErr_SetString (PyExc_RuntimeError , message );
62
- }
63
- #endif
64
- }
21
+ static PyObject * BrotliError ;
65
22
66
23
/* -----------------------------------
67
24
BlocksOutputBuffer code
@@ -275,19 +232,19 @@ static int as_bounded_int(PyObject* o, int* result, int lower_bound,
275
232
276
233
static int mode_convertor (PyObject * o , BrotliEncoderMode * mode ) {
277
234
if (!PyInt_Check (o )) {
278
- set_brotli_exception ( "Invalid mode" );
235
+ PyErr_SetString ( BrotliError , "Invalid mode" );
279
236
return 0 ;
280
237
}
281
238
282
239
int mode_value = -1 ;
283
240
if (!as_bounded_int (o , & mode_value , 0 , 255 )) {
284
- set_brotli_exception ( "Invalid mode" );
241
+ PyErr_SetString ( BrotliError , "Invalid mode" );
285
242
return 0 ;
286
243
}
287
244
* mode = (BrotliEncoderMode )mode_value ;
288
245
if (* mode != BROTLI_MODE_GENERIC && * mode != BROTLI_MODE_TEXT &&
289
246
* mode != BROTLI_MODE_FONT ) {
290
- set_brotli_exception ( "Invalid mode" );
247
+ PyErr_SetString ( BrotliError , "Invalid mode" );
291
248
return 0 ;
292
249
}
293
250
@@ -296,12 +253,12 @@ static int mode_convertor(PyObject* o, BrotliEncoderMode* mode) {
296
253
297
254
static int quality_convertor (PyObject * o , int * quality ) {
298
255
if (!PyInt_Check (o )) {
299
- set_brotli_exception ( "Invalid quality" );
256
+ PyErr_SetString ( BrotliError , "Invalid quality" );
300
257
return 0 ;
301
258
}
302
259
303
260
if (!as_bounded_int (o , quality , 0 , 11 )) {
304
- set_brotli_exception ( "Invalid quality. Range is 0 to 11." );
261
+ PyErr_SetString ( BrotliError , "Invalid quality. Range is 0 to 11." );
305
262
return 0 ;
306
263
}
307
264
@@ -310,12 +267,12 @@ static int quality_convertor(PyObject* o, int* quality) {
310
267
311
268
static int lgwin_convertor (PyObject * o , int * lgwin ) {
312
269
if (!PyInt_Check (o )) {
313
- set_brotli_exception ( "Invalid lgwin" );
270
+ PyErr_SetString ( BrotliError , "Invalid lgwin" );
314
271
return 0 ;
315
272
}
316
273
317
274
if (!as_bounded_int (o , lgwin , 10 , 24 )) {
318
- set_brotli_exception ( "Invalid lgwin. Range is 10 to 24." );
275
+ PyErr_SetString ( BrotliError , "Invalid lgwin. Range is 10 to 24." );
319
276
return 0 ;
320
277
}
321
278
@@ -324,12 +281,13 @@ static int lgwin_convertor(PyObject* o, int* lgwin) {
324
281
325
282
static int lgblock_convertor (PyObject * o , int * lgblock ) {
326
283
if (!PyInt_Check (o )) {
327
- set_brotli_exception ( "Invalid lgblock" );
284
+ PyErr_SetString ( BrotliError , "Invalid lgblock" );
328
285
return 0 ;
329
286
}
330
287
331
288
if (!as_bounded_int (o , lgblock , 0 , 24 ) || (* lgblock != 0 && * lgblock < 16 )) {
332
- set_brotli_exception ("Invalid lgblock. Can be 0 or in range 16 to 24." );
289
+ PyErr_SetString (BrotliError ,
290
+ "Invalid lgblock. Can be 0 or in range 16 to 24." );
333
291
return 0 ;
334
292
}
335
293
@@ -515,7 +473,8 @@ static PyObject* brotli_Compressor_process(brotli_Compressor* self,
515
473
}
516
474
517
475
error :
518
- set_brotli_exception (
476
+ PyErr_SetString (
477
+ BrotliError ,
519
478
"BrotliEncoderCompressStream failed while processing the stream" );
520
479
ret = NULL ;
521
480
@@ -553,7 +512,8 @@ static PyObject* brotli_Compressor_flush(brotli_Compressor* self) {
553
512
}
554
513
555
514
error :
556
- set_brotli_exception (
515
+ PyErr_SetString (
516
+ BrotliError ,
557
517
"BrotliEncoderCompressStream failed while flushing the stream" );
558
518
ret = NULL ;
559
519
finally :
@@ -596,7 +556,8 @@ static PyObject* brotli_Compressor_finish(brotli_Compressor* self) {
596
556
goto finally ;
597
557
598
558
error :
599
- set_brotli_exception (
559
+ PyErr_SetString (
560
+ BrotliError ,
600
561
"BrotliEncoderCompressStream failed while finishing the stream" );
601
562
ret = NULL ;
602
563
finally :
@@ -851,7 +812,8 @@ static PyObject* brotli_Decompressor_process(brotli_Decompressor* self,
851
812
852
813
if (self -> unconsumed_data_length > 0 ) {
853
814
if (input .len > 0 ) {
854
- set_brotli_exception (
815
+ PyErr_SetString (
816
+ BrotliError ,
855
817
"process called with data when accept_more_data is False" );
856
818
ret = NULL ;
857
819
goto finally ;
@@ -869,7 +831,8 @@ static PyObject* brotli_Decompressor_process(brotli_Decompressor* self,
869
831
}
870
832
871
833
error :
872
- set_brotli_exception (
834
+ PyErr_SetString (
835
+ BrotliError ,
873
836
"BrotliDecoderDecompressStream failed while processing the stream" );
874
837
ret = NULL ;
875
838
@@ -895,8 +858,8 @@ PyDoc_STRVAR(brotli_Decompressor_is_finished_doc,
895
858
896
859
static PyObject * brotli_Decompressor_is_finished (brotli_Decompressor * self ) {
897
860
if (!self -> dec ) {
898
- set_brotli_exception (
899
- "BrotliDecoderState is NULL while checking is_finished" );
861
+ PyErr_SetString ( BrotliError ,
862
+ "BrotliDecoderState is NULL while checking is_finished" );
900
863
return NULL ;
901
864
}
902
865
@@ -1075,7 +1038,7 @@ static PyObject* brotli_decompress(PyObject* self, PyObject* args,
1075
1038
1076
1039
error :
1077
1040
BlocksOutputBuffer_OnError (& buffer );
1078
- set_brotli_exception ( "BrotliDecompress failed" );
1041
+ PyErr_SetString ( BrotliError , "BrotliDecompress failed" );
1079
1042
ret = NULL ;
1080
1043
1081
1044
finally :
@@ -1091,34 +1054,47 @@ static PyMethodDef brotli_methods[] = {
1091
1054
1092
1055
PyDoc_STRVAR (brotli_doc , "Implementation module for the Brotli library." );
1093
1056
1094
- static int init_brotli_mod (PyObject * m ) {
1095
1057
#if PY_MAJOR_VERSION >= 3
1096
- brotli_module_state * state = get_brotli_state (m );
1097
- state -> BrotliError = PyErr_NewException ("brotli.error" , NULL , NULL );
1098
- if (state -> BrotliError == NULL ) {
1099
- return -1 ;
1100
- }
1101
- Py_INCREF (state -> BrotliError );
1102
- if (PyModule_AddObject (m , "error" , state -> BrotliError ) < 0 ) {
1103
- Py_DECREF (state -> BrotliError );
1104
- return -1 ;
1105
- }
1058
+ #define INIT_BROTLI PyInit__brotli
1059
+ #define CREATE_BROTLI PyModule_Create(&brotli_module)
1060
+ #define RETURN_BROTLI return m
1061
+ #define RETURN_NULL return NULL
1062
+
1063
+ static struct PyModuleDef brotli_module = {
1064
+ PyModuleDef_HEAD_INIT ,
1065
+ "_brotli" , /* m_name */
1066
+ brotli_doc , /* m_doc */
1067
+ 0 , /* m_size */
1068
+ brotli_methods , /* m_methods */
1069
+ NULL , /* m_reload */
1070
+ NULL , /* m_traverse */
1071
+ NULL , /* m_clear */
1072
+ NULL /* m_free */
1073
+ };
1106
1074
#else
1107
- PyObject * err = PyErr_NewException ("brotli.error" , NULL , NULL );
1108
- if (err != NULL ) {
1109
- Py_INCREF (err );
1110
- PyModule_AddObject (m , "error" , err );
1111
- }
1075
+ #define INIT_BROTLI init_brotli
1076
+ #define CREATE_BROTLI Py_InitModule3("_brotli", brotli_methods, brotli_doc)
1077
+ #define RETURN_BROTLI return
1078
+ #define RETURN_NULL return
1112
1079
#endif
1113
1080
1081
+ PyMODINIT_FUNC INIT_BROTLI (void ) {
1082
+ PyObject * m = CREATE_BROTLI ;
1083
+
1084
+ BrotliError = PyErr_NewException ((char * )"brotli.error" , NULL , NULL );
1085
+ if (BrotliError != NULL ) {
1086
+ Py_INCREF (BrotliError );
1087
+ PyModule_AddObject (m , "error" , BrotliError );
1088
+ }
1089
+
1114
1090
if (PyType_Ready (& brotli_CompressorType ) < 0 ) {
1115
- return -1 ;
1091
+ RETURN_NULL ;
1116
1092
}
1117
1093
Py_INCREF (& brotli_CompressorType );
1118
1094
PyModule_AddObject (m , "Compressor" , (PyObject * )& brotli_CompressorType );
1119
1095
1120
1096
if (PyType_Ready (& brotli_DecompressorType ) < 0 ) {
1121
- return -1 ;
1097
+ RETURN_NULL ;
1122
1098
}
1123
1099
Py_INCREF (& brotli_DecompressorType );
1124
1100
PyModule_AddObject (m , "Decompressor" , (PyObject * )& brotli_DecompressorType );
@@ -1133,49 +1109,5 @@ static int init_brotli_mod(PyObject* m) {
1133
1109
(decoderVersion >> 12 ) & 0xFFF , decoderVersion & 0xFFF );
1134
1110
PyModule_AddStringConstant (m , "__version__" , version );
1135
1111
1136
- return 0 ;
1137
- }
1138
-
1139
- #if PY_MAJOR_VERSION >= 3
1140
-
1141
- static PyModuleDef_Slot brotli_mod_slots [] = {
1142
- {Py_mod_exec , init_brotli_mod },
1143
- #if PY_MINOR_VERSION >= 12
1144
- {Py_mod_multiple_interpreters , Py_MOD_PER_INTERPRETER_GIL_SUPPORTED },
1145
- #endif
1146
- {0 , NULL }};
1147
-
1148
- static int brotli_traverse (PyObject * m , visitproc visit , void * arg ) {
1149
- brotli_module_state * state = get_brotli_state (m );
1150
- Py_VISIT (state -> BrotliError );
1151
- return 0 ;
1112
+ RETURN_BROTLI ;
1152
1113
}
1153
-
1154
- static int brotli_clear (PyObject * m ) {
1155
- brotli_module_state * state = get_brotli_state (m );
1156
- Py_CLEAR (state -> BrotliError );
1157
- return 0 ;
1158
- }
1159
-
1160
- static struct PyModuleDef brotli_module = {
1161
- PyModuleDef_HEAD_INIT ,
1162
- "_brotli" , /* m_name */
1163
- brotli_doc , /* m_doc */
1164
- sizeof (brotli_module_state ), /* m_size */
1165
- brotli_methods , /* m_methods */
1166
- brotli_mod_slots , /* m_slots */
1167
- brotli_traverse , /* m_traverse */
1168
- brotli_clear , /* m_clear */
1169
- NULL /* m_free */
1170
- };
1171
-
1172
- PyMODINIT_FUNC PyInit__brotli (void ) { return PyModuleDef_Init (& brotli_module ); }
1173
-
1174
- #else
1175
-
1176
- PyMODINIT_FUNC init_brotli (void ) {
1177
- PyObject * m = Py_InitModule3 ("_brotli" , brotli_methods , brotli_doc );
1178
- init_brotli_mod (m );
1179
- }
1180
-
1181
- #endif
0 commit comments