@@ -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 ;
@@ -79,31 +77,32 @@ indexing in the cc array must be done using the symbolic constants defined\n\
79
77
in this module." );
80
78
81
79
static PyObject *
82
- termios_tcgetattr (PyObject * self , PyObject * args )
80
+ termios_tcgetattr (PyObject * module , 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_SetFromErrno (modulestate_global -> TermiosError );
88
+ termiosmodulestate * state = PyModule_GetState (module );
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 ;
@@ -156,36 +155,38 @@ queued output, or termios.TCSAFLUSH to change after transmitting all\n\
156
155
queued output and discarding all queued input. " );
157
156
158
157
static PyObject *
159
- termios_tcsetattr (PyObject * self , PyObject * args )
158
+ termios_tcsetattr (PyObject * module , 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 (module );
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
@@ -226,15 +229,18 @@ A zero duration sends a break for 0.25-0.5 seconds; a nonzero duration\n\
226
229
has a system dependent meaning." );
227
230
228
231
static PyObject *
229
- termios_tcsendbreak (PyObject * self , PyObject * args )
232
+ termios_tcsendbreak (PyObject * module , 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 (module );
241
+ if (tcsendbreak (fd , duration ) == -1 ) {
242
+ return PyErr_SetFromErrno (state -> TermiosError );
243
+ }
238
244
239
245
Py_RETURN_NONE ;
240
246
}
@@ -245,15 +251,18 @@ PyDoc_STRVAR(termios_tcdrain__doc__,
245
251
Wait until all output written to file descriptor fd has been transmitted." );
246
252
247
253
static PyObject *
248
- termios_tcdrain (PyObject * self , PyObject * args )
254
+ termios_tcdrain (PyObject * module , 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 (module );
263
+ if (tcdrain (fd ) == -1 ) {
264
+ return PyErr_SetFromErrno (state -> TermiosError );
265
+ }
257
266
258
267
Py_RETURN_NONE ;
259
268
}
@@ -267,15 +276,18 @@ queue, termios.TCOFLUSH for the output queue, or termios.TCIOFLUSH for\n\
267
276
both queues. " );
268
277
269
278
static PyObject *
270
- termios_tcflush (PyObject * self , PyObject * args )
279
+ termios_tcflush (PyObject * module , 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 (module );
288
+ if (tcflush (fd , queue ) == -1 ) {
289
+ return PyErr_SetFromErrno (state -> TermiosError );
290
+ }
279
291
280
292
Py_RETURN_NONE ;
281
293
}
@@ -289,15 +301,18 @@ termios.TCOON to restart output, termios.TCIOFF to suspend input,\n\
289
301
or termios.TCION to restart input." );
290
302
291
303
static PyObject *
292
- termios_tcflow (PyObject * self , PyObject * args )
304
+ termios_tcflow (PyObject * module , 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 (module );
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,49 @@ 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
+ if (PyModule_AddObject (mod , "error" , state -> TermiosError ) < 0 ) {
1026
+ Py_DECREF (state -> TermiosError );
1027
+ return -1 ;
1028
+ }
1034
1029
1035
1030
while (constant -> name != NULL ) {
1036
- PyModule_AddIntConstant (m , constant -> name , constant -> value );
1031
+ if (PyModule_AddIntConstant (
1032
+ mod , constant -> name , constant -> value ) < 0 ) {
1033
+ return -1 ;
1034
+ }
1037
1035
++ constant ;
1038
1036
}
1039
- return m ;
1037
+ return 0 ;
1038
+ }
1039
+
1040
+ static PyModuleDef_Slot termios_slots [] = {
1041
+ {Py_mod_exec , termios_exec },
1042
+ {0 , NULL }
1043
+ };
1044
+
1045
+ static struct PyModuleDef termiosmodule = {
1046
+ PyModuleDef_HEAD_INIT ,
1047
+ .m_name = "termios" ,
1048
+ .m_doc = termios__doc__ ,
1049
+ .m_size = sizeof (termiosmodulestate ),
1050
+ .m_methods = termios_methods ,
1051
+ .m_slots = termios_slots ,
1052
+ .m_traverse = termiosmodule_traverse ,
1053
+ .m_clear = termiosmodule_clear ,
1054
+ .m_free = termiosmodule_free ,
1055
+ };
1056
+
1057
+ PyMODINIT_FUNC PyInit_termios (void )
1058
+ {
1059
+ return PyModuleDef_Init (& termiosmodule );
1040
1060
}
0 commit comments