@@ -85,19 +85,17 @@ get_posixsubprocess_state(PyObject *module)
85
85
return (_posixsubprocessstate * )state ;
86
86
}
87
87
88
- #define _posixsubprocessstate_global get_posixsubprocess_state(PyState_FindModule(&_posixsubprocessmodule))
89
-
90
88
/* If gc was disabled, call gc.enable(). Ignore errors. */
91
89
static void
92
- _enable_gc (int need_to_reenable_gc , PyObject * gc_module )
90
+ _enable_gc (int need_to_reenable_gc , PyObject * gc_module , _posixsubprocessstate * state )
93
91
{
94
92
PyObject * result ;
95
93
PyObject * exctype , * val , * tb ;
96
94
97
95
if (need_to_reenable_gc ) {
98
96
PyErr_Fetch (& exctype , & val , & tb );
99
97
result = PyObject_CallMethodNoArgs (
100
- gc_module , _posixsubprocessstate_global -> enable );
98
+ gc_module , state -> enable );
101
99
if (result == NULL ) {
102
100
/* We might have created a child process at this point, we
103
101
* we have no good way to handle a failure to reenable GC
@@ -758,7 +756,7 @@ do_fork_exec(char *const exec_array[],
758
756
759
757
760
758
static PyObject *
761
- subprocess_fork_exec (PyObject * self , PyObject * args )
759
+ subprocess_fork_exec (PyObject * module , PyObject * args )
762
760
{
763
761
PyObject * gc_module = NULL ;
764
762
PyObject * executable_list , * py_fds_to_keep ;
@@ -782,6 +780,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
782
780
Py_ssize_t arg_num , num_groups = 0 ;
783
781
int need_after_fork = 0 ;
784
782
int saved_errno = 0 ;
783
+ _posixsubprocessstate * state = get_posixsubprocess_state (module );
785
784
786
785
if (!PyArg_ParseTuple (
787
786
args , "OOpO!OOiiiiiiiiiiOOOiO:fork_exec" ,
@@ -827,7 +826,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
827
826
if (gc_module == NULL )
828
827
return NULL ;
829
828
result = PyObject_CallMethodNoArgs (
830
- gc_module , _posixsubprocessstate_global -> isenabled );
829
+ gc_module , state -> isenabled );
831
830
if (result == NULL ) {
832
831
Py_DECREF (gc_module );
833
832
return NULL ;
@@ -839,7 +838,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
839
838
return NULL ;
840
839
}
841
840
result = PyObject_CallMethodNoArgs (
842
- gc_module , _posixsubprocessstate_global -> disable );
841
+ gc_module , state -> disable );
843
842
if (result == NULL ) {
844
843
Py_DECREF (gc_module );
845
844
return NULL ;
@@ -1073,7 +1072,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
1073
1072
if (exec_array )
1074
1073
_Py_FreeCharPArray (exec_array );
1075
1074
1076
- _enable_gc (need_to_reenable_gc , gc_module );
1075
+ _enable_gc (need_to_reenable_gc , gc_module , state );
1077
1076
Py_XDECREF (gc_module );
1078
1077
1079
1078
return pid == -1 ? NULL : PyLong_FromPid (pid );
@@ -1113,12 +1112,38 @@ Raises: Only on an error in the parent process.\n\
1113
1112
PyDoc_STRVAR (module_doc ,
1114
1113
"A POSIX helper for the subprocess module." );
1115
1114
1115
+ static int
1116
+ _posixsubprocess_exec (PyObject * module )
1117
+ {
1118
+ _posixsubprocessstate * state = get_posixsubprocess_state (module );
1119
+
1120
+ state -> disable = PyUnicode_InternFromString ("disable" );
1121
+ if (state -> disable == NULL ) {
1122
+ return -1 ;
1123
+ }
1124
+
1125
+ state -> enable = PyUnicode_InternFromString ("enable" );
1126
+ if (state -> enable == NULL ) {
1127
+ return -1 ;
1128
+ }
1129
+
1130
+ state -> isenabled = PyUnicode_InternFromString ("isenabled" );
1131
+ if (state -> isenabled == NULL ) {
1132
+ return -1 ;
1133
+ }
1134
+
1135
+ return 0 ;
1136
+ }
1116
1137
1117
1138
static PyMethodDef module_methods [] = {
1118
1139
{"fork_exec" , subprocess_fork_exec , METH_VARARGS , subprocess_fork_exec_doc },
1119
1140
{NULL , NULL } /* sentinel */
1120
1141
};
1121
1142
1143
+ static PyModuleDef_Slot _posixsubprocess_slots [] = {
1144
+ {Py_mod_exec , _posixsubprocess_exec },
1145
+ {0 , NULL }
1146
+ };
1122
1147
1123
1148
static int _posixsubprocess_traverse (PyObject * m , visitproc visit , void * arg ) {
1124
1149
Py_VISIT (get_posixsubprocess_state (m )-> disable );
@@ -1140,36 +1165,18 @@ static void _posixsubprocess_free(void *m) {
1140
1165
1141
1166
static struct PyModuleDef _posixsubprocessmodule = {
1142
1167
PyModuleDef_HEAD_INIT ,
1143
- "_posixsubprocess" ,
1144
- module_doc ,
1145
- sizeof (_posixsubprocessstate ),
1146
- module_methods ,
1147
- NULL ,
1148
- _posixsubprocess_traverse ,
1149
- _posixsubprocess_clear ,
1150
- _posixsubprocess_free ,
1168
+ . m_name = "_posixsubprocess" ,
1169
+ . m_doc = module_doc ,
1170
+ . m_size = sizeof (_posixsubprocessstate ),
1171
+ . m_methods = module_methods ,
1172
+ . m_slots = _posixsubprocess_slots ,
1173
+ . m_traverse = _posixsubprocess_traverse ,
1174
+ . m_clear = _posixsubprocess_clear ,
1175
+ . m_free = _posixsubprocess_free ,
1151
1176
};
1152
1177
1153
1178
PyMODINIT_FUNC
1154
1179
PyInit__posixsubprocess (void )
1155
1180
{
1156
- PyObject * m ;
1157
-
1158
- m = PyState_FindModule (& _posixsubprocessmodule );
1159
- if (m != NULL ) {
1160
- Py_INCREF (m );
1161
- return m ;
1162
- }
1163
-
1164
- m = PyModule_Create (& _posixsubprocessmodule );
1165
- if (m == NULL ) {
1166
- return NULL ;
1167
- }
1168
-
1169
- get_posixsubprocess_state (m )-> disable = PyUnicode_InternFromString ("disable" );
1170
- get_posixsubprocess_state (m )-> enable = PyUnicode_InternFromString ("enable" );
1171
- get_posixsubprocess_state (m )-> isenabled = PyUnicode_InternFromString ("isenabled" );
1172
-
1173
- PyState_AddModule (m , & _posixsubprocessmodule );
1174
- return m ;
1181
+ return PyModuleDef_Init (& _posixsubprocessmodule );
1175
1182
}
0 commit comments