Commits

Anonymous committed a708d9e

Added iter support to the PixelArray class.

Comments (0)

Files changed (3)

 # BUG    = fixed a bug that was (or could have been) crashing
 #
 #
+Apr 3, 2008
+    Added iter support to PixelArray.
+
+Apr 2, 2008
+    [BUG] Fixed mask settings for 8-bit surfaces created by
+        surfarray.make_surface()
+    [BUG] Fixed integer color value assignment bound checking in PixelArray.
 
 Mar 30, 2008
     Added subscript support to PixelArray and PixelArray.make_surface()
 static int _pxarray_ass_slice (PyPixelArray *array, Py_ssize_t low,
     Py_ssize_t high, PyObject *value);
 static int _pxarray_contains (PyPixelArray *array, PyObject *value);
+static PyObject* _pxarray_iter (PyPixelArray *array);
 
 /* Mapping methods */
 static int _get_subslice (PyObject *op, Py_ssize_t length, Py_ssize_t *start,
     0,                          /* tp_getattr */
     0,                          /* tp_setattr */
     0,                          /* tp_compare */
-    (reprfunc) &_pxarray_repr,  /* tp_repr */
+    (reprfunc) _pxarray_repr,  /* tp_repr */
     0,                          /* tp_as_number */
-    &_pxarray_sequence,         /* tp_as_sequence */
-    &_pxarray_mapping,          /* tp_as_mapping */
+    &_pxarray_sequence,          /* tp_as_sequence */
+    &_pxarray_mapping,           /* tp_as_mapping */
     0,                          /* tp_hash */
     0,                          /* tp_call */
     0,                          /* tp_str */
     0,                          /* tp_clear */
     0,                          /* tp_richcompare */
     offsetof (PyPixelArray, weakrefs),  /* tp_weaklistoffset */
-    0,                          /* tp_iter */
+    (getiterfunc) _pxarray_iter, /* tp_iter */
     0,                          /* tp_iternext */
     _pxarray_methods,           /* tp_methods */
     0,                          /* tp_members */
 {
     if (self->weakrefs)
         PyObject_ClearWeakRefs ((PyObject *) self);
-    Py_DECREF (self->lock);
-    Py_DECREF (self->surface);
+    Py_XDECREF (self->lock);
     Py_XDECREF (self->parent);
     Py_XDECREF (self->dict);
-
+    Py_DECREF (self->surface);
     self->ob_type->tp_free ((PyObject *) self);
 }
 
         ystep = _step;
         xstep = array->xstep;
         padding = array->padding;
+
+        /* Out of bounds? */
+        if (_start >= (Sint32) array->ylen && ystep > 0)
+            return RAISE (PyExc_IndexError, "array index out of range");
     }
     else
     {
         xstep = _step;
         ystep = array->ystep;
         padding = array->padding;
+
+        /* Out of bounds? */
+        if (_start >= (Sint32) array->xlen && xstep > 0)
+            return RAISE (PyExc_IndexError, "array index out of range");
     }
 
 /*
      /* Access of a single column. */
     if (array->xlen == 1)
     {
+        if ((Uint32) _index >= array->ystart + array->ylen)
+            return RAISE (PyExc_IndexError, "array index out of range");
+
         return _get_single_pixel ((Uint8 *) surface->pixels, bpp,
             array->xstart, _index * array->padding * array->ystep);
     }
     if (array->ylen == 1)
     {
+        if ((Uint32) _index >= array->xstart + array->xlen)
+            return RAISE (PyExc_IndexError, "array index out of range");
+
         return _get_single_pixel ((Uint8 *) surface->pixels, bpp,
             array->xstart + _index * array->xstep,
             array->ystart * array->padding * array->ystep);
     }
+
     return _array_slice_internal (array, _index, _index + 1, 1);
 }
 
     return 0;
 }
 
+/**
+ * iter (arrray), for x in array
+ */
+static PyObject*
+_pxarray_iter (PyPixelArray *array)
+{
+/*
+    printf ("Iter ARRAY: %d:%d:%d %d:%d:%d\n",
+        array->xstart, array->xlen, array->xstep,
+        array->ystart, array->ylen, array->ystep);
+*/
+    return PySeqIter_New ((PyObject *) array);
+}
+
 /**** Mapping interfaces ****/
 
 /**

test/pixelarray_test.py

             self.assertEqual (rect.width, 5)
             self.assertEqual (rect.height, 10)
 
+    def test_iter (self):
+        for bpp in (8, 16, 24, 32):
+            sf = pygame.Surface ((5, 10), 0, bpp)
+            ar = pygame.PixelArray (sf)
+            iterations = 0
+            for col in ar:
+                self.assertEqual (len (col), 10)
+                iterations += 1
+            self.assertEqual (iterations, 5)
+
 if __name__ == '__main__':
     unittest.main()