Anonymous avatar Anonymous committed 47b2877

Added copy support to math.Vector class.

Comments (0)

Files changed (3)

       DOC_BASE_COLOR_NORMALIZE },
     { "correct_gamma", (PyCFunction) _color_correct_gamma, METH_O,
       DOC_BASE_COLOR_CORRECT_GAMMA },
-    { "__copy__", _color_copy, METH_NOARGS, NULL },
-    { "__deepcopy__", _color_copy, METH_O, NULL },
+    { "__copy__", (PyCFunction) _color_copy, METH_NOARGS, NULL },
+    { "__deepcopy__", (PyCFunction) _color_copy, METH_O, NULL },
     { NULL, NULL, 0, NULL }
 };
 

src/math/vector.c

 static PyObject* _vector_reflect_ip (PyVector *self, PyObject *args);
 static PyObject* _vector_distance (PyVector *self, PyObject *args);
 static PyObject* _vector_distance_squared (PyVector *self, PyObject *args);
+static PyObject* _vector_copy (PyObject *self, PyObject *unused);
 
 /* Generic math operations for vectors. */
 static PyObject* _vector_generic_math (PyObject *o1, PyObject *o2, int op);
       DOC_BASE_VECTOR_DISTANCE },
     { "distance_squared", (PyCFunction) _vector_distance_squared, METH_O,
       DOC_BASE_VECTOR_DISTANCE_SQUARED },
+    { "__copy__", (PyCFunction) _vector_copy, METH_NOARGS, NULL },
+    { "__deepcopy__", (PyCFunction) _vector_copy, METH_O, NULL },
     { NULL, NULL, 0, NULL },
 };
 
     Py_RETURN_NONE;
 }
 
+static PyObject*
+_vector_copy (PyObject *self, PyObject *unused)
+{
+    PyVector *selfv = (PyVector*) self;
+    PyVector *newv = (PyVector*) PyVector_New (selfv->dim);
+    
+    if (!newv)
+        return NULL;
+    newv->epsilon = selfv->epsilon;
+    memcpy (newv->coords, selfv->coords, selfv->dim * sizeof (double));
+    return (PyObject*) newv;
+}
+
+
 static double*
 _do_reflect (const double *srccoords, Py_ssize_t dim, double eps, PyObject *n)
 {

test/math_test.py

 except:
     import pgunittest as unittest
 
+import copy
 import math
 import pygame2
 import pygame2.math as pmath
 
 class MathTest (unittest.TestCase):
 
+    def test_pygame2_math_base_Vector_copy (self):
+        #
+        # Test the __copy__ implementation and copy constructor
+        #
+        v1 = Vector ((1, 2, 3))
+        self.assertEqual (v1.dimension, 3)
+
+        v2 = copy.copy (v1)
+        self.assertEqual (v1, v2)
+        v3 = copy.deepcopy (v1)
+        self.assertEqual (v1, v3)
+        v4 = Vector (v1)
+        self.assertEqual (v1, v4)
+
     def test_pygame2_math_base_vector_from_polar(self):
 
         # __doc__ (as of 2010-01-09) for pygame2.math.base.vector_from_polar:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.