Skip to content

Commit

Permalink
Merge pull request pygame-community#2934 from pygame-community/ankith…
Browse files Browse the repository at this point in the history
…26-np-v2

Fix failing tests based on numpy v2
  • Loading branch information
zoldalma999 authored Jun 18, 2024
2 parents e14abb1 + 6746b44 commit 3ed89fe
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
8 changes: 4 additions & 4 deletions test/pixelcopy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def test_surface_to_array_3d(self):

def test_map_array(self):
try:
from numpy import array, zeros, uint8, int32, alltrue
from numpy import array, zeros, uint8, int32, all as np_all
except ImportError:
return

Expand All @@ -545,15 +545,15 @@ def test_map_array(self):
target = zeros((5, 7), int32)
map_array(target, color, surf)

self.assertTrue(alltrue(target == surf.map_rgb(color)))
self.assertTrue(np_all(target == surf.map_rgb(color)))

# array column stripes
stripe = array([[2, 5, 7], [11, 19, 23], [37, 53, 101]], uint8)
target = zeros((4, stripe.shape[0]), int32)
map_array(target, stripe, surf)
target_stripe = array([surf.map_rgb(c) for c in stripe], int32)

self.assertTrue(alltrue(target == target_stripe))
self.assertTrue(np_all(target == target_stripe))

# array row stripes
stripe = array(
Expand All @@ -563,7 +563,7 @@ def test_map_array(self):
map_array(target, stripe, surf)
target_stripe = array([[surf.map_rgb(c)] for c in stripe[:, 0]], int32)

self.assertTrue(alltrue(target == target_stripe))
self.assertTrue(np_all(target == target_stripe))

# mismatched shape
w = 4
Expand Down
14 changes: 7 additions & 7 deletions test/sndarray_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from numpy import int8, int16, uint8, uint16, float32, array, alltrue
from numpy import int8, int16, uint8, uint16, float32, array, all as np_all

import pygame
import pygame.sndarray
Expand Down Expand Up @@ -28,7 +28,7 @@ def check_array(size, channels, test_data):
arr = pygame.sndarray.array(snd)
self._assert_compatible(arr, size)
self.assertTrue(
alltrue(arr == srcarr),
np_all(arr == srcarr),
"size: %i\n%s\n%s" % (size, arr, test_data),
)
finally:
Expand All @@ -41,7 +41,7 @@ def check_array(size, channels, test_data):
16, 2, [[0, 0xFFFF], [0xFFFF, 0], [0x00FF, 0xFF00], [0x0F0F, 0xF0F0]]
)
check_array(-8, 1, [0, -0x80, 0x7F, 0x64])
check_array(-8, 2, [[0, -0x80], [-0x64, 0x64], [0x25, -0x50], [0xFF, 0]])
check_array(-8, 2, [[0, -0x80], [-0x64, 0x64], [0x25, -0x50], [0x7F, 0]])
check_array(-16, 1, [0, 0x7FFF, -0x7FFF, -1])
check_array(-16, 2, [[0, -0x7FFF], [-0x7FFF, 0], [0x7FFF, 0], [0, 0x7FFF]])

