Skip to content

Commit 92dd2d6

Browse files
committed
fix vector3 tests and also actually fix vector3 init cuz I forgot to before
1 parent 13c5887 commit 92dd2d6

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

src_c/math.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,20 +2725,24 @@ static int
27252725
_vector3_set(pgVector *self, PyObject *xOrSequence, PyObject *y, PyObject *z)
27262726
{
27272727
if (xOrSequence) {
2728-
if (RealNumber_Check(xOrSequence)) {
2729-
self->coords[0] = PyFloat_AsDouble(xOrSequence);
2730-
/* scalar constructor. */
2731-
if (y == NULL && z == NULL) {
2732-
self->coords[1] = self->coords[0];
2733-
self->coords[2] = self->coords[0];
2728+
if (pgVectorCompatible_Check(xOrSequence, self->dim)) {
2729+
if (!PySequence_AsVectorCoords(xOrSequence, self->coords, 3)) {
2730+
return -1;
2731+
}
2732+
else {
27342733
return 0;
27352734
}
27362735
}
2737-
else if (pgVectorCompatible_Check(xOrSequence, self->dim)) {
2738-
if (!PySequence_AsVectorCoords(xOrSequence, self->coords, 3)) {
2736+
else if (RealNumber_Check(xOrSequence)) {
2737+
self->coords[0] = PyFloat_AsDouble(xOrSequence);
2738+
if (self->coords[0] == -1.0 && PyErr_Occurred()) {
27392739
return -1;
27402740
}
2741-
else {
2741+
2742+
/* scalar constructor. */
2743+
if (y == NULL && z == NULL) {
2744+
self->coords[1] = self->coords[0];
2745+
self->coords[2] = self->coords[0];
27422746
return 0;
27432747
}
27442748
}
@@ -2771,7 +2775,14 @@ _vector3_set(pgVector *self, PyObject *xOrSequence, PyObject *y, PyObject *z)
27712775
else if (y && z) {
27722776
if (RealNumber_Check(y) && RealNumber_Check(z)) {
27732777
self->coords[1] = PyFloat_AsDouble(y);
2778+
if (self->coords[1] == -1.0 && PyErr_Occurred()) {
2779+
return -1;
2780+
}
2781+
27742782
self->coords[2] = PyFloat_AsDouble(z);
2783+
if (self->coords[2] == -1.0 && PyErr_Occurred()) {
2784+
return -1;
2785+
}
27752786
}
27762787
else {
27772788
goto error;

test/math_test.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,14 +1486,15 @@ def __float__(self):
14861486
raise TypeError("Cannot convert to float")
14871487

14881488
def __getitem__(self, index):
1489-
return [1, 0][index]
1489+
return [1, 0, 5][index]
14901490

14911491
def __len__(self):
1492-
return 2
1492+
return 3
14931493

1494-
v = Vector2(NumericSequence())
1494+
v = Vector3(NumericSequence())
14951495
self.assertEqual(v.x, 1.0)
14961496
self.assertEqual(v.y, 0.0)
1497+
self.assertEqual(v.z, 5.0)
14971498

14981499
def testConstructionNumericNonFloat(self):
14991500
class NumericNonFloat:
@@ -1503,22 +1504,26 @@ def __float__(self):
15031504
raise TypeError("Cannot convert to float")
15041505

15051506
with self.assertRaises(TypeError):
1506-
Vector2(NumericNonFloat())
1507+
Vector3(NumericNonFloat())
15071508

15081509
with self.assertRaises(TypeError):
1509-
Vector2(NumericNonFloat(), NumericNonFloat())
1510+
Vector3(NumericNonFloat(), NumericNonFloat(), NumericNonFloat())
15101511

15111512
with self.assertRaises(TypeError):
1512-
Vector2(1.0, NumericNonFloat())
1513+
Vector3(1.0, NumericNonFloat(), 5.0)
1514+
1515+
with self.assertRaises(TypeError):
1516+
Vector3(1.0, 0.0, NumericNonFloat())
15131517

15141518
@unittest.skipIf(numpy is None, "numpy not available")
15151519
def testConstructionNumpyArray(self):
15161520
assert numpy is not None
15171521

1518-
arr = numpy.array([1.2, 3.4])
1519-
v = Vector2(arr)
1522+
arr = numpy.array([1.2, 3.4, 5.6], dtype=float)
1523+
v = Vector3(arr)
15201524
self.assertEqual(v.x, 1.2)
15211525
self.assertEqual(v.y, 3.4)
1526+
self.assertEqual(v.z, 5.6)
15221527

15231528
def testAttributeAccess(self):
15241529
tmp = self.v1.x

0 commit comments

Comments
 (0)