@@ -77,23 +77,27 @@ _pg_polygon_vertices_astuple(pgPolygonBase *poly)
77
77
static int
78
78
_pgPolygon_InitFromObject (PyObject * obj , pgPolygonBase * init_poly )
79
79
{
80
- /* This function always allocates new memory for the vertices, and
81
- * therefore it should be used only when the polygon is not yet
82
- * initialized. */
80
+ /* This function initializes a Polygon object. It can resize the memory
81
+ * for the vertices if needed, and therefore it should be used only when
82
+ * the polygon is not yet initialized. */
83
83
Py_ssize_t length ;
84
84
85
85
/* If the Python object is already a pgPolygonBase object, copy the
86
86
* relevant information from that object to the init_poly object into
87
- * the new memory. */
87
+ * the memory, resize if needed . */
88
88
if (pgPolygon_Check (obj )) {
89
89
pgPolygonBase * poly = & pgPolygon_AsPolygon (obj );
90
90
91
91
/* Copy the vertices from the old polygon to the new polygon, while
92
- * also allocating new memory. */
93
- if (!(init_poly -> vertices = _pg_new_vertices_from_polygon (poly ))) {
94
- PyErr_Clear ();
95
- return 0 ;
92
+ * also allocating new memory. Resize the vertices array if needed. */
93
+ if (poly -> verts_num > 3 ) {
94
+ PyMem_Resize (init_poly -> vertices , double , poly -> verts_num * 2 );
95
+ if (!init_poly -> vertices ) {
96
+ return 0 ;
97
+ }
96
98
}
99
+ memcpy (init_poly -> vertices , poly -> vertices ,
100
+ poly -> verts_num * 2 * sizeof (double ));
97
101
98
102
init_poly -> verts_num = poly -> verts_num ;
99
103
init_poly -> c_x = poly -> c_x ;
@@ -113,10 +117,12 @@ _pgPolygon_InitFromObject(PyObject *obj, pgPolygonBase *init_poly)
113
117
if (length >= 3 ) {
114
118
Py_ssize_t i ;
115
119
116
- /*Allocate memory for the vertices of the polygon*/
117
- init_poly -> vertices = PyMem_New (double , length * 2 );
118
- if (!init_poly -> vertices ) {
119
- return 0 ;
120
+ /* Resize the vertices array if needed. */
121
+ if (length > 3 ) {
122
+ PyMem_Resize (init_poly -> vertices , double , length * 2 );
123
+ if (!init_poly -> vertices ) {
124
+ return 0 ;
125
+ }
120
126
}
121
127
122
128
/*Extract the x and y coordinates of each vertex and store them in
@@ -171,9 +177,11 @@ _pgPolygon_InitFromObject(PyObject *obj, pgPolygonBase *init_poly)
171
177
Py_ssize_t i ;
172
178
173
179
/*Allocate memory for the vertices of the polygon*/
174
- init_poly -> vertices = PyMem_New (double , length * 2 );
175
- if (!init_poly -> vertices ) {
176
- return 0 ;
180
+ if (length > 3 ) {
181
+ PyMem_Resize (init_poly -> vertices , double , length * 2 );
182
+ if (!init_poly -> vertices ) {
183
+ return 0 ;
184
+ }
177
185
}
178
186
179
187
/*Extract the x and y coordinates of each vertex and store
@@ -231,12 +239,6 @@ _pgPolygon_InitFromObject(PyObject *obj, pgPolygonBase *init_poly)
231
239
PyObject * iter = PyObject_GetIter (obj );
232
240
Py_ssize_t i = 0 , currently_allocated = 3 ;
233
241
234
- /* Allocate memory for the vertices of the polygon */
235
- init_poly -> vertices = PyMem_New (double , 2 * currently_allocated );
236
- if (!init_poly -> vertices ) {
237
- return 0 ;
238
- }
239
-
240
242
/* Extract the x and y coordinates of each vertex and store
241
243
them in the init_poly object */
242
244
while ((item = PyIter_Next (iter ))) {
@@ -356,12 +358,13 @@ pgPolygon_FromObject(PyObject *obj, pgPolygonBase *out, int *was_sequence)
356
358
357
359
if (pgPolygon_Check (obj )) {
358
360
/*Do a shallow copy of the pgPolygonBase object*/
359
- memcpy (out , & pgPolygon_AsPolygon (obj ), sizeof (pgPolygonBase ));
360
361
* was_sequence = 0 ;
362
+ memcpy (out , & pgPolygon_AsPolygon (obj ), sizeof (pgPolygonBase ));
361
363
return 1 ;
362
364
}
363
365
364
366
if (PySequence_FAST_CHECK (obj )) {
367
+ * was_sequence = 1 ;
365
368
PyObject * * f_arr = PySequence_Fast_ITEMS (obj );
366
369
length = PySequence_Fast_GET_SIZE (obj );
367
370
@@ -374,7 +377,7 @@ pgPolygon_FromObject(PyObject *obj, pgPolygonBase *out, int *was_sequence)
374
377
return 0 ;
375
378
}
376
379
377
- for (i = 0 ; i < out -> verts_num ; i ++ ) {
380
+ for (i = 0 ; i < length ; i ++ ) {
378
381
double x , y ;
379
382
if (!pg_TwoDoublesFromObj (f_arr [i ], & x , & y )) {
380
383
PyMem_Free (out -> vertices );
@@ -391,22 +394,21 @@ pgPolygon_FromObject(PyObject *obj, pgPolygonBase *out, int *was_sequence)
391
394
out -> c_x /= length ;
392
395
out -> c_y /= length ;
393
396
394
- * was_sequence = 1 ;
395
397
return 1 ;
396
398
}
397
399
else if (length == 1 ) {
398
400
if (!pgPolygon_FromObject (f_arr [0 ], out , was_sequence )) {
399
401
return 0 ;
400
402
}
401
- * was_sequence = 1 ;
402
403
return 1 ;
403
404
}
404
-
405
+ /*Length is 0 or 2 -> invalid polygon*/
405
406
return 0 ;
406
407
}
407
408
408
409
else if (PySequence_Check (obj )) {
409
410
/* Path for other sequences or Types that count as sequences*/
411
+ * was_sequence = 1 ;
410
412
PyObject * tmp = NULL ;
411
413
length = PySequence_Length (obj );
412
414
@@ -437,7 +439,6 @@ pgPolygon_FromObject(PyObject *obj, pgPolygonBase *out, int *was_sequence)
437
439
out -> c_x /= length ;
438
440
out -> c_y /= length ;
439
441
440
- * was_sequence = 1 ;
441
442
return 1 ;
442
443
}
443
444
else if (length == 1 ) {
@@ -448,10 +449,9 @@ pgPolygon_FromObject(PyObject *obj, pgPolygonBase *out, int *was_sequence)
448
449
return 0 ;
449
450
}
450
451
Py_DECREF (tmp );
451
- * was_sequence = 1 ;
452
452
return 1 ;
453
453
}
454
-
454
+ /*Length is 0 or 2 -> invalid polygon*/
455
455
return 0 ;
456
456
}
457
457
@@ -521,6 +521,8 @@ pgPolygon_FromObjectFastcall(PyObject *const *args, Py_ssize_t nargs,
521
521
else if (nargs >= 3 ) {
522
522
Py_ssize_t i ;
523
523
524
+ * was_sequence = 1 ;
525
+
524
526
out -> vertices = PyMem_New (double , nargs * 2 );
525
527
if (!out -> vertices ) {
526
528
return 0 ;
@@ -542,7 +544,6 @@ pgPolygon_FromObjectFastcall(PyObject *const *args, Py_ssize_t nargs,
542
544
out -> c_x /= nargs ;
543
545
out -> c_y /= nargs ;
544
546
545
- * was_sequence = 1 ;
546
547
return 1 ;
547
548
}
548
549
@@ -652,8 +653,12 @@ pg_polygon_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
652
653
pgPolygonObject * self = (pgPolygonObject * )type -> tp_alloc (type , 0 );
653
654
654
655
if (self ) {
655
- self -> polygon .vertices = NULL ;
656
- self -> polygon .verts_num = 0 ;
656
+ self -> polygon .vertices = PyMem_New (double , 6 );
657
+ if (!self -> polygon .vertices ) {
658
+ Py_DECREF (self );
659
+ return NULL ;
660
+ }
661
+ self -> polygon .verts_num = 3 ;
657
662
self -> polygon .c_x = 0 ;
658
663
self -> polygon .c_y = 0 ;
659
664
self -> weakreflist = NULL ;
0 commit comments