Expand Down Expand Up @@ -71,7 +71,7 @@ def check_sound(size, channels, test_data):
snd = pygame.sndarray.make_sound(srcarr)
arr = pygame.sndarray.samples(snd)
self.assertTrue(
alltrue(arr == srcarr),
np_all(arr == srcarr),
"size: %i\n%s\n%s" % (size, arr, test_data),
)
finally:
Expand All @@ -84,7 +84,7 @@ def check_sound(size, channels, test_data):
16, 2, [[0, 0xFFFF], [0xFFFF, 0], [0x00FF, 0xFF00], [0x0F0F, 0xF0F0]]
)
check_sound(-8, 1, [0, -0x80, 0x7F, 0x64])
check_sound(-8, 2, [[0, -0x80], [-0x64, 0x64], [0x25, -0x50], [0xFF, 0]])
check_sound(-8, 2, [[0, -0x80], [-0x64, 0x64], [0x25, -0x50], [0x7F, 0]])
check_sound(-16, 1, [0, 0x7FFF, -0x7FFF, -1])
check_sound(-16, 2, [[0, -0x7FFF], [-0x7FFF, 0], [0x7FFF, 0], [0, 0x7FFF]])
check_sound(32, 2, [[0.0, -1.0], [-1.0, 0], [1.0, 0], [0, 1.0]])
Expand All @@ -110,7 +110,7 @@ def check_sample(size, channels, test_data):
samples[...] = test_data
arr = pygame.sndarray.array(snd)
self.assertTrue(
alltrue(samples == arr),
np_all(samples == arr),
"size: %i\n%s\n%s" % (size, arr, test_data),
)
finally:
Expand All @@ -123,7 +123,7 @@ def check_sample(size, channels, test_data):
16, 2, [[0, 0xFFFF], [0xFFFF, 0], [0x00FF, 0xFF00], [0x0F0F, 0xF0F0]]
)
check_sample(-8, 1, [0, -0x80, 0x7F, 0x64])
check_sample(-8, 2, [[0, -0x80], [-0x64, 0x64], [0x25, -0x50], [0xFF, 0]])
check_sample(-8, 2, [[0, -0x80], [-0x64, 0x64], [0x25, -0x50], [0x7F, 0]])
check_sample(-16, 1, [0, 0x7FFF, -0x7FFF, -1])
check_sample(-16, 2, [[0, -0x7FFF], [-0x7FFF, 0], [0x7FFF, 0], [0, 0x7FFF]])
check_sample(32, 2, [[0.0, -1.0], [-1.0, 0], [1.0, 0], [0, 1.0]])
Expand Down
18 changes: 12 additions & 6 deletions test/surfarray_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
zeros,
float32,
float64,
alltrue,
all as np_all,
rint,
arange,
__version__ as np_version,
)

import pygame
Expand Down Expand Up @@ -235,15 +236,15 @@ def test_array_alpha(self):
),
)
else:
self.assertTrue(alltrue(arr == 255))
self.assertTrue(np_all(arr == 255))

# No per-pixel alpha when blanket alpha is None.
for surf in targets:
blanket_alpha = surf.get_alpha()
surf.set_alpha(None)
arr = pygame.surfarray.array_alpha(surf)
self.assertTrue(
alltrue(arr == 255),
np_all(arr == 255),
"All alpha values should be 255 when"
" surf.set_alpha(None) has been set."
" bitsize: %i, flags: %i" % (surf.get_bitsize(), surf.get_flags()),
Expand All @@ -257,12 +258,12 @@ def test_array_alpha(self):
arr = pygame.surfarray.array_alpha(surf)
if surf.get_masks()[3]:
self.assertFalse(
alltrue(arr == 255),
np_all(arr == 255),
"bitsize: %i, flags: %i" % (surf.get_bitsize(), surf.get_flags()),
)
else:
self.assertTrue(
alltrue(arr == 255),
np_all(arr == 255),
"bitsize: %i, flags: %i" % (surf.get_bitsize(), surf.get_flags()),
)
surf.set_alpha(blanket_alpha)
Expand Down Expand Up @@ -290,7 +291,7 @@ def test_array_colorkey(self):
p = [surf.unmap_rgb(surf.map_rgb(c)) for c in p]
surf.set_colorkey(None)
arr = pygame.surfarray.array_colorkey(surf)
self.assertTrue(alltrue(arr == 255))
self.assertTrue(np_all(arr == 255))

for i in range(1, len(palette)):
surf.set_colorkey(p[i])
Expand Down Expand Up @@ -560,6 +561,11 @@ def test_map_array(self):
self._make_array2d(uint8),
)

@unittest.skipIf(
int(np_version.split(".")[0]) >= 2,
"This test fails due to a change in numpy 2.0.0, and a 'proper fix' "
"requires an API/behaviour change",
)
def test_pixels2d(self):
sources = [
self._make_surface(8),
Expand Down

0 comments on commit 3ed89fe

Please sign in to comment.