49
49
50
50
#define POSIX_CALL (call ) do { if ((call) == -1) goto error; } while (0)
51
51
52
+ typedef struct {
53
+ PyObject * disable ;
54
+ PyObject * enable ;
55
+ PyObject * isenabled ;
56
+ } _posixsubprocessstate ;
57
+
58
+ static struct PyModuleDef _posixsubprocessmodule ;
59
+
60
+ #define _posixsubprocessstate (o ) ((_posixsubprocessstate *)PyModule_GetState(o))
61
+ #define _posixsubprocessstate_global _posixsubprocessstate(PyState_FindModule(&_posixsubprocessmodule))
52
62
53
63
/* If gc was disabled, call gc.enable(). Return 0 on success. */
54
64
static int
55
65
_enable_gc (int need_to_reenable_gc , PyObject * gc_module )
56
66
{
57
67
PyObject * result ;
58
- _Py_IDENTIFIER (enable );
59
68
PyObject * exctype , * val , * tb ;
60
69
61
70
if (need_to_reenable_gc ) {
62
71
PyErr_Fetch (& exctype , & val , & tb );
63
- result = _PyObject_CallMethodIdNoArgs (gc_module , & PyId_enable );
72
+ result = _PyObject_CallMethodNoArgs (
73
+ gc_module , _posixsubprocessstate_global -> enable );
64
74
if (exctype != NULL ) {
65
75
PyErr_Restore (exctype , val , tb );
66
76
}
@@ -602,13 +612,12 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
602
612
/* We need to call gc.disable() when we'll be calling preexec_fn */
603
613
if (preexec_fn != Py_None ) {
604
614
PyObject * result ;
605
- _Py_IDENTIFIER (isenabled );
606
- _Py_IDENTIFIER (disable );
607
615
608
616
gc_module = PyImport_ImportModule ("gc" );
609
617
if (gc_module == NULL )
610
618
return NULL ;
611
- result = _PyObject_CallMethodIdNoArgs (gc_module , & PyId_isenabled );
619
+ result = _PyObject_CallMethodNoArgs (
620
+ gc_module , _posixsubprocessstate_global -> isenabled );
612
621
if (result == NULL ) {
613
622
Py_DECREF (gc_module );
614
623
return NULL ;
@@ -619,7 +628,8 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
619
628
Py_DECREF (gc_module );
620
629
return NULL ;
621
630
}
622
- result = _PyObject_CallMethodIdNoArgs (gc_module , & PyId_disable );
631
+ result = _PyObject_CallMethodNoArgs (
632
+ gc_module , _posixsubprocessstate_global -> disable );
623
633
if (result == NULL ) {
624
634
Py_DECREF (gc_module );
625
635
return NULL ;
@@ -798,16 +808,56 @@ static PyMethodDef module_methods[] = {
798
808
};
799
809
800
810
811
+ static int _posixsubprocess_traverse (PyObject * m , visitproc visit , void * arg ) {
812
+ Py_VISIT (_posixsubprocessstate (m )-> disable );
813
+ Py_VISIT (_posixsubprocessstate (m )-> enable );
814
+ Py_VISIT (_posixsubprocessstate (m )-> isenabled );
815
+ return 0 ;
816
+ }
817
+
818
+ static int _posixsubprocess_clear (PyObject * m ) {
819
+ Py_CLEAR (_posixsubprocessstate (m )-> disable );
820
+ Py_CLEAR (_posixsubprocessstate (m )-> enable );
821
+ Py_CLEAR (_posixsubprocessstate (m )-> isenabled );
822
+ return 0 ;
823
+ }
824
+
825
+ static void _posixsubprocess_free (void * m ) {
826
+ _posixsubprocess_clear ((PyObject * )m );
827
+ }
828
+
801
829
static struct PyModuleDef _posixsubprocessmodule = {
802
830
PyModuleDef_HEAD_INIT ,
803
831
"_posixsubprocess" ,
804
832
module_doc ,
805
- -1 , /* No memory is needed. */
833
+ sizeof ( _posixsubprocessstate ),
806
834
module_methods ,
835
+ NULL ,
836
+ _posixsubprocess_traverse ,
837
+ _posixsubprocess_clear ,
838
+ _posixsubprocess_free ,
807
839
};
808
840
809
841
PyMODINIT_FUNC
810
842
PyInit__posixsubprocess (void )
811
843
{
812
- return PyModule_Create (& _posixsubprocessmodule );
844
+ PyObject * m ;
845
+
846
+ m = PyState_FindModule (& _posixsubprocessmodule );
847
+ if (m != NULL ) {
848
+ Py_INCREF (m );
849
+ return m ;
850
+ }
851
+
852
+ m = PyModule_Create (& _posixsubprocessmodule );
853
+ if (m == NULL ) {
854
+ return NULL ;
855
+ }
856
+
857
+ _posixsubprocessstate (m )-> disable = PyUnicode_InternFromString ("disable" );
858
+ _posixsubprocessstate (m )-> enable = PyUnicode_InternFromString ("enable" );
859
+ _posixsubprocessstate (m )-> isenabled = PyUnicode_InternFromString ("isenabled" );
860
+
861
+ PyState_AddModule (m , & _posixsubprocessmodule );
862
+ return m ;
813
863
}
0 commit comments