@@ -561,25 +561,9 @@ PyNumber_AsOff_t(PyObject *item, PyObject *err)
561
561
return result ;
562
562
}
563
563
564
- _PyIO_State *
565
- _PyIO_get_module_state (void )
566
- {
567
- PyObject * mod = PyState_FindModule (& _PyIO_Module );
568
- _PyIO_State * state ;
569
- if (mod == NULL || (state = get_io_state (mod )) == NULL ) {
570
- PyErr_SetString (PyExc_RuntimeError ,
571
- "could not find io module state "
572
- "(interpreter shutdown?)" );
573
- return NULL ;
574
- }
575
- return state ;
576
- }
577
-
578
564
static int
579
565
iomodule_traverse (PyObject * mod , visitproc visit , void * arg ) {
580
566
_PyIO_State * state = get_io_state (mod );
581
- if (!state -> initialized )
582
- return 0 ;
583
567
Py_VISIT (state -> unsupported_operation );
584
568
585
569
Py_VISIT (state -> PyIOBase_Type );
@@ -606,8 +590,6 @@ iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
606
590
static int
607
591
iomodule_clear (PyObject * mod ) {
608
592
_PyIO_State * state = get_io_state (mod );
609
- if (!state -> initialized )
610
- return 0 ;
611
593
Py_CLEAR (state -> unsupported_operation );
612
594
613
595
Py_CLEAR (state -> PyIOBase_Type );
@@ -652,115 +634,57 @@ static PyMethodDef module_methods[] = {
652
634
{NULL , NULL }
653
635
};
654
636
655
- struct PyModuleDef _PyIO_Module = {
656
- PyModuleDef_HEAD_INIT ,
657
- "io" ,
658
- module_doc ,
659
- sizeof (_PyIO_State ),
660
- module_methods ,
661
- NULL ,
662
- iomodule_traverse ,
663
- iomodule_clear ,
664
- (freefunc )iomodule_free ,
665
- };
666
-
667
-
668
- static PyTypeObject * static_types [] = {
669
- // Base classes
670
- & PyIOBase_Type ,
671
-
672
- // PyIOBase_Type subclasses
673
- & PyBufferedIOBase_Type ,
674
- & PyRawIOBase_Type ,
675
- & PyTextIOBase_Type ,
676
- };
677
-
678
-
679
- PyStatus
680
- _PyIO_InitTypes (PyInterpreterState * interp )
681
- {
682
- for (size_t i = 0 ; i < Py_ARRAY_LENGTH (static_types ); i ++ ) {
683
- PyTypeObject * type = static_types [i ];
684
- if (_PyStaticType_InitBuiltin (interp , type ) < 0 ) {
685
- return _PyStatus_ERR ("Can't initialize builtin type" );
686
- }
687
- }
688
-
689
- return _PyStatus_OK ();
690
- }
691
-
692
- void
693
- _PyIO_FiniTypes (PyInterpreterState * interp )
694
- {
695
- // Deallocate types in the reverse order to deallocate subclasses before
696
- // their base classes.
697
- for (Py_ssize_t i = Py_ARRAY_LENGTH (static_types ) - 1 ; i >= 0 ; i -- ) {
698
- PyTypeObject * type = static_types [i ];
699
- _PyStaticType_Dealloc (interp , type );
700
- }
701
- }
702
-
703
637
#define ADD_TYPE (module , type , spec , base ) \
704
638
do { \
705
639
type = (PyTypeObject *)PyType_FromModuleAndSpec(module, spec, \
706
640
(PyObject *)base); \
707
641
if (type == NULL) { \
708
- goto fail ; \
642
+ return -1 ; \
709
643
} \
710
644
if (PyModule_AddType(module, type) < 0) { \
711
- goto fail ; \
645
+ return -1 ; \
712
646
} \
713
647
} while (0)
714
648
715
- PyMODINIT_FUNC
716
- PyInit__io ( void )
649
+ static int
650
+ iomodule_exec ( PyObject * m )
717
651
{
718
- PyObject * m = PyModule_Create (& _PyIO_Module );
719
- _PyIO_State * state = NULL ;
720
- if (m == NULL )
721
- return NULL ;
722
- state = get_io_state (m );
723
- state -> initialized = 0 ;
652
+ _PyIO_State * state = get_io_state (m );
724
653
725
654
/* DEFAULT_BUFFER_SIZE */
726
655
if (PyModule_AddIntMacro (m , DEFAULT_BUFFER_SIZE ) < 0 )
727
- goto fail ;
656
+ return -1 ;
728
657
729
658
/* UnsupportedOperation inherits from ValueError and OSError */
730
659
state -> unsupported_operation = PyObject_CallFunction (
731
660
(PyObject * )& PyType_Type , "s(OO){}" ,
732
661
"UnsupportedOperation" , PyExc_OSError , PyExc_ValueError );
733
662
if (state -> unsupported_operation == NULL )
734
- goto fail ;
663
+ return -1 ;
735
664
if (PyModule_AddObjectRef (m , "UnsupportedOperation" ,
736
665
state -> unsupported_operation ) < 0 )
737
666
{
738
- goto fail ;
667
+ return -1 ;
739
668
}
740
669
741
670
/* BlockingIOError, for compatibility */
742
671
if (PyModule_AddObjectRef (m , "BlockingIOError" ,
743
672
(PyObject * ) PyExc_BlockingIOError ) < 0 ) {
744
- goto fail ;
745
- }
746
-
747
- // Add types
748
- for (size_t i = 0 ; i < Py_ARRAY_LENGTH (static_types ); i ++ ) {
749
- PyTypeObject * type = static_types [i ];
750
- if (PyModule_AddType (m , type ) < 0 ) {
751
- goto fail ;
752
- }
673
+ return -1 ;
753
674
}
754
675
755
676
// Base classes
756
- state -> PyIOBase_Type = (PyTypeObject * )Py_NewRef (& PyIOBase_Type );
757
677
ADD_TYPE (m , state -> PyIncrementalNewlineDecoder_Type , & nldecoder_spec , NULL );
758
678
ADD_TYPE (m , state -> PyBytesIOBuffer_Type , & bytesiobuf_spec , NULL );
679
+ ADD_TYPE (m , state -> PyIOBase_Type , & iobase_spec , NULL );
759
680
760
681
// PyIOBase_Type subclasses
761
- state -> PyRawIOBase_Type = (PyTypeObject * )Py_NewRef (& PyRawIOBase_Type );
762
- state -> PyBufferedIOBase_Type = (PyTypeObject * )Py_NewRef (& PyBufferedIOBase_Type );
763
- state -> PyTextIOBase_Type = (PyTypeObject * )Py_NewRef (& PyTextIOBase_Type );
682
+ ADD_TYPE (m , state -> PyTextIOBase_Type , & textiobase_spec ,
683
+ state -> PyIOBase_Type );
684
+ ADD_TYPE (m , state -> PyBufferedIOBase_Type , & bufferediobase_spec ,
685
+ state -> PyIOBase_Type );
686
+ ADD_TYPE (m , state -> PyRawIOBase_Type , & rawiobase_spec ,
687
+ state -> PyIOBase_Type );
764
688
765
689
// PyBufferedIOBase_Type(PyIOBase_Type) subclasses
766
690
ADD_TYPE (m , state -> PyBytesIO_Type , & bytesio_spec , state -> PyBufferedIOBase_Type );
@@ -775,6 +699,7 @@ PyInit__io(void)
775
699
776
700
// PyRawIOBase_Type(PyIOBase_Type) subclasses
777
701
ADD_TYPE (m , state -> PyFileIO_Type , & fileio_spec , state -> PyRawIOBase_Type );
702
+
778
703
#ifdef HAVE_WINDOWS_CONSOLE_IO
779
704
ADD_TYPE (m , state -> PyWindowsConsoleIO_Type , & winconsoleio_spec ,
780
705
state -> PyRawIOBase_Type );
@@ -785,11 +710,30 @@ PyInit__io(void)
785
710
ADD_TYPE (m , state -> PyTextIOWrapper_Type , & textiowrapper_spec ,
786
711
state -> PyTextIOBase_Type );
787
712
788
- state -> initialized = 1 ;
713
+ #undef ADD_TYPE
714
+ return 0 ;
715
+ }
789
716
790
- return m ;
717
+ static struct PyModuleDef_Slot iomodule_slots [] = {
718
+ {Py_mod_exec , iomodule_exec },
719
+ {Py_mod_multiple_interpreters , Py_MOD_PER_INTERPRETER_GIL_SUPPORTED },
720
+ {0 , NULL },
721
+ };
791
722
792
- fail :
793
- Py_DECREF (m );
794
- return NULL ;
723
+ struct PyModuleDef _PyIO_Module = {
724
+ .m_base = PyModuleDef_HEAD_INIT ,
725
+ .m_name = "io" ,
726
+ .m_doc = module_doc ,
727
+ .m_size = sizeof (_PyIO_State ),
728
+ .m_methods = module_methods ,
729
+ .m_traverse = iomodule_traverse ,
730
+ .m_clear = iomodule_clear ,
731
+ .m_free = iomodule_free ,
732
+ .m_slots = iomodule_slots ,
733
+ };
734
+
735
+ PyMODINIT_FUNC
736
+ PyInit__io (void )
737
+ {
738
+ return PyModuleDef_Init (& _PyIO_Module );
795
739
}
0 commit comments