Skip to content

Commit ec30d0b

Browse files
committed
swap checking order for numbers and sequences to handle cases where something that is a sequence, implements a numeric protocol and it fails on conversion to double (also improved error checking for PyFloat_AsDouble)
1 parent 74043a6 commit ec30d0b

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src_c/math.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,19 +2280,23 @@ static int
22802280
_vector2_set(pgVector *self, PyObject *xOrSequence, PyObject *y)
22812281
{
22822282
if (xOrSequence) {
2283-
if (RealNumber_Check(xOrSequence)) {
2284-
self->coords[0] = PyFloat_AsDouble(xOrSequence);
2285-
/* scalar constructor. */
2286-
if (y == NULL) {
2287-
self->coords[1] = self->coords[0];
2283+
if (pgVectorCompatible_Check(xOrSequence, self->dim)) {
2284+
if (!PySequence_AsVectorCoords(xOrSequence, self->coords, 2)) {
2285+
return -1;
2286+
}
2287+
else {
22882288
return 0;
22892289
}
22902290
}
2291-
else if (pgVectorCompatible_Check(xOrSequence, self->dim)) {
2292-
if (!PySequence_AsVectorCoords(xOrSequence, self->coords, 2)) {
2291+
else if (RealNumber_Check(xOrSequence)) {
2292+
self->coords[0] = PyFloat_AsDouble(xOrSequence);
2293+
if (self->coords[0] == -1.0 && PyErr_Occurred()) {
22932294
return -1;
22942295
}
2295-
else {
2296+
2297+
/* scalar constructor. */
2298+
if (y == NULL) {
2299+
self->coords[1] = self->coords[0];
22962300
return 0;
22972301
}
22982302
}
@@ -2323,6 +2327,9 @@ _vector2_set(pgVector *self, PyObject *xOrSequence, PyObject *y)
23232327

23242328
if (RealNumber_Check(y)) {
23252329
self->coords[1] = PyFloat_AsDouble(y);
2330+
if (self->coords[1] == -1.0 && PyErr_Occurred()) {
2331+
return -1;
2332+
}
23262333
}
23272334
else {
23282335
goto error;

0 commit comments

Comments
 (0)