From 875ed65e44e9cb2134a3b08cb999c2d53ed28f12 Mon Sep 17 00:00:00 2001 From: Ankith Date: Mon, 17 Jun 2024 15:09:46 +0530 Subject: [PATCH 1/3] Replace np.alltrue with np.all for np v2 support --- test/pixelcopy_test.py | 8 ++++---- test/sndarray_test.py | 8 ++++---- test/surfarray_test.py | 12 ++++++------ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/test/pixelcopy_test.py b/test/pixelcopy_test.py index b0541952b9..662918d120 100644 --- a/test/pixelcopy_test.py +++ b/test/pixelcopy_test.py @@ -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 @@ -545,7 +545,7 @@ 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) @@ -553,7 +553,7 @@ def test_map_array(self): 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( @@ -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 diff --git a/test/sndarray_test.py b/test/sndarray_test.py index 5b624caf41..38a1dfd0e6 100644 --- a/test/sndarray_test.py +++ b/test/sndarray_test.py @@ -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 @@ -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: @@ -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: @@ -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: diff --git a/test/surfarray_test.py b/test/surfarray_test.py index b27dc0b460..0c4d46b288 100644 --- a/test/surfarray_test.py +++ b/test/surfarray_test.py @@ -9,7 +9,7 @@ zeros, float32, float64, - alltrue, + all as np_all, rint, arange, ) @@ -235,7 +235,7 @@ 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: @@ -243,7 +243,7 @@ def test_array_alpha(self): 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()), @@ -257,12 +257,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) @@ -290,7 +290,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]) From 98cbad16a182dece08e9b1465c570da3bd3e269a Mon Sep 17 00:00:00 2001 From: Ankith Date: Mon, 17 Jun 2024 17:01:26 +0530 Subject: [PATCH 2/3] Fix some incorrect testcases in sndarray --- test/sndarray_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/sndarray_test.py b/test/sndarray_test.py index 38a1dfd0e6..a99171fc5a 100644 --- a/test/sndarray_test.py +++ b/test/sndarray_test.py @@ -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]]) @@ -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]]) @@ -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]]) From 6746b441e88102aaf0232341d62e42d978e37269 Mon Sep 17 00:00:00 2001 From: Ankith Date: Tue, 18 Jun 2024 13:19:51 +0530 Subject: [PATCH 3/3] Skip test_pixels2d on numpy 2+ for now --- test/surfarray_test.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/surfarray_test.py b/test/surfarray_test.py index 0c4d46b288..2288f9b862 100644 --- a/test/surfarray_test.py +++ b/test/surfarray_test.py @@ -12,6 +12,7 @@ all as np_all, rint, arange, + __version__ as np_version, ) import pygame @@ -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),