@@ -51,8 +51,6 @@ get_termios_state(PyObject *module)
51
51
return (termiosmodulestate * )state ;
52
52
}
53
53
54
- #define modulestate_global get_termios_state(PyState_FindModule(&termiosmodule))
55
-
56
54
static int fdconv (PyObject * obj , void * p )
57
55
{
58
56
int fd ;
@@ -82,28 +80,29 @@ static PyObject *
82
80
termios_tcgetattr (PyObject * self , PyObject * args )
83
81
{
84
82
int fd ;
85
- struct termios mode ;
86
- PyObject * cc ;
87
- speed_t ispeed , ospeed ;
88
- PyObject * v ;
89
- int i ;
90
- char ch ;
91
-
92
83
if (!PyArg_ParseTuple (args , "O&:tcgetattr" ,
93
- fdconv , (void * )& fd ))
84
+ fdconv , (void * )& fd )) {
94
85
return NULL ;
86
+ }
95
87
96
- if (tcgetattr (fd , & mode ) == -1 )
97
- return PyErr_
8000
SetFromErrno (modulestate_global -> TermiosError );
88
+ termiosmodulestate * state = PyModule_GetState (self );
89
+ struct termios mode ;
90
+ if (tcgetattr (fd , & mode ) == -1 ) {
91
+ return PyErr_SetFromErrno (state -> TermiosError );
92
+ }
98
93
99
- ispeed = cfgetispeed (& mode );
100
- ospeed = cfgetospeed (& mode );
94
+ speed_t ispeed = cfgetispeed (& mode );
95
+ speed_t ospeed = cfgetospeed (& mode );
101
96
102
- cc = PyList_New (NCCS );
103
- if (cc == NULL )
97
+ PyObject * cc = PyList_New (NCCS );
98
+ if (cc == NULL ) {
104
99
return NULL ;
100
+ }
101
+
102
+ PyObject * v ;
103
+ int i ;
105
104
for (i = 0 ; i < NCCS ; i ++ ) {
106
- ch = (char )mode .c_cc [i ];
105
+ char ch = (char )mode .c_cc [i ];
107
106
v = PyBytes_FromStringAndSize (& ch , 1 );
108
107
if (v == NULL )
109
108
goto err ;
@@ -159,33 +158,35 @@ static PyObject *
159
158
termios_tcsetattr (PyObject * self , PyObject * args )
160
159
{
161
160
int fd , when ;
162
- struct termios mode ;
163
- speed_t ispeed , ospeed ;
164
- PyObject * term , * cc , * v ;
165
- int i ;
166
-
161
+ PyObject * term ;
167
162
if (!PyArg_ParseTuple (args , "O&iO:tcsetattr" ,
168
- fdconv , & fd , & when , & term ))
163
+ fdconv , & fd , & when , & term )) {
169
164
return NULL ;
165
+ }
166
+
170
167
if (!PyList_Check (term ) || PyList_Size (term ) != 7 ) {
171
168
PyErr_SetString (PyExc_TypeError ,
172
169
"tcsetattr, arg 3: must be 7 element list" );
173
170
return NULL ;
174
171
}
175
172
176
173
/* Get the old mode, in case there are any hidden fields... */
177
- termiosmodulestate * state = modulestate_global ;
178
- if (tcgetattr (fd , & mode ) == -1 )
174
+ termiosmodulestate * state = PyModule_GetState (self );
175
+ struct termios mode ;
176
+ if (tcgetattr (fd , & mode ) == -1 ) {
179
177
return PyErr_SetFromErrno (state -> TermiosError );
178
+ }
179
+
180
180
mode .c_iflag = (tcflag_t ) PyLong_AsLong (PyList_GetItem (term , 0 ));
181
181
mode .c_oflag = (tcflag_t ) PyLong_AsLong (PyList_GetItem (term , 1 ));
182
182
mode .c_cflag = (tcflag_t ) PyLong_AsLong (PyList_GetItem (term , 2 ));
183
183
mode .c_lflag = (tcflag_t ) PyLong_AsLong (PyList_GetItem (term , 3 ));
184
- ispeed = (speed_t ) PyLong_AsLong (PyList_GetItem (term , 4 ));
185
- ospeed = (speed_t ) PyLong_AsLong (PyList_GetItem (term , 5 ));
186
- cc = PyList_GetItem (term , 6 );
187
- if (PyErr_Occurred ())
184
+ speed_t ispeed = (speed_t ) PyLong_AsLong (PyList_GetItem (term , 4 ));
185
+ speed_t ospeed = (speed_t ) PyLong_AsLong (PyList_GetItem (term , 5 ));
186
+ PyObject * cc = PyList_GetItem (term , 6 );
187
+ if (PyErr_Occurred ()) {
188
188
return NULL ;
189
+ }
189
190
190
191
if (!PyList_Check (cc ) || PyList_Size (cc ) != NCCS ) {
191
192
PyErr_Format (PyExc_TypeError ,
@@ -194,6 +195,8 @@ termios_tcsetattr(PyObject *self, PyObject *args)
194
195
return NULL ;
195
196
}
196
197
198
+ int i ;
199
+ PyObject * v ;
197
200
for (i = 0 ; i < NCCS ; i ++ ) {
198
201
v = PyList_GetItem (cc , i );
199
202
@@ -229,12 +232,15 @@ static PyObject *
229
232
termios_tcsendbreak (PyObject * self , PyObject * args )
230
233
{
231
234
int fd , duration ;
232
-
233
235
if (!PyArg_ParseTuple (args , "O&i:tcsendbreak" ,
234
- fdconv , & fd , & duration ))
236
+ fdconv , & fd , & duration )) {
235
237
return NULL ;
236
- if (tcsendbreak (fd , duration ) == -1 )
237
- return PyErr_SetFromErrno (modulestate_global -> TermiosError );
238
+ }
239
+
240
+ termiosmodulestate * state = PyModule_GetState (self );
241
+ if (tcsendbreak (fd , duration ) == -1 ) {
242
+ return PyErr_SetFromErrno (state -> TermiosError );
243
+ }
238
244
239
245
Py_RETURN_NONE ;
240
246
}
@@ -248,12 +254,15 @@ static PyObject *
248
254
termios_tcdrain (PyObject * self , PyObject * args )
249
255
{
250
256
int fd ;
251
-
252
257
if (!PyArg_ParseTuple (args , "O&:tcdrain" ,
253
- fdconv , & fd ))
258
+ fdconv , & fd )) {
254
259
return NULL ;
255
- if (tcdrain (fd ) == -1 )
256
- return PyErr_SetFromErrno (modulestate_global -> TermiosError );
260
+ }
261
+
262
+ termiosmodulestate * state = PyModule_GetState (self );
263
+ if (tcdrain (fd ) == -1 ) {
264
+ return PyErr_SetFromErrno (state -> TermiosError );
265
+ }
257
266
258
267
Py_RETURN_NONE ;
259
268
}
@@ -270,12 +279,15 @@ static PyObject *
270
279
termios_tcflush (PyObject * self , PyObject * args )
271
280
{
272
281
int fd , queue ;
273
-
274
282
if (!PyArg_ParseTuple (args , "O&i:tcflush" ,
275
- fdconv , & fd , & queue ))
283
+ fdconv , & fd , & queue )) {
276
284
return NULL ;
277
- if (tcflush (fd , queue ) == -1 )
278
- return PyErr_SetFromErrno (modulestate_global -> TermiosError );
285
+ }
286
+
287
+ termiosmodulestate * state = PyModule_GetState (self );
288
+ if (tcflush (fd , queue ) == -1 ) {
289
+ return PyErr_SetFromErrno (state -> TermiosError );
290
+ }
279
291
280
292
Py_RETURN_NONE ;
281
293
}
@@ -292,12 +304,15 @@ static PyObject *
292
304
termios_tcflow (PyObject * self , PyObject * args )
293
305
{
294
306
int fd , action ;
295
-
296
307
if (!PyArg_ParseTuple (args , "O&i:tcflow" ,
297
- fdconv , & fd , & action ))
308
+ fdconv , & fd , & action )) {
298
309
return NULL ;
299
- if (tcflow (fd , action ) == -1 )
300
- return PyErr_SetFromErrno (modulestate_global -> TermiosError );
310
+ }
311
+
312
+ termiosmodulestate * state = PyModule_GetState (self );
313
+ if (tcflow (fd , action ) == -1 ) {
314
+ return PyErr_SetFromErrno (state -> TermiosError );
315
+ }
301
316
302
317
Py_RETURN_NONE ;
303
318
}
@@ -997,44 +1012,43 @@ static void termiosmodule_free(void *m) {
997
1012
termiosmodule_clear ((PyObject * )m );
998
1013
}
999
1014
1000
- static struct PyModuleDef termiosmodule = {
1001
- PyModuleDef_HEAD_INIT ,
1002
- "termios" ,
1003
- termios__doc__ ,
1004
- sizeof (termiosmodulestate ),
1005
- termios_methods ,
1006
- NULL ,
1007
- termiosmodule_traverse ,
1008
- termiosmodule_clear ,
1009
- termiosmodule_free ,
1010
- };
1011
-
1012
- PyMODINIT_FUNC
1013
- PyInit_termios (void )
1015
+ static int
1016
+ termios_exec (PyObject * mod )
1014
1017
{
1015
- PyObject * m ;
1016
1018
struct constant * constant = termios_constants ;
1017
-
1018
- if ((m = PyState_FindModule (& termiosmodule )) != NULL ) {
1019
- Py_INCREF (m );
1020
- return m ;
1021
- }
1022
-
1023
- if ((m = PyModule_Create (& termiosmodule )) == NULL ) {
1024
- return NULL ;
1025
- }
1026
-
1027
- termiosmodulestate * state = get_termios_state (m );
1019
+ termiosmodulestate * state = get_termios_state (mod );
1028
1020
state -> TermiosError = PyErr_NewException ("termios.error" , NULL , NULL );
1029
1021
if (state -> TermiosError == NULL ) {
1030
- return NULL ;
1022
+ return -1 ;
1031
1023
}
1032
1024
Py_INCREF (state -> TermiosError );
1033
- PyModule_AddObject (m , "error" , state -> TermiosError );
1025
+ PyModule_AddObject (mod , "error" , state -> TermiosError );
1034
1026
1035
1027
while (constant -> name != NULL ) {
1036
- PyModule_AddIntConstant (m , constant -> name , constant -> value );
1028
+ PyModule_AddIntConstant (mod , constant -> name , constant -> value );
1037
1029
++ constant ;
1038
1030
}
1039
- return m ;
1031
+ return 0 ;
1032
+ }
1033
+
1034
+ static PyModuleDef_Slot termios_slots [] = {
1035
+ {Py_mod_exec , termios_exec },
1036
+ {0 , NULL }
1037
+ };
1038
+
1039
+ static struct PyModuleDef termiosmodule = {
1040
+ PyModuleDef_HEAD_INIT ,
1041
+ .m_name = "termios" ,
1042
+ .m_doc = termios__doc__ ,
1043
+ .m_size = sizeof (termiosmodulestate ),
1044
+ .m_methods = termios_methods ,
1045
+ .m_slots = termios_slots ,
1046
+ .m_traverse = termiosmodule_traverse ,
1047
+ .m_clear = termiosmodule_clear ,
1048
+ .m_free = termiosmodule_free ,
1049
+ };
1050
+
1051
+ PyMODINIT_FUNC PyInit_termios (void )
1052
+ {
1053
+ return PyModuleDef_Init (& termiosmodule );
1040
1054
}
0 commit comments