15
15
#error "The maximum block size accepted by libbzip2 is UINT32_MAX."
16
16
#endif
17
17
18
+ typedef struct {
19
+ PyTypeObject * bz2_compressor_type ;
20
+ PyTypeObject * bz2_decompressor_type ;
21
+ } _bz2_state ;
22
+
23
+ static inline _bz2_state *
24
+ get_module_state (PyObject * module )
25
+ {
26
+ void * state = PyModule_GetState (module );
27
+ assert (state != NULL );
28
+ return (_bz2_state * )state ;
29
+ }
30
+
31
+ static struct PyModuleDef _bz2module ;
32
+
33
+ static inline _bz2_state *
34
+ find_module_state_by_def (PyTypeObject * type )
35
+ {
36
+ PyObject * module = PyType_GetModuleByDef (type , & _bz2module );
37
+ assert (module != NULL );
38
+ return get_module_state (module );
39
+ }
40
+
18
41
/* On success, return value >= 0
19
42
On failure, return -1 */
20
43
static inline Py_ssize_t
@@ -81,19 +104,6 @@ OutputBuffer_OnError(_BlocksOutputBuffer *buffer)
81
104
#define RELEASE_LOCK (obj ) PyThread_release_lock((obj)->lock)
82
105
83
106
84
- typedef struct {
85
- PyTypeObject * bz2_compressor_type ;
86
- PyTypeObject * bz2_decompressor_type ;
87
- } _bz2_state ;
88
-
89
- static inline _bz2_state *
90
- get_bz2_state (PyObject * module )
91
- {
92
- void * state = PyModule_GetState (module );
93
- assert (state != NULL );
94
- return (_bz2_state * )state ;
95
- }
96
-
97
107
typedef struct {
98
108
PyObject_HEAD
99
109
bz_stream bzs ;
@@ -227,12 +237,14 @@ compress(BZ2Compressor *c, char *data, size_t len, int action)
227
237
228
238
/*[clinic input]
229
239
module _bz2
230
- class _bz2.BZ2Compressor "BZ2Compressor *" "&BZ2Compressor_Type "
231
- class _bz2.BZ2Decompressor "BZ2Decompressor *" "&BZ2Decompressor_Type "
240
+ class _bz2.BZ2Compressor "BZ2Compressor *" "clinic_state()->bz2_compressor_type "
241
+ class _bz2.BZ2Decompressor "BZ2Decompressor *" "clinic_state()->bz2_decompressor_type "
232
242
[clinic start generated code]*/
233
- /*[clinic end generated code: output=da39a3ee5e6b4b0d input=dc7d7992a79f9cb7 ]*/
243
+ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=92348121632b94c4 ]*/
234
244
245
+ #define clinic_state () (find_module_state_by_def(type))
235
246
#include "clinic/_bz2module.c.h"
247
+ #undef clinic_state
236
248
237
249
/*[clinic input]
238
250
_bz2.BZ2Compressor.compress
@@ -308,24 +320,43 @@ BZ2_Free(void* ctx, void *ptr)
308
320
PyMem_RawFree (ptr );
309
321
}
310
322
323
+ /*[clinic input]
324
+ @classmethod
325
+ _bz2.BZ2Compressor.__new__
311
326
312
- /* Argument Clinic is not used since the Argument Clinic always want to
313
- check the type which would be wrong here */
314
- static int
315
- _bz2_BZ2Compressor___init___impl (BZ2Compressor * self , int compresslevel )
327
+ compresslevel: int = 9
328
+ Compression level, as a number between 1 and 9.
329
+ /
330
+
331
+ Create a compressor object for compressing data incrementally.
332
+
333
+ For one-shot compression, use the compress() function instead.
334
+ [clinic start generated code]*/
335
+
336
+ static PyObject *
337
+ _bz2_BZ2Compressor_impl (PyTypeObject * type , int compresslevel )
338
+ /*[clinic end generated code: output=83346c96beaacad7 input=d4500d2a52c8b263]*/
316
339
{
317
340
int bzerror ;
341
+ BZ2Compressor * self ;
318
342
319
343
if (!(1 <= compresslevel && compresslevel <= 9 )) {
320
344
PyErr_SetString (PyExc_ValueError ,
321
345
"compresslevel must be between 1 and 9" );
322
- return -1 ;
346
+ return NULL ;
347
+ }
348
+
349
+ assert (type != NULL && type -> tp_alloc != NULL );
350
+ self = (BZ2Compressor * )type -> tp_alloc (type , 0 );
351
+ if (self == NULL ) {
352
+ return NULL ;
323
353
}
324
354
325
355
self -> lock = PyThread_allocate_lock ();
326
356
if (self -> lock == NULL ) {
357
+ Py_DECREF (self );
327
358
PyErr_SetString (PyExc_MemoryError , "Unable to allocate lock" );
328
- return -1 ;
359
+ return NULL ;
329
360
}
330
361
331
362
self -> bzs .opaque = NULL ;
@@ -335,49 +366,11 @@ _bz2_BZ2Compressor___init___impl(BZ2Compressor *self, int compresslevel)
335
366
if (catch_bz2_error (bzerror ))
336
367
goto error ;
337
368
338
- return 0 ;
369
+ return ( PyObject * ) self ;
339
370
340
371
error :
341
- PyThread_free_lock (self -> lock );
342
- self -> lock = NULL ;
343
- return -1 ;
344
- }
345
-
346
- PyDoc_STRVAR (_bz2_BZ2Compressor___init____doc__ ,
347
- "BZ2Compressor(compresslevel=9, /)\n"
348
- "--\n"
349
- "\n"
350
- "Create a compressor object for compressing data incrementally.\n"
351
- "\n"
352
- " compresslevel\n"
353
- " Compression level, as a number between 1 and 9.\n"
354
- "\n"
355
- "For one-shot compression, use the compress() function instead." );
356
-
357
- static int
358
- _bz2_BZ2Compressor___init__ (PyObject * self , PyObject * args , PyObject * kwargs )
359
- {
360
- int return_value = -1 ;
361
- int compresslevel = 9 ;
362
-
363
- if (!_PyArg_NoKeywords ("BZ2Compressor" , kwargs )) {
364
- goto exit ;
365
- }
366
- if (!_PyArg_CheckPositional ("BZ2Compressor" , PyTuple_GET_SIZE (args ), 0 , 1 )) {
367
- goto exit ;
368
- }
369
- if (PyTuple_GET_SIZE (args ) < 1 ) {
370
- goto skip_optional ;
371
- }
372
- compresslevel = _PyLong_AsInt (PyTuple_GET_ITEM (args , 0 ));
373
- if (compresslevel == -1 && PyErr_Occurred ()) {
374
- goto exit ;
375
- }
376
- skip_optional :
377
- return_value = _bz2_BZ2Compressor___init___impl ((BZ2Compressor * )self , compresslevel );
378
-
379
- exit :
380
- return return_value ;
372
+ Py_DECREF (self );
373
+ return NULL ;
381
374
}
382
375
383
376
static void
@@ -408,9 +401,8 @@ static PyMethodDef BZ2Compressor_methods[] = {
408
401
static PyType_Slot bz2_compressor_type_slots [] = {
409
402
{Py_tp_dealloc , BZ2Compressor_dealloc },
410
403
{Py_tp_methods , BZ2Compressor_methods },
411
- {Py_tp_init , _bz2_BZ2Compressor___init__ },
412
- {Py_tp_new , PyType_GenericNew },
413
- {Py_tp_doc , (char * )_bz2_BZ2Compressor___init____doc__ },
404
+ {Py_tp_new , _bz2_BZ2Compressor },
405
+ {Py_tp_doc , (char * )_bz2_BZ2Compressor__doc__ },
414
406
{Py_tp_traverse , BZ2Compressor_traverse },
415
407
{0 , 0 }
416
408
};
@@ -637,69 +629,54 @@ _bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data,
637
629
return result ;
638
630
}
639
631
640
- /* Argument Clinic is not used since the Argument Clinic always want to
641
- check the type which would be wrong here */
642
- static int
643
- _bz2_BZ2Decompressor___init___impl (BZ2Decompressor * self )
632
+ /*[clinic input]
633
+ @classmethod
634
+ _bz2.BZ2Decompressor.__new__
635
+
636
+ Create a decompressor object for decompressing data incrementally.
637
+
638
+ For one-shot decompression, use the decompress() function instead.
639
+ [clinic start generated code]*/
640
+
641
+ static PyObject *
642
+ _bz2_BZ2Decompressor_impl (PyTypeObject * type )
643
+ /*[clinic end generated code: output=5150d51ccaab220e input=b87413ce51853528]*/
644
644
{
645
+ BZ2Decompressor * self ;
645
646
int bzerror ;
646
647
647
- PyThread_type_lock lock = PyThread_allocate_lock ( );
648
- if ( lock == NULL ) {
649
- PyErr_SetString ( PyExc_MemoryError , "Unable to allocate lock" );
650
- return -1 ;
648
+ assert ( type != NULL && type -> tp_alloc != NULL );
649
+ self = ( BZ2Decompressor * ) type -> tp_alloc ( type , 0 );
650
+ if ( self == NULL ) {
651
+ return NULL ;
651
652
}
652
- if (self -> lock != NULL ) {
653
- PyThread_free_lock (self -> lock );
653
+
654
+ self -> lock = PyThread_allocate_lock ();
655
+ if (self -> lock == NULL ) {
656
+ Py_DECREF (self );
657
+ PyErr_SetString (PyExc_MemoryError , "Unable to allocate lock" );
658
+ return NULL ;
654
659
}
655
- self -> lock = lock ;
656
660
657
661
self -> needs_input = 1 ;
658
662
self -> bzs_avail_in_real = 0 ;
659
663
self -> input_buffer = NULL ;
660
664
self -> input_buffer_size = 0 ;
661
- Py_XSETREF ( self -> unused_data , PyBytes_FromStringAndSize (NULL , 0 ) );
665
+ self -> unused_data = PyBytes_FromStringAndSize (NULL , 0 );
662
666
if (self -> unused_data == NULL )
663
667
goto error ;
664
668
665
669
bzerror = BZ2_bzDecompressInit (& self -> bzs , 0 , 0 );
666
670
if (catch_bz2_error (bzerror ))
667
671
goto error ;
668
672
669
- return 0 ;
673
+ return ( PyObject * ) self ;
670
674
671
675
error :
672
- Py_CLEAR (self -> unused_data );
673
- PyThread_free_lock (self -> lock );
674
- self -> lock = NULL ;
675
- return -1 ;
676
- }
677
-
678
- static int
679
- _bz2_BZ2Decompressor___init__ (PyObject * self , PyObject * args , PyObject * kwargs )
680
- {
681
- int return_value = -1 ;
682
-
683
- if (!_PyArg_NoPositional ("BZ2Decompressor" , args )) {
684
- goto exit ;
685
- }
686
- if (!_PyArg_NoKeywords ("BZ2Decompressor" , kwargs )) {
687
- goto exit ;
688
- }
689
- return_value = _bz2_BZ2Decompressor___init___impl ((BZ2Decompressor * )self );
690
-
691
- exit :
692
- return return_value ;
676
+ Py_DECREF (self );
677
+ return NULL ;
693
678
}
694
679
695
- PyDoc_STRVAR (_bz2_BZ2Decompressor___init____doc__ ,
696
- "BZ2Decompressor()\n"
697
- "--\n"
698
- "\n"
699
- "Create a decompressor object for decompressing data incrementally.\n"
700
- "\n"
701
- "For one-shot decompression, use the decompress() function instead." );
702
-
703
680
static void
704
681
BZ2Decompressor_dealloc (BZ2Decompressor * self )
705
682
{
@@ -751,10 +728,9 @@ static PyMemberDef BZ2Decompressor_members[] = {
751
728
static PyType_Slot bz2_decompressor_type_slots [] = {
752
729
{Py_tp_dealloc , BZ2Decompressor_dealloc },
753
730
{Py_tp_methods , BZ2Decompressor_methods },
754
- {Py_tp_init , _bz2_BZ2Decompressor___init__ },
755
- {Py_tp_doc , (char * )_bz2_BZ2Decompressor___init____doc__ },
731
+ {Py_tp_doc , (char * )_bz2_BZ2Decompressor__doc__ },
756
732
{Py_tp_members , BZ2Decompressor_members },
757
- {Py_tp_new , PyType_GenericNew },
733
+ {Py_tp_new , _bz2_BZ2Decompressor },
758
734
{Py_tp_traverse , BZ2Decompressor_traverse },
759
735
{0 , 0 }
760
736
};
@@ -775,7 +751,7 @@ static PyType_Spec bz2_decompressor_type_spec = {
775
751
static int
776
752
_bz2_exec (PyObject * module )
777
753
{
778
- _bz2_state * state = get_bz2_state (module );
754
+ _bz2_state * state = get_module_state (module );
779
755
state -> bz2_compressor_type = (PyTypeObject * )PyType_FromModuleAndSpec (module ,
780
756
& bz2_compressor_type_spec , NULL );
781
757
if (state -> bz2_compressor_type == NULL ) {
@@ -800,7 +776,7 @@ _bz2_exec(PyObject *module)
800
776
static int
801
777
_bz2_traverse (PyObject * module , visitproc visit , void * arg )
802
778
{
803
- _bz2_state * state = get_bz2_state (module );
779
+ _bz2_state * state = get_module_state (module );
804
780
Py_VISIT (state -> bz2_compressor_type );
805
781
Py_VISIT (state -> bz2_decompressor_type );
806
782
return 0 ;
@@ -809,7 +785,7 @@ _bz2_traverse(PyObject *module, visitproc visit, void *arg)
809
785
static int
810
786
_bz2_clear (PyObject * module )
811
787
{
812
- _bz2_state * state = get_bz2_state (module );
788
+ _bz2_state * state = get_module_state (module );
813
789
Py_CLEAR (state -> bz2_compressor_type );
814
790
Py_CLEAR (state -> bz2_decompressor_type );
815
791
return 0 ;
@@ -818,22 +794,23 @@ _bz2_clear(PyObject *module)
818
794
static void
819
795
_bz2_free (void * module )
820
796
{
821
- _bz2_clear ((PyObject * )module );
797
+ ( void ) _bz2_clear ((PyObject * )module );
822
798
}
823
799
824
800
static struct PyModuleDef_Slot _bz2_slots [] = {
825
801
{Py_mod_exec , _bz2_exec },
802
+ {Py_mod_multiple_interpreters , Py_MOD_PER_INTERPRETER_GIL_SUPPORTED },
826
803
{0 , NULL }
827
804
};
828
805
829
806
static struct PyModuleDef _bz2module = {
830
- PyModuleDef_HEAD_INIT ,
807
+ . m_base = PyModuleDef_HEAD_INIT ,
831
808
.m_name = "_bz2" ,
832
809
.m_size = sizeof (_bz2_state ),
833
- .m_slots = _bz2_slots ,
834
810
.m_traverse = _bz2_traverse ,
835
811
.m_clear = _bz2_clear ,
836
812
.m_free = _bz2_free ,
813
+ .m_slots = _bz2_slots ,
837
814
};
838
815
839
816
PyMODINIT_FUNC
0 commit comments