Skip to content

Commit

Permalink
Allow constructor to take 3 arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
moble committed Mar 13, 2017
1 parent a2c3152 commit 53d8322
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 7 additions & 2 deletions numpy_quaternion.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pyquaternion_init(PyObject *self, PyObject *args, PyObject *kwds)
// initialization should take place in `tp_new`, while for mutable
// types, most initialization should be deferred to `tp_init`."
// ---Python 2.7.8 docs

Py_ssize_t size = PyTuple_Size(args);
quaternion* q;
q = &(((PyQuaternion*)self)->obval);
if (kwds && PyDict_Size(kwds)) {
Expand All @@ -99,10 +101,13 @@ pyquaternion_init(PyObject *self, PyObject *args, PyObject *kwds)
return -1;
}

if (!PyArg_ParseTuple(args, "dddd", &q->w, &q->x, &q->y, &q->z)) {
if (((size == 3) && (!PyArg_ParseTuple(args, "ddd", &q->x, &q->y, &q->z)))
|| ((size == 4) && (!PyArg_ParseTuple(args, "dddd", &q->w, &q->x, &q->y, &q->z)))) {
PyErr_SetString(PyExc_TypeError,
"quaternion constructor takes four double (float) arguments");
"quaternion constructor takes three or four float arguments");
return -1;
} else if(size == 3) {
q->w = 0.0;
}

return 0;
Expand Down
7 changes: 7 additions & 0 deletions test/test_quaternion.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ def test_quaternion_members():
assert Q.y == 3.3
assert Q.z == 4.4

Q = quaternion.quaternion(2.2, 3.3, 4.4)
assert Q.real == 0.0
assert Q.w == 0.0
assert Q.x == 2.2
assert Q.y == 3.3
assert Q.z == 4.4


def test_constants():
assert quaternion.one == np.quaternion(1.0, 0.0, 0.0, 0.0)
Expand Down

0 comments on commit 53d8322

Please sign in to comment.