@@ -109,6 +109,39 @@ path_dealloc(PyPathObject *path) {
109
109
110
110
#define PyPath_Check (op ) (Py_TYPE(op) == &PyPathType)
111
111
112
+ static int
113
+ assign_item_to_array (double * xy , Py_ssize_t j , PyObject * op ) {
114
+ if (PyFloat_Check (op )) {
115
+ xy [j ++ ] = PyFloat_AS_DOUBLE (op );
116
+ } else if (PyLong_Check (op )) {
117
+ xy [j ++ ] = (float )PyLong_AS_LONG (op );
118
+ } else if (PyNumber_Check (op )) {
119
+ xy [j ++ ] = PyFloat_AsDouble (op );
120
+ } else if (PyList_Check (op )) {
121
+ for (int k = 0 ; k < 2 ; k ++ ) {
122
+ PyObject * op1 = PyList_GetItemRef (op , k );
123
+ if (op1 == NULL ) {
124
+ return -1 ;
125
+ }
126
+ j = assign_item_to_array (xy , j , op1 );
127
+ Py_DECREF (op1 );
128
+ if (j == -1 ) {
129
+ return -1 ;
130
+ }
131
+ }
132
+ } else {
133
+ double x , y ;
134
+ if (PyArg_ParseTuple (op , "dd" , & x , & y )) {
135
+ xy [j ++ ] = x ;
136
+ xy [j ++ ] = y ;
137
+ } else {
138
+ PyErr_SetString (PyExc_ValueError , "incorrect coordinate type" );
139
+ return -1 ;
140
+ }
141
+ }
142
+ return j ;
143
+ }
144
+
112
145
Py_ssize_t
113
146
PyPath_Flatten (PyObject * data , double * * pxy ) {
114
147
Py_ssize_t i , j , n ;
@@ -164,48 +197,32 @@ PyPath_Flatten(PyObject *data, double **pxy) {
164
197
return -1 ;
165
198
}
166
199
167
- #define assign_item_to_array (op , decref ) \
168
- if (PyFloat_Check(op)) { \
169
- xy[j++] = PyFloat_AS_DOUBLE(op); \
170
- } else if (PyLong_Check(op)) { \
171
- xy[j++] = (float)PyLong_AS_LONG(op); \
172
- } else if (PyNumber_Check(op)) { \
173
- xy[j++] = PyFloat_AsDouble(op); \
174
- } else if (PyArg_ParseTuple(op, "dd", &x, &y)) { \
175
- xy[j++] = x; \
176
- xy[j++] = y; \
177
- } else { \
178
- PyErr_SetString(PyExc_ValueError, "incorrect coordinate type"); \
179
- if (decref) { \
180
- Py_DECREF(op); \
181
- } \
182
- free(xy); \
183
- return -1; \
184
- } \
185
- if (decref) { \
186
- Py_DECREF(op); \
187
- }
188
-
189
200
/* Copy table to path array */
190
201
if (PyList_Check (data )) {
191
202
for (i = 0 ; i < n ; i ++ ) {
192
- double x , y ;
193
203
PyObject * op = PyList_GetItemRef (data , i );
194
204
if (op == NULL ) {
195
205
free (xy );
196
206
return -1 ;
197
207
}
198
- assign_item_to_array (op , 1 );
208
+ j = assign_item_to_array (xy , j , op );
209
+ Py_DECREF (op );
210
+ if (j == -1 ) {
211
+ free (xy );
212
+ return -1 ;
213
+ }
199
214
}
200
215
} else if (PyTuple_Check (data )) {
201
216
for (i = 0 ; i < n ; i ++ ) {
202
- double x , y ;
203
217
PyObject * op = PyTuple_GET_ITEM (data , i );
204
- assign_item_to_array (op , 0 );
218
+ j = assign_item_to_array (xy , j , op );
219
+ if (j == -1 ) {
220
+ free (xy );
221
+ return -1 ;
222
+ }
205
223
}
206
224
} else {
207
225
for (i = 0 ; i < n ; i ++ ) {
208
- double x , y ;
209
226
PyObject * op = PySequence_GetItem (data , i );
210
227
if (!op ) {
211
228
/* treat IndexError as end of sequence */
@@ -217,7 +234,12 @@ PyPath_Flatten(PyObject *data, double **pxy) {
217
234
return -1 ;
218
235
}
219
236
}
220
- assign_item_to_array (op , 1 );
237
+ j = assign_item_to_array (xy , j , op );
238
+ Py_DECREF (op );
239
+ if (j == -1 ) {
240
+ free (xy );
241
+ return -1 ;
242
+ }
221
243
}
222
244
}
223
245
0 commit comments