Skip to content

2D PixelArrays with height of 0 incorrectly marked as 1D #3986

Description

@aatle

A 2D PixelArray with the second dimension equal to 0 should have len(array.shape) == array.ndim == 2.
Instead, it has len(array.shape) == array.ndim == 1; operations treat it as a 1D array.

This behavior does not happen for the first dimension (width) being equal to 0.

from pygame import Surface, PixelArray

surface = Surface((10, 20))
array = PixelArray(surface)

assert array.shape == (10, 20)
assert array[0:0, :].shape == (0, 20)
assert array[:, 0:0].shape == (10, 0)  # AssertionError - actual shape is (10,), ndim is 1

This distinction is not trivial; the implicit 'decay' of a 2D array into a 1D one can cause unexpected behavior.

Internally, PixelArrays do not store ndim, and a height (dim1) of 0 is currently the marker for a 1D array.
Suggestion: use an actually invalid sentinel value such as -1 to mark a nonexistent height for a 1D array, instead of 0.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    PixelArraypygame.PixelArraybugNot working as intended

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions