@@ -120,37 +120,52 @@ termios_tcgetattr_impl(PyObject *module, int fd)
120120 v = PyBytes_FromStringAndSize (& ch , 1 );
121121 if (v == NULL )
122122 goto err ;
123- PyList_SetItem (cc , i , v );
123+ PyList_SET_ITEM (cc , i , v );
124124 }
125125
126126 /* Convert the MIN and TIME slots to integer. On some systems, the
127127 MIN and TIME slots are the same as the EOF and EOL slots. So we
128128 only do this in noncanonical input mode. */
129129 if ((mode .c_lflag & ICANON ) == 0 ) {
130130 v = PyLong_FromLong ((long )mode .c_cc [VMIN ]);
131- if (v == NULL )
131+ if (v == NULL ) {
132+ goto err ;
133+ }
134+ if (PyList_SetItem (cc , VMIN , v ) < 0 ) {
132135 goto err ;
133- PyList_SetItem ( cc , VMIN , v );
136+ }
134137 v = PyLong_FromLong ((long )mode .c_cc [VTIME ]);
135- if (v == NULL )
138+ if (v == NULL ) {
139+ goto err ;
140+ }
141+ if (PyList_SetItem (cc , VTIME , v ) < 0 ) {
136142 goto err ;
137- PyList_SetItem ( cc , VTIME , v );
143+ }
138144 }
139145
140- if (!(v = PyList_New (7 )))
141- goto err ;
142-
143- PyList_SetItem (v , 0 , PyLong_FromLong ((long )mode .c_iflag ));
144- PyList_SetItem (v , 1 , PyLong_FromLong ((long )mode .c_oflag ));
145- PyList_SetItem (v , 2 , PyLong_FromLong ((long )mode .c_cflag ));
146- PyList_SetItem (v , 3 , PyLong_FromLong ((long )mode .c_lflag ));
147- PyList_SetItem (v , 4 , PyLong_FromLong ((long )ispeed ));
148- PyList_SetItem (v , 5 , PyLong_FromLong ((long )ospeed ));
149- if (PyErr_Occurred ()) {
150- Py_DECREF (v );
146+ if (!(v = PyList_New (7 ))) {
151147 goto err ;
152148 }
153- PyList_SetItem (v , 6 , cc );
149+
150+ #define ADD_LONG_ITEM (index , val ) \
151+ do { \
152+ PyObject *l = PyLong_FromLong((long)val); \
153+ if (l == NULL) { \
154+ Py_DECREF(v); \
155+ goto err; \
156+ } \
157+ PyList_SET_ITEM(v, index, l); \
158+ } while (0)
159+
160+ ADD_LONG_ITEM (0 , mode .c_iflag );
161+ ADD_LONG_ITEM (1 , mode .c_oflag );
162+ ADD_LONG_ITEM (2 , mode .c_cflag );
163+ ADD_LONG_ITEM (3 , mode .c_lflag );
164+ ADD_LONG_ITEM (4 , ispeed );
165+ ADD_LONG_ITEM (5 , ospeed );
166+ #undef ADD_LONG_ITEM
167+
168+ PyList_SET_ITEM (v , 6 , cc );
154169 return v ;
155170 err :
156171 Py_DECREF (cc );
0 commit comments