PyAPI: add utilities PyTuple_SET_ITEMS, Py_INCREF_RET
[blender.git] / source / blender / python / mathutils / mathutils_Euler.c
index b7b011931073da3495fe3ed471d37f5cc456b520..9c0ced39403927ca68d13e68b3137d3e1e4dc00a 100644 (file)
@@ -1,6 +1,4 @@
 /*
 /*
- * $Id: mathutils_Euler.c 38409 2011-07-15 04:01:47Z campbellbarton $
- *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
- * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
- * All rights reserved.
- *
- *
  * Contributor(s): Joseph Gilbert
  *
  * ***** END GPL LICENSE BLOCK *****
  */
 
  * Contributor(s): Joseph Gilbert
  *
  * ***** END GPL LICENSE BLOCK *****
  */
 
-/** \file blender/python/generic/mathutils_Euler.c
- *  \ingroup pygen
+/** \file blender/python/mathutils/mathutils_Euler.c
+ *  \ingroup pymathutils
  */
 
 
  */
 
 
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "../generic/python_utildefines.h"
+
+#ifndef MATH_STANDALONE
+#  include "BLI_dynstr.h"
+#endif
 
 #define EULER_SIZE 3
 
 
 #define EULER_SIZE 3
 
-//----------------------------------mathutils.Euler() -------------------
-//makes a new euler for you to play with
+/* ----------------------------------mathutils.Euler() ------------------- */
+/* makes a new euler for you to play with */
 static PyObject *Euler_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
 static PyObject *Euler_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-       PyObject *seq= NULL;
-       const char *order_str= NULL;
+       PyObject *seq = NULL;
+       const char *order_str = NULL;
 
 
-       float eul[EULER_SIZE]= {0.0f, 0.0f, 0.0f};
-       short order= EULER_ORDER_XYZ;
+       float eul[EULER_SIZE] = {0.0f, 0.0f, 0.0f};
+       short order = EULER_ORDER_XYZ;
 
        if (kwds && PyDict_Size(kwds)) {
                PyErr_SetString(PyExc_TypeError,
 
        if (kwds && PyDict_Size(kwds)) {
                PyErr_SetString(PyExc_TypeError,
@@ -60,39 +59,48 @@ static PyObject *Euler_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
        if (!PyArg_ParseTuple(args, "|Os:mathutils.Euler", &seq, &order_str))
                return NULL;
 
        if (!PyArg_ParseTuple(args, "|Os:mathutils.Euler", &seq, &order_str))
                return NULL;
 
-       switch(PyTuple_GET_SIZE(args)) {
-       case 0:
-               break;
-       case 2:
-               if ((order=euler_order_from_string(order_str, "mathutils.Euler()")) == -1)
-                       return NULL;
-               /* intentionally pass through */
-       case 1:
-               if (mathutils_array_parse(eul, EULER_SIZE, EULER_SIZE, seq, "mathutils.Euler()") == -1)
-                       return NULL;
-               break;
+       switch (PyTuple_GET_SIZE(args)) {
+               case 0:
+                       break;
+               case 2:
+                       if ((order = euler_order_from_string(order_str, "mathutils.Euler()")) == -1)
+                               return NULL;
+                       /* fall-through */
+               case 1:
+                       if (mathutils_array_parse(eul, EULER_SIZE, EULER_SIZE, seq, "mathutils.Euler()") == -1)
+                               return NULL;
+                       break;
        }
        }
-       return newEulerObject(eul, order, Py_NEW, type);
+       return Euler_CreatePyObject(eul, order, type);
 }
 
 }
 
-/* internal use, assuem read callback is done */
+/* internal use, assume read callback is done */
 static const char *euler_order_str(EulerObject *self)
 {
        static const char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"};
 static const char *euler_order_str(EulerObject *self)
 {
        static const char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"};
-       return order[self->order-EULER_ORDER_XYZ];
+       return order[self->order - EULER_ORDER_XYZ];
 }
 
 short euler_order_from_string(const char *str, const char *error_prefix)
 {
 }
 
 short euler_order_from_string(const char *str, const char *error_prefix)
 {
-       if ((str[0] && str[1] && str[2] && str[3]=='\0')) {
-               switch(*((PY_INT32_T *)str)) {
-                       case 'X'|'Y'<<8|'Z'<<16:        return EULER_ORDER_XYZ;
-                       case 'X'|'Z'<<8|'Y'<<16:        return EULER_ORDER_XZY;
-                       case 'Y'|'X'<<8|'Z'<<16:        return EULER_ORDER_YXZ;
-                       case 'Y'|'Z'<<8|'X'<<16:        return EULER_ORDER_YZX;
-                       case 'Z'|'X'<<8|'Y'<<16:        return EULER_ORDER_ZXY;
-                       case 'Z'|'Y'<<8|'X'<<16:        return EULER_ORDER_ZYX;
+       if ((str[0] && str[1] && str[2] && str[3] == '\0')) {
+
+#ifdef __LITTLE_ENDIAN__
+#  define MAKE_ID3(a, b, c)  (((a)) | ((b) << 8) | ((c) << 16))
+#else
+#  define MAKE_ID3(a, b, c)  (((a) << 24) | ((b) << 16) | ((c) << 8))
+#endif
+
+               switch (*((PY_INT32_T *)str)) {
+                       case MAKE_ID3('X', 'Y', 'Z'): return EULER_ORDER_XYZ;
+                       case MAKE_ID3('X', 'Z', 'Y'): return EULER_ORDER_XZY;
+                       case MAKE_ID3('Y', 'X', 'Z'): return EULER_ORDER_YXZ;
+                       case MAKE_ID3('Y', 'Z', 'X'): return EULER_ORDER_YZX;
+                       case MAKE_ID3('Z', 'X', 'Y'): return EULER_ORDER_ZXY;
+                       case MAKE_ID3('Z', 'Y', 'X'): return EULER_ORDER_ZYX;
                }
                }
+
+#undef MAKE_ID3
        }
 
        PyErr_Format(PyExc_ValueError,
        }
 
        PyErr_Format(PyExc_ValueError,
@@ -107,15 +115,15 @@ static PyObject *Euler_ToTupleExt(EulerObject *self, int ndigits)
        PyObject *ret;
        int i;
 
        PyObject *ret;
        int i;
 
-       ret= PyTuple_New(EULER_SIZE);
+       ret = PyTuple_New(EULER_SIZE);
 
        if (ndigits >= 0) {
 
        if (ndigits >= 0) {
-               for (i= 0; i < EULER_SIZE; i++) {
+               for (i = 0; i < EULER_SIZE; i++) {
                        PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->eul[i], ndigits)));
                }
        }
        else {
                        PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->eul[i], ndigits)));
                }
        }
        else {
-               for (i= 0; i < EULER_SIZE; i++) {
+               for (i = 0; i < EULER_SIZE; i++) {
                        PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->eul[i]));
                }
        }
                        PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->eul[i]));
                }
        }
@@ -123,8 +131,8 @@ static PyObject *Euler_ToTupleExt(EulerObject *self, int ndigits)
        return ret;
 }
 
        return ret;
 }
 
-//-----------------------------METHODS----------------------------
-//return a quaternion representation of the euler
+/-----------------------------METHODS----------------------------
+ * return a quaternion representation of the euler */
 
 PyDoc_STRVAR(Euler_to_quaternion_doc,
 ".. method:: to_quaternion()\n"
 
 PyDoc_STRVAR(Euler_to_quaternion_doc,
 ".. method:: to_quaternion()\n"
@@ -134,7 +142,7 @@ PyDoc_STRVAR(Euler_to_quaternion_doc,
 "   :return: Quaternion representation of the euler.\n"
 "   :rtype: :class:`Quaternion`\n"
 );
 "   :return: Quaternion representation of the euler.\n"
 "   :rtype: :class:`Quaternion`\n"
 );
-static PyObject *Euler_to_quaternion(EulerObject * self)
+static PyObject *Euler_to_quaternion(EulerObject *self)
 {
        float quat[4];
 
 {
        float quat[4];
 
@@ -143,10 +151,10 @@ static PyObject *Euler_to_quaternion(EulerObject * self)
 
        eulO_to_quat(quat, self->eul, self->order);
 
 
        eulO_to_quat(quat, self->eul, self->order);
 
-       return newQuaternionObject(quat, Py_NEW, NULL);
+       return Quaternion_CreatePyObject(quat, NULL);
 }
 
 }
 
-//return a matrix representation of the euler
+/* return a matrix representation of the euler */
 PyDoc_STRVAR(Euler_to_matrix_doc,
 ".. method:: to_matrix()\n"
 "\n"
 PyDoc_STRVAR(Euler_to_matrix_doc,
 ".. method:: to_matrix()\n"
 "\n"
@@ -155,7 +163,7 @@ PyDoc_STRVAR(Euler_to_matrix_doc,
 "   :return: A 3x3 roation matrix representation of the euler.\n"
 "   :rtype: :class:`Matrix`\n"
 );
 "   :return: A 3x3 roation matrix representation of the euler.\n"
 "   :rtype: :class:`Matrix`\n"
 );
-static PyObject *Euler_to_matrix(EulerObject * self)
+static PyObject *Euler_to_matrix(EulerObject *self)
 {
        float mat[9];
 
 {
        float mat[9];
 
@@ -164,7 +172,7 @@ static PyObject *Euler_to_matrix(EulerObject * self)
 
        eulO_to_mat3((float (*)[3])mat, self->eul, self->order);
 
 
        eulO_to_mat3((float (*)[3])mat, self->eul, self->order);
 
-       return newMatrixObject(mat, 3, 3 , Py_NEW, NULL);
+       return Matrix_CreatePyObject(mat, 3, 3, NULL);
 }
 
 PyDoc_STRVAR(Euler_zero_doc,
 }
 
 PyDoc_STRVAR(Euler_zero_doc,
@@ -172,7 +180,7 @@ PyDoc_STRVAR(Euler_zero_doc,
 "\n"
 "   Set all values to zero.\n"
 );
 "\n"
 "   Set all values to zero.\n"
 );
-static PyObject *Euler_zero(EulerObject * self)
+static PyObject *Euler_zero(EulerObject *self)
 {
        zero_v3(self->eul);
 
 {
        zero_v3(self->eul);
 
@@ -193,19 +201,19 @@ PyDoc_STRVAR(Euler_rotate_axis_doc,
 "   :arg angle: angle in radians.\n"
 "   :type angle: float\n"
 );
 "   :arg angle: angle in radians.\n"
 "   :type angle: float\n"
 );
-static PyObject *Euler_rotate_axis(EulerObject * self, PyObject *args)
+static PyObject *Euler_rotate_axis(EulerObject *self, PyObject *args)
 {
        float angle = 0.0f;
        int axis; /* actually a character */
 
 {
        float angle = 0.0f;
        int axis; /* actually a character */
 
-       if (!PyArg_ParseTuple(args, "Cf:rotate", &axis, &angle)) {
+       if (!PyArg_ParseTuple(args, "Cf:rotate_axis", &axis, &angle)) {
                PyErr_SetString(PyExc_TypeError,
                                "Euler.rotate_axis(): "
                                "expected an axis 'X', 'Y', 'Z' and an angle (float)");
                return NULL;
        }
 
                PyErr_SetString(PyExc_TypeError,
                                "Euler.rotate_axis(): "
                                "expected an axis 'X', 'Y', 'Z' and an angle (float)");
                return NULL;
        }
 
-       if (!(ELEM3(axis, 'X', 'Y', 'Z'))) {
+       if (!(ELEM(axis, 'X', 'Y', 'Z'))) {
                PyErr_SetString(PyExc_ValueError,
                                "Euler.rotate_axis(): "
                                "expected axis to be 'X', 'Y' or 'Z'");
                PyErr_SetString(PyExc_ValueError,
                                "Euler.rotate_axis(): "
                                "expected axis to be 'X', 'Y' or 'Z'");
@@ -226,12 +234,12 @@ static PyObject *Euler_rotate_axis(EulerObject * self, PyObject *args)
 PyDoc_STRVAR(Euler_rotate_doc,
 ".. method:: rotate(other)\n"
 "\n"
 PyDoc_STRVAR(Euler_rotate_doc,
 ".. method:: rotate(other)\n"
 "\n"
-"   Rotates the euler by another mathutils value.\n"
+"   Rotates the euler by another mathutils value.\n"
 "\n"
 "   :arg other: rotation component of mathutils value\n"
 "   :type other: :class:`Euler`, :class:`Quaternion` or :class:`Matrix`\n"
 );
 "\n"
 "   :arg other: rotation component of mathutils value\n"
 "   :type other: :class:`Euler`, :class:`Quaternion` or :class:`Matrix`\n"
 );
-static PyObject *Euler_rotate(EulerObject * self, PyObject *value)
+static PyObject *Euler_rotate(EulerObject *self, PyObject *value)
 {
        float self_rmat[3][3], other_rmat[3][3], rmat[3][3];
 
 {
        float self_rmat[3][3], other_rmat[3][3], rmat[3][3];
 
@@ -242,7 +250,7 @@ static PyObject *Euler_rotate(EulerObject * self, PyObject *value)
                return NULL;
 
        eulO_to_mat3(self_rmat, self->eul, self->order);
                return NULL;
 
        eulO_to_mat3(self_rmat, self->eul, self->order);
-       mul_m3_m3m3(rmat, self_rmat, other_rmat);
+       mul_m3_m3m3(rmat, other_rmat, self_rmat);
 
        mat3_to_compatible_eulO(self->eul, self->eul, self->order, rmat);
 
 
        mat3_to_compatible_eulO(self->eul, self->eul, self->order, rmat);
 
@@ -258,15 +266,18 @@ PyDoc_STRVAR(Euler_make_compatible_doc,
 "\n"
 "   .. note:: the rotation order is not taken into account for this function.\n"
 );
 "\n"
 "   .. note:: the rotation order is not taken into account for this function.\n"
 );
-static PyObject *Euler_make_compatible(EulerObject * self, PyObject *value)
+static PyObject *Euler_make_compatible(EulerObject *self, PyObject *value)
 {
        float teul[EULER_SIZE];
 
        if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
 {
        float teul[EULER_SIZE];
 
        if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       if (mathutils_array_parse(teul, EULER_SIZE, EULER_SIZE, value, "euler.make_compatible(other), invalid 'other' arg") == -1)
+       if (mathutils_array_parse(teul, EULER_SIZE, EULER_SIZE, value,
+                                 "euler.make_compatible(other), invalid 'other' arg") == -1)
+       {
                return NULL;
                return NULL;
+       }
 
        compatible_eul(self->eul, teul);
 
 
        compatible_eul(self->eul, teul);
 
@@ -275,8 +286,8 @@ static PyObject *Euler_make_compatible(EulerObject * self, PyObject *value)
        Py_RETURN_NONE;
 }
 
        Py_RETURN_NONE;
 }
 
-//----------------------------Euler.rotate()-----------------------
-// return a copy of the euler
+/----------------------------Euler.rotate()-----------------------
+ * return a copy of the euler */
 
 PyDoc_STRVAR(Euler_copy_doc,
 ".. function:: copy()\n"
 
 PyDoc_STRVAR(Euler_copy_doc,
 ".. function:: copy()\n"
@@ -294,75 +305,99 @@ static PyObject *Euler_copy(EulerObject *self)
        if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
        if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       return newEulerObject(self->eul, self->order, Py_NEW, Py_TYPE(self));
+       return Euler_CreatePyObject(self->eul, self->order, Py_TYPE(self));
+}
+static PyObject *Euler_deepcopy(EulerObject *self, PyObject *args)
+{
+       if (!mathutils_deepcopy_args_check(args))
+               return NULL;
+       return Euler_copy(self);
 }
 
 }
 
-//----------------------------print object (internal)--------------
-//print the object to screen
+/----------------------------print object (internal)--------------
+ * print the object to screen */
 
 
-static PyObject *Euler_repr(EulerObject * self)
+static PyObject *Euler_repr(EulerObject *self)
 {
        PyObject *ret, *tuple;
 
        if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
 {
        PyObject *ret, *tuple;
 
        if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       tuple= Euler_ToTupleExt(self, -1);
+       tuple = Euler_ToTupleExt(self, -1);
 
 
-       ret= PyUnicode_FromFormat("Euler(%R, '%s')", tuple, euler_order_str(self));
+       ret = PyUnicode_FromFormat("Euler(%R, '%s')", tuple, euler_order_str(self));
 
        Py_DECREF(tuple);
        return ret;
 }
 
 
        Py_DECREF(tuple);
        return ret;
 }
 
-static PyObject* Euler_richcmpr(PyObject *a, PyObject *b, int op)
+#ifndef MATH_STANDALONE
+static PyObject *Euler_str(EulerObject *self)
+{
+       DynStr *ds;
+
+       if (BaseMath_ReadCallback(self) == -1)
+               return NULL;
+
+       ds = BLI_dynstr_new();
+
+       BLI_dynstr_appendf(ds, "<Euler (x=%.4f, y=%.4f, z=%.4f), order='%s'>",
+                          self->eul[0], self->eul[1], self->eul[2], euler_order_str(self));
+
+       return mathutils_dynstr_to_py(ds); /* frees ds */
+}
+#endif
+
+static PyObject *Euler_richcmpr(PyObject *a, PyObject *b, int op)
 {
        PyObject *res;
 {
        PyObject *res;
-       int ok= -1; /* zero is true */
+       int ok = -1; /* zero is true */
 
        if (EulerObject_Check(a) && EulerObject_Check(b)) {
 
        if (EulerObject_Check(a) && EulerObject_Check(b)) {
-               EulerObject *eulA= (EulerObject*)a;
-               EulerObject *eulB= (EulerObject*)b;
+               EulerObject *eulA = (EulerObject *)a;
+               EulerObject *eulB = (EulerObject *)b;
 
                if (BaseMath_ReadCallback(eulA) == -1 || BaseMath_ReadCallback(eulB) == -1)
                        return NULL;
 
 
                if (BaseMath_ReadCallback(eulA) == -1 || BaseMath_ReadCallback(eulB) == -1)
                        return NULL;
 
-               ok= ((eulA->order == eulB->order) && EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1)) ? 0 : -1;
+               ok = ((eulA->order == eulB->order) && EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1)) ? 0 : -1;
        }
 
        switch (op) {
        }
 
        switch (op) {
-       case Py_NE:
-               ok = !ok; /* pass through */
-       case Py_EQ:
-               res = ok ? Py_False : Py_True;
-               break;
-
-       case Py_LT:
-       case Py_LE:
-       case Py_GT:
-       case Py_GE:
-               res = Py_NotImplemented;
-               break;
-       default:
-               PyErr_BadArgument();
-               return NULL;
+               case Py_NE:
+                       ok = !ok;
+                       /* fall-through */
+               case Py_EQ:
+                       res = ok ? Py_False : Py_True;
+                       break;
+
+               case Py_LT:
+               case Py_LE:
+               case Py_GT:
+               case Py_GE:
+                       res = Py_NotImplemented;
+                       break;
+               default:
+                       PyErr_BadArgument();
+                       return NULL;
        }
 
        }
 
-       return Py_INCREF(res), res;
+       return Py_INCREF_RET(res);
 }
 
 }
 
-//---------------------SEQUENCE PROTOCOLS------------------------
-//----------------------------len(object)------------------------
-//sequence length
+/* ---------------------SEQUENCE PROTOCOLS------------------------ */
+/* ----------------------------len(object)------------------------ */
+/* sequence length */
 static int Euler_len(EulerObject *UNUSED(self))
 {
        return EULER_SIZE;
 }
 static int Euler_len(EulerObject *UNUSED(self))
 {
        return EULER_SIZE;
 }
-//----------------------------object[]---------------------------
-//sequence accessor (get)
-static PyObject *Euler_item(EulerObject * self, int i)
+/* ----------------------------object[]--------------------------- */
+/* sequence accessor (get) */
+static PyObject *Euler_item(EulerObject *self, int i)
 {
 {
-       if (i<0) i= EULER_SIZE-i;
+       if (i < 0) i = EULER_SIZE - i;
 
        if (i < 0 || i >= EULER_SIZE) {
                PyErr_SetString(PyExc_IndexError,
 
        if (i < 0 || i >= EULER_SIZE) {
                PyErr_SetString(PyExc_IndexError,
@@ -377,20 +412,20 @@ static PyObject *Euler_item(EulerObject * self, int i)
        return PyFloat_FromDouble(self->eul[i]);
 
 }
        return PyFloat_FromDouble(self->eul[i]);
 
 }
-//----------------------------object[]-------------------------
-//sequence accessor (set)
-static int Euler_ass_item(EulerObject * self, int i, PyObject *value)
+/* ----------------------------object[]------------------------- */
+/* sequence accessor (set) */
+static int Euler_ass_item(EulerObject *self, int i, PyObject *value)
 {
        float f = PyFloat_AsDouble(value);
 
 {
        float f = PyFloat_AsDouble(value);
 
-       if (f == -1 && PyErr_Occurred()) { // parsed item not a number
+       if (f == -1 && PyErr_Occurred()) {  /* parsed item not a number */
                PyErr_SetString(PyExc_TypeError,
                                "euler[attribute] = x: "
                PyErr_SetString(PyExc_TypeError,
                                "euler[attribute] = x: "
-                               "argument not a number");
+                               "assigned value not a number");
                return -1;
        }
 
                return -1;
        }
 
-       if (i<0) i= EULER_SIZE-i;
+       if (i < 0) i = EULER_SIZE - i;
 
        if (i < 0 || i >= EULER_SIZE) {
                PyErr_SetString(PyExc_IndexError,
 
        if (i < 0 || i >= EULER_SIZE) {
                PyErr_SetString(PyExc_IndexError,
@@ -406,9 +441,9 @@ static int Euler_ass_item(EulerObject * self, int i, PyObject *value)
 
        return 0;
 }
 
        return 0;
 }
-//----------------------------object[z:y]------------------------
-//sequence slice (get)
-static PyObject *Euler_slice(EulerObject * self, int begin, int end)
+/* ----------------------------object[z:y]------------------------ */
+/* sequence slice (get) */
+static PyObject *Euler_slice(EulerObject *self, int begin, int end)
 {
        PyObject *tuple;
        int count;
 {
        PyObject *tuple;
        int count;
@@ -417,19 +452,19 @@ static PyObject *Euler_slice(EulerObject * self, int begin, int end)
                return NULL;
 
        CLAMP(begin, 0, EULER_SIZE);
                return NULL;
 
        CLAMP(begin, 0, EULER_SIZE);
-       if (end<0) end= (EULER_SIZE + 1) + end;
+       if (end < 0) end = (EULER_SIZE + 1) + end;
        CLAMP(end, 0, EULER_SIZE);
        CLAMP(end, 0, EULER_SIZE);
-       begin= MIN2(begin, end);
+       begin = MIN2(begin, end);
 
 
-       tuple= PyTuple_New(end - begin);
+       tuple = PyTuple_New(end - begin);
        for (count = begin; count < end; count++) {
                PyTuple_SET_ITEM(tuple, count - begin, PyFloat_FromDouble(self->eul[count]));
        }
 
        return tuple;
 }
        for (count = begin; count < end; count++) {
                PyTuple_SET_ITEM(tuple, count - begin, PyFloat_FromDouble(self->eul[count]));
        }
 
        return tuple;
 }
-//----------------------------object[z:y]------------------------
-//sequence slice (set)
+/* ----------------------------object[z:y]------------------------ */
+/* sequence slice (set) */
 static int Euler_ass_slice(EulerObject *self, int begin, int end, PyObject *seq)
 {
        int i, size;
 static int Euler_ass_slice(EulerObject *self, int begin, int end, PyObject *seq)
 {
        int i, size;
@@ -439,11 +474,11 @@ static int Euler_ass_slice(EulerObject *self, int begin, int end, PyObject *seq)
                return -1;
 
        CLAMP(begin, 0, EULER_SIZE);
                return -1;
 
        CLAMP(begin, 0, EULER_SIZE);
-       if (end<0) end= (EULER_SIZE + 1) + end;
+       if (end < 0) end = (EULER_SIZE + 1) + end;
        CLAMP(end, 0, EULER_SIZE);
        begin = MIN2(begin, end);
 
        CLAMP(end, 0, EULER_SIZE);
        begin = MIN2(begin, end);
 
-       if ((size=mathutils_array_parse(eul, 0, EULER_SIZE, seq, "mathutils.Euler[begin:end] = []")) == -1)
+       if ((size = mathutils_array_parse(eul, 0, EULER_SIZE, seq, "mathutils.Euler[begin:end] = []")) == -1)
                return -1;
 
        if (size != (end - begin)) {
                return -1;
 
        if (size != (end - begin)) {
@@ -453,7 +488,7 @@ static int Euler_ass_slice(EulerObject *self, int begin, int end, PyObject *seq)
                return -1;
        }
 
                return -1;
        }
 
-       for (i= 0; i < EULER_SIZE; i++)
+       for (i = 0; i < EULER_SIZE; i++)
                self->eul[begin + i] = eul[i];
 
        (void)BaseMath_WriteCallback(self);
                self->eul[begin + i] = eul[i];
 
        (void)BaseMath_WriteCallback(self);
@@ -474,7 +509,7 @@ static PyObject *Euler_subscript(EulerObject *self, PyObject *item)
        else if (PySlice_Check(item)) {
                Py_ssize_t start, stop, step, slicelength;
 
        else if (PySlice_Check(item)) {
                Py_ssize_t start, stop, step, slicelength;
 
-               if (PySlice_GetIndicesEx((void *)item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
+               if (PySlice_GetIndicesEx(item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
                        return NULL;
 
                if (slicelength <= 0) {
                        return NULL;
 
                if (slicelength <= 0) {
@@ -511,7 +546,7 @@ static int Euler_ass_subscript(EulerObject *self, PyObject *item, PyObject *valu
        else if (PySlice_Check(item)) {
                Py_ssize_t start, stop, step, slicelength;
 
        else if (PySlice_Check(item)) {
                Py_ssize_t start, stop, step, slicelength;
 
-               if (PySlice_GetIndicesEx((void *)item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
+               if (PySlice_GetIndicesEx(item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
                        return -1;
 
                if (step == 1)
                        return -1;
 
                if (step == 1)
@@ -530,18 +565,18 @@ static int Euler_ass_subscript(EulerObject *self, PyObject *item, PyObject *valu
        }
 }
 
        }
 }
 
-//-----------------PROTCOL DECLARATIONS--------------------------
+/* -----------------PROTCOL DECLARATIONS-------------------------- */
 static PySequenceMethods Euler_SeqMethods = {
 static PySequenceMethods Euler_SeqMethods = {
-       (lenfunc) Euler_len,                                    /* sq_length */
-       (binaryfunc) NULL,                                              /* sq_concat */
-       (ssizeargfunc) NULL,                                    /* sq_repeat */
-       (ssizeargfunc) Euler_item,                              /* sq_item */
-       (ssizessizeargfunc) NULL,                               /* sq_slice, deprecated  */
-       (ssizeobjargproc) Euler_ass_item,               /* sq_ass_item */
-       (ssizessizeobjargproc) NULL,                    /* sq_ass_slice, deprecated */
-       (objobjproc) NULL,                                              /* sq_contains */
-       (binaryfunc) NULL,                                              /* sq_inplace_concat */
-       (ssizeargfunc) NULL,                                    /* sq_inplace_repeat */
+       (lenfunc) Euler_len,                    /* sq_length */
+       (binaryfunc) NULL,                      /* sq_concat */
+       (ssizeargfunc) NULL,                    /* sq_repeat */
+       (ssizeargfunc) Euler_item,              /* sq_item */
+       (ssizessizeargfunc) NULL,               /* sq_slice, deprecated  */
+       (ssizeobjargproc) Euler_ass_item,       /* sq_ass_item */
+       (ssizessizeobjargproc) NULL,            /* sq_ass_slice, deprecated */
+       (objobjproc) NULL,                      /* sq_contains */
+       (binaryfunc) NULL,                      /* sq_inplace_concat */
+       (ssizeargfunc) NULL,                    /* sq_inplace_repeat */
 };
 
 static PyMappingMethods Euler_AsMapping = {
 };
 
 static PyMappingMethods Euler_AsMapping = {
@@ -550,21 +585,27 @@ static PyMappingMethods Euler_AsMapping = {
        (objobjargproc)Euler_ass_subscript
 };
 
        (objobjargproc)Euler_ass_subscript
 };
 
-/*
- * euler axis, euler.x/y/z
- */
-static PyObject *Euler_getAxis(EulerObject *self, void *type)
+/* euler axis, euler.x/y/z */
+
+PyDoc_STRVAR(Euler_axis_doc,
+"Euler axis angle in radians.\n\n:type: float"
+);
+static PyObject *Euler_axis_get(EulerObject *self, void *type)
 {
        return Euler_item(self, GET_INT_FROM_POINTER(type));
 }
 
 {
        return Euler_item(self, GET_INT_FROM_POINTER(type));
 }
 
-static int Euler_setAxis(EulerObject *self, PyObject *value, void *type)
+static int Euler_axis_set(EulerObject *self, PyObject *value, void *type)
 {
        return Euler_ass_item(self, GET_INT_FROM_POINTER(type), value);
 }
 
 /* rotation order */
 {
        return Euler_ass_item(self, GET_INT_FROM_POINTER(type), value);
 }
 
 /* rotation order */
-static PyObject *Euler_getOrder(EulerObject *self, void *UNUSED(closure))
+
+PyDoc_STRVAR(Euler_order_doc,
+"Euler rotation order.\n\n:type: string in ['XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX']"
+);
+static PyObject *Euler_order_get(EulerObject *self, void *UNUSED(closure))
 {
        if (BaseMath_ReadCallback(self) == -1) /* can read order too */
                return NULL;
 {
        if (BaseMath_ReadCallback(self) == -1) /* can read order too */
                return NULL;
@@ -572,15 +613,15 @@ static PyObject *Euler_getOrder(EulerObject *self, void *UNUSED(closure))
        return PyUnicode_FromString(euler_order_str(self));
 }
 
        return PyUnicode_FromString(euler_order_str(self));
 }
 
-static int Euler_setOrder(EulerObject *self, PyObject *value, void *UNUSED(closure))
+static int Euler_order_set(EulerObject *self, PyObject *value, void *UNUSED(closure))
 {
 {
-       const char *order_str= _PyUnicode_AsString(value);
-       short order= euler_order_from_string(order_str, "euler.order");
+       const char *order_str = _PyUnicode_AsString(value);
+       short order = euler_order_from_string(order_str, "euler.order");
 
        if (order == -1)
                return -1;
 
 
        if (order == -1)
                return -1;
 
-       self->order= order;
+       self->order = order;
        (void)BaseMath_WriteCallback(self); /* order can be written back */
        return 0;
 }
        (void)BaseMath_WriteCallback(self); /* order can be written back */
        return 0;
 }
@@ -589,18 +630,18 @@ static int Euler_setOrder(EulerObject *self, PyObject *value, void *UNUSED(closu
 /* Python attributes get/set structure:                                      */
 /*****************************************************************************/
 static PyGetSetDef Euler_getseters[] = {
 /* Python attributes get/set structure:                                      */
 /*****************************************************************************/
 static PyGetSetDef Euler_getseters[] = {
-       {(char *)"x", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler X axis in radians.\n\n:type: float", (void *)0},
-       {(char *)"y", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler Y axis in radians.\n\n:type: float", (void *)1},
-       {(char *)"z", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler Z axis in radians.\n\n:type: float", (void *)2},
-       {(char *)"order", (getter)Euler_getOrder, (setter)Euler_setOrder, (char *)"Euler rotation order.\n\n:type: string in ['XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX']", (void *)NULL},
+       {(char *)"x", (getter)Euler_axis_get, (setter)Euler_axis_set, Euler_axis_doc, (void *)0},
+       {(char *)"y", (getter)Euler_axis_get, (setter)Euler_axis_set, Euler_axis_doc, (void *)1},
+       {(char *)"z", (getter)Euler_axis_get, (setter)Euler_axis_set, Euler_axis_doc, (void *)2},
+       {(char *)"order", (getter)Euler_order_get, (setter)Euler_order_set, Euler_order_doc, (void *)NULL},
 
 
-       {(char *)"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, (char *)BaseMathObject_Wrapped_doc, NULL},
-       {(char *)"owner", (getter)BaseMathObject_getOwner, (setter)NULL, (char *)BaseMathObject_Owner_doc, NULL},
+       {(char *)"is_wrapped", (getter)BaseMathObject_is_wrapped_get, (setter)NULL, BaseMathObject_is_wrapped_doc, NULL},
+       {(char *)"owner", (getter)BaseMathObject_owner_get, (setter)NULL, BaseMathObject_owner_doc, NULL},
        {NULL, NULL, NULL, NULL, NULL}  /* Sentinel */
 };
 
 
        {NULL, NULL, NULL, NULL, NULL}  /* Sentinel */
 };
 
 
-//-----------------------METHOD DEFINITIONS ----------------------
+/* -----------------------METHOD DEFINITIONS ---------------------- */
 static struct PyMethodDef Euler_methods[] = {
        {"zero", (PyCFunction) Euler_zero, METH_NOARGS, Euler_zero_doc},
        {"to_matrix", (PyCFunction) Euler_to_matrix, METH_NOARGS, Euler_to_matrix_doc},
 static struct PyMethodDef Euler_methods[] = {
        {"zero", (PyCFunction) Euler_zero, METH_NOARGS, Euler_zero_doc},
        {"to_matrix", (PyCFunction) Euler_to_matrix, METH_NOARGS, Euler_to_matrix_doc},
@@ -608,114 +649,141 @@ static struct PyMethodDef Euler_methods[] = {
        {"rotate_axis", (PyCFunction) Euler_rotate_axis, METH_VARARGS, Euler_rotate_axis_doc},
        {"rotate", (PyCFunction) Euler_rotate, METH_O, Euler_rotate_doc},
        {"make_compatible", (PyCFunction) Euler_make_compatible, METH_O, Euler_make_compatible_doc},
        {"rotate_axis", (PyCFunction) Euler_rotate_axis, METH_VARARGS, Euler_rotate_axis_doc},
        {"rotate", (PyCFunction) Euler_rotate, METH_O, Euler_rotate_doc},
        {"make_compatible", (PyCFunction) Euler_make_compatible, METH_O, Euler_make_compatible_doc},
-       {"__copy__", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
        {"copy", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
        {"copy", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
+       {"__copy__", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
+       {"__deepcopy__", (PyCFunction) Euler_deepcopy, METH_VARARGS, Euler_copy_doc},
        {NULL, NULL, 0, NULL}
 };
 
        {NULL, NULL, 0, NULL}
 };
 
-//------------------PY_OBECT DEFINITION--------------------------
+/* ------------------PY_OBECT DEFINITION-------------------------- */
 PyDoc_STRVAR(euler_doc,
 "This object gives access to Eulers in Blender."
 );
 PyTypeObject euler_Type = {
        PyVarObject_HEAD_INIT(NULL, 0)
 PyDoc_STRVAR(euler_doc,
 "This object gives access to Eulers in Blender."
 );
 PyTypeObject euler_Type = {
        PyVarObject_HEAD_INIT(NULL, 0)
-       "mathutils.Euler",                              //tp_name
-       sizeof(EulerObject),                    //tp_basicsize
-       0,                                                              //tp_itemsize
-       (destructor)BaseMathObject_dealloc,             //tp_dealloc
-       NULL,                                                   //tp_print
-       NULL,                                                   //tp_getattr
-       NULL,                                                   //tp_setattr
-       NULL,                                                   //tp_compare
-       (reprfunc) Euler_repr,                  //tp_repr
-       NULL,                                                   //tp_as_number
-       &Euler_SeqMethods,                              //tp_as_sequence
-       &Euler_AsMapping,                               //tp_as_mapping
-       NULL,                                                   //tp_hash
-       NULL,                                                   //tp_call
-       NULL,                                                   //tp_str
-       NULL,                                                   //tp_getattro
-       NULL,                                                   //tp_setattro
-       NULL,                                                   //tp_as_buffer
-       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, //tp_flags
-       euler_doc, //tp_doc
-       (traverseproc)BaseMathObject_traverse,  //tp_traverse
-       (inquiry)BaseMathObject_clear,  //tp_clear
-       (richcmpfunc)Euler_richcmpr,    //tp_richcompare
-       0,                                                              //tp_weaklistoffset
-       NULL,                                                   //tp_iter
-       NULL,                                                   //tp_iternext
-       Euler_methods,                                  //tp_methods
-       NULL,                                                   //tp_members
-       Euler_getseters,                                //tp_getset
-       NULL,                                                   //tp_base
-       NULL,                                                   //tp_dict
-       NULL,                                                   //tp_descr_get
-       NULL,                                                   //tp_descr_set
-       0,                                                              //tp_dictoffset
-       NULL,                                                   //tp_init
-       NULL,                                                   //tp_alloc
-       Euler_new,                                              //tp_new
-       NULL,                                                   //tp_free
-       NULL,                                                   //tp_is_gc
-       NULL,                                                   //tp_bases
-       NULL,                                                   //tp_mro
-       NULL,                                                   //tp_cache
-       NULL,                                                   //tp_subclasses
-       NULL,                                                   //tp_weaklist
-       NULL                                                    //tp_del
+       "Euler",                        /* tp_name */
+       sizeof(EulerObject),            /* tp_basicsize */
+       0,                              /* tp_itemsize */
+       (destructor)BaseMathObject_dealloc,     /* tp_dealloc */
+       NULL,                           /* tp_print */
+       NULL,                           /* tp_getattr */
+       NULL,                           /* tp_setattr */
+       NULL,                           /* tp_compare */
+       (reprfunc) Euler_repr,          /* tp_repr */
+       NULL,                           /* tp_as_number */
+       &Euler_SeqMethods,              /* tp_as_sequence */
+       &Euler_AsMapping,               /* tp_as_mapping */
+       NULL,                           /* tp_hash */
+       NULL,                           /* tp_call */
+#ifndef MATH_STANDALONE
+       (reprfunc) Euler_str,           /* tp_str */
+#else
+       NULL,                           /* tp_str */
+#endif
+       NULL,                           /* tp_getattro */
+       NULL,                           /* tp_setattro */
+       NULL,                           /* tp_as_buffer */
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+       euler_doc, /* tp_doc */
+       (traverseproc)BaseMathObject_traverse,  /* tp_traverse */
+       (inquiry)BaseMathObject_clear,  /* tp_clear */
+       (richcmpfunc)Euler_richcmpr,    /* tp_richcompare */
+       0,                              /* tp_weaklistoffset */
+       NULL,                           /* tp_iter */
+       NULL,                           /* tp_iternext */
+       Euler_methods,                  /* tp_methods */
+       NULL,                           /* tp_members */
+       Euler_getseters,                /* tp_getset */
+       NULL,                           /* tp_base */
+       NULL,                           /* tp_dict */
+       NULL,                           /* tp_descr_get */
+       NULL,                           /* tp_descr_set */
+       0,                              /* tp_dictoffset */
+       NULL,                           /* tp_init */
+       NULL,                           /* tp_alloc */
+       Euler_new,                      /* tp_new */
+       NULL,                           /* tp_free */
+       NULL,                           /* tp_is_gc */
+       NULL,                           /* tp_bases */
+       NULL,                           /* tp_mro */
+       NULL,                           /* tp_cache */
+       NULL,                           /* tp_subclasses */
+       NULL,                           /* tp_weaklist */
+       NULL                            /* tp_del */
 };
 };
-//------------------------newEulerObject (internal)-------------
-//creates a new euler object
-/*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
- (i.e. it was allocated elsewhere by MEM_mallocN())
-  pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
- (i.e. it must be created here with PyMEM_malloc())*/
-PyObject *newEulerObject(float *eul, short order, int type, PyTypeObject *base_type)
+
+
+PyObject *Euler_CreatePyObject(
+        const float eul[3], const short order,
+        PyTypeObject *base_type)
 {
        EulerObject *self;
 {
        EulerObject *self;
+       float *eul_alloc;
 
 
-       self= base_type ?       (EulerObject *)base_type->tp_alloc(base_type, 0) :
-                                               (EulerObject *)PyObject_GC_New(EulerObject, &euler_Type);
+       eul_alloc = PyMem_Malloc(EULER_SIZE * sizeof(float));
+       if (UNLIKELY(eul_alloc == NULL)) {
+               PyErr_SetString(PyExc_MemoryError,
+                               "Euler(): "
+                               "problem allocating data");
+               return NULL;
+       }
 
 
+       self = BASE_MATH_NEW(EulerObject, euler_Type, base_type);
        if (self) {
        if (self) {
+               self->eul = eul_alloc;
+
                /* init callbacks as NULL */
                /* init callbacks as NULL */
-               self->cb_user= NULL;
-               self->cb_type= self->cb_subtype= 0;
+               self->cb_user = NULL;
+               self->cb_type = self->cb_subtype = 0;
 
 
-               if (type == Py_WRAP) {
-                       self->eul = eul;
-                       self->wrapped = Py_WRAP;
-               }
-               else if (type == Py_NEW) {
-                       self->eul = PyMem_Malloc(EULER_SIZE * sizeof(float));
-                       if (eul) {
-                               copy_v3_v3(self->eul, eul);
-                       }
-                       else {
-                               zero_v3(self->eul);
-                       }
-
-                       self->wrapped = Py_NEW;
+               if (eul) {
+                       copy_v3_v3(self->eul, eul);
                }
                else {
                }
                else {
-                       Py_FatalError("Euler(): invalid type!");
+                       zero_v3(self->eul);
                }
 
                }
 
-               self->order= order;
+               self->flag = BASE_MATH_FLAG_DEFAULT;
+               self->order = order;
+       }
+       else {
+               PyMem_Free(eul_alloc);
+       }
+
+       return (PyObject *)self;
+}
+
+PyObject *Euler_CreatePyObject_wrap(
+        float eul[3], const short order,
+        PyTypeObject *base_type)
+{
+       EulerObject *self;
+
+       self = BASE_MATH_NEW(EulerObject, euler_Type, base_type);
+       if (self) {
+               /* init callbacks as NULL */
+               self->cb_user = NULL;
+               self->cb_type = self->cb_subtype = 0;
+
+               self->eul = eul;
+               self->flag = BASE_MATH_FLAG_DEFAULT | BASE_MATH_FLAG_IS_WRAP;
+
+               self->order = order;
        }
 
        return (PyObject *)self;
 }
 
        }
 
        return (PyObject *)self;
 }
 
-PyObject *newEulerObject_cb(PyObject *cb_user, short order, int cb_type, int cb_subtype)
+PyObject *Euler_CreatePyObject_cb(
+        PyObject *cb_user, const short order,
+        unsigned char cb_type, unsigned char cb_subtype)
 {
 {
-       EulerObject *self= (EulerObject *)newEulerObject(NULL, order, Py_NEW, NULL);
+       EulerObject *self = (EulerObject *)Euler_CreatePyObject(NULL, order, NULL);
        if (self) {
                Py_INCREF(cb_user);
        if (self) {
                Py_INCREF(cb_user);
-               self->cb_user=                  cb_user;
-               self->cb_type=                  (unsigned char)cb_type;
-               self->cb_subtype=               (unsigned char)cb_subtype;
+               self->cb_user         = cb_user;
+               self->cb_type         = cb_type;
+               self->cb_subtype      = cb_subtype;
                PyObject_GC_Track(self);
        }
 
                PyObject_GC_Track(self);
        }