Skip to content

Commit 58f02cf

Browse files
authored
Merge pull request #3538 from Starbuck5/fix-tests-for-314
Fix refcount tests for Python 3.14
2 parents 57da6dc + 3f62707 commit 58f02cf

File tree

3 files changed

+45
-49
lines changed

3 files changed

+45
-49
lines changed

test/freetype_test.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import pathlib
66
import platform
7+
import sys
78
import unittest
89
import weakref
910

@@ -1611,16 +1612,16 @@ def ref_items(seq):
16111612
else:
16121613
array = arrinter.Array(rect.size, "u", 1)
16131614
o = font.render_raw(text)
1614-
self.assertEqual(getrefcount(o), 2)
1615+
self.assertIn(getrefcount(o), (1, 2))
16151616
self.assertEqual(getrefcount(o[0]), 2)
16161617
self.assertEqual(getrefcount(o[1]), 2)
16171618
self.assertEqual(getrefcount(font.render_raw_to(array, text)), 1)
16181619
o = font.get_metrics("AB")
1619-
self.assertEqual(getrefcount(o), 2)
1620+
self.assertIn(getrefcount(o), (1, 2))
16201621
for i in range(len(o)):
16211622
self.assertEqual(getrefcount(o[i]), 2, "refcount fail for item %d" % i)
16221623
o = font.get_sizes()
1623-
self.assertEqual(getrefcount(o), 2)
1624+
self.assertIn(getrefcount(o), (1, 2))
16241625
for i in range(len(o)):
16251626
self.assertEqual(getrefcount(o[i]), 2, "refcount fail for item %d" % i)
16261627

test/mask_test.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,7 +2569,7 @@ def test_get_bounding_rects(self):
25692569
@unittest.skipIf(IS_PYPY, "Segfaults on pypy")
25702570
def test_to_surface(self):
25712571
"""Ensures empty and full masks can be drawn onto surfaces."""
2572-
expected_ref_count = 3
2572+
expected_ref_count = (2, 3)
25732573
size = (33, 65)
25742574
surface = pygame.Surface(size, SRCALPHA, 32)
25752575
surface_color = pygame.Color("red")
@@ -2583,13 +2583,13 @@ def test_to_surface(self):
25832583

25842584
self.assertIs(to_surface, surface)
25852585
if not IS_PYPY:
2586-
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
2586+
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
25872587
self.assertEqual(to_surface.get_size(), size)
25882588
assertSurfaceFilled(self, to_surface, expected_color)
25892589

25902590
def test_to_surface__create_surface(self):
25912591
"""Ensures empty and full masks can be drawn onto a created surface."""
2592-
expected_ref_count = 2
2592+
expected_ref_count = (1, 2)
25932593
expected_flag = SRCALPHA
25942594
expected_depth = 32
25952595
size = (33, 65)
@@ -2606,15 +2606,15 @@ def test_to_surface__create_surface(self):
26062606

26072607
self.assertIsInstance(to_surface, pygame.Surface)
26082608
if not IS_PYPY:
2609-
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
2609+
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
26102610
self.assertTrue(to_surface.get_flags() & expected_flag)
26112611
self.assertEqual(to_surface.get_bitsize(), expected_depth)
26122612
self.assertEqual(to_surface.get_size(), size)
26132613
assertSurfaceFilled(self, to_surface, expected_color)
26142614

26152615
def test_to_surface__surface_param(self):
26162616
"""Ensures to_surface accepts a surface arg/kwarg."""
2617-
expected_ref_count = 4
2617+
expected_ref_count = (3, 4)
26182618
expected_color = pygame.Color("white")
26192619
surface_color = pygame.Color("red")
26202620
size = (5, 3)
@@ -2632,13 +2632,13 @@ def test_to_surface__surface_param(self):
26322632

26332633
self.assertIs(to_surface, surface)
26342634
if not IS_PYPY:
2635-
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
2635+
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
26362636
self.assertEqual(to_surface.get_size(), size)
26372637
assertSurfaceFilled(self, to_surface, expected_color)
26382638

26392639
def test_to_surface__setsurface_param(self):
26402640
"""Ensures to_surface accepts a setsurface arg/kwarg."""
2641-
expected_ref_count = 2
2641+
expected_ref_count = (1, 2)
26422642
expected_flag = SRCALPHA
26432643
expected_depth = 32
26442644
expected_color = pygame.Color("red")
@@ -2657,15 +2657,15 @@ def test_to_surface__setsurface_param(self):
26572657
self.assertIsInstance(to_surface, pygame.Surface)
26582658

26592659
if not IS_PYPY:
2660-
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
2660+
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
26612661
self.assertTrue(to_surface.get_flags() & expected_flag)
26622662
self.assertEqual(to_surface.get_bitsize(), expected_depth)
26632663
self.assertEqual(to_surface.get_size(), size)
26642664
assertSurfaceFilled(self, to_surface, expected_color)
26652665

26662666
def test_to_surface__unsetsurface_param(self):
26672667
"""Ensures to_surface accepts a unsetsurface arg/kwarg."""
2668-
expected_ref_count = 2
2668+
expected_ref_count = (1, 2)
26692669
expected_flag = SRCALPHA
26702670
expected_depth = 32
26712671
expected_color = pygame.Color("red")
@@ -2683,15 +2683,15 @@ def test_to_surface__unsetsurface_param(self):
26832683

26842684
self.assertIsInstance(to_surface, pygame.Surface)
26852685
if not IS_PYPY:
2686-
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
2686+
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
26872687
self.assertTrue(to_surface.get_flags() & expected_flag)
26882688
self.assertEqual(to_surface.get_bitsize(), expected_depth)
26892689
self.assertEqual(to_surface.get_size(), size)
26902690
assertSurfaceFilled(self, to_surface, expected_color)
26912691

26922692
def test_to_surface__setcolor_param(self):
26932693
"""Ensures to_surface accepts a setcolor arg/kwarg."""
2694-
expected_ref_count = 2
2694+
expected_ref_count = (1, 2)
26952695
expected_flag = SRCALPHA
26962696
expected_depth = 32
26972697
expected_color = pygame.Color("red")
@@ -2707,7 +2707,7 @@ def test_to_surface__setcolor_param(self):
27072707

27082708
self.assertIsInstance(to_surface, pygame.Surface)
27092709
if not IS_PYPY:
2710-
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
2710+
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
27112711
self.assertTrue(to_surface.get_flags() & expected_flag)
27122712
self.assertEqual(to_surface.get_bitsize(), expected_depth)
27132713
self.assertEqual(to_surface.get_size(), size)
@@ -2728,7 +2728,7 @@ def test_to_surface__setcolor_default(self):
27282728

27292729
def test_to_surface__unsetcolor_param(self):
27302730
"""Ensures to_surface accepts an unsetcolor arg/kwarg."""
2731-
expected_ref_count = 2
2731+
expected_ref_count = (1, 2)
27322732
expected_flag = SRCALPHA
27332733
expected_depth = 32
27342734
expected_color = pygame.Color("red")
@@ -2746,7 +2746,7 @@ def test_to_surface__unsetcolor_param(self):
27462746

27472747
self.assertIsInstance(to_surface, pygame.Surface)
27482748
if not IS_PYPY:
2749-
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
2749+
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
27502750
self.assertTrue(to_surface.get_flags() & expected_flag)
27512751
self.assertEqual(to_surface.get_bitsize(), expected_depth)
27522752
self.assertEqual(to_surface.get_size(), size)
@@ -2767,7 +2767,7 @@ def test_to_surface__unsetcolor_default(self):
27672767

27682768
def test_to_surface__dest_param(self):
27692769
"""Ensures to_surface accepts a dest arg/kwarg."""
2770-
expected_ref_count = 2
2770+
expected_ref_count = (1, 2)
27712771
expected_flag = SRCALPHA
27722772
expected_depth = 32
27732773
default_surface_color = (0, 0, 0, 0)
@@ -2791,7 +2791,7 @@ def test_to_surface__dest_param(self):
27912791

27922792
self.assertIsInstance(to_surface, pygame.Surface)
27932793
if not IS_PYPY:
2794-
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
2794+
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
27952795
self.assertTrue(to_surface.get_flags() & expected_flag)
27962796
self.assertEqual(to_surface.get_bitsize(), expected_depth)
27972797
self.assertEqual(to_surface.get_size(), size)
@@ -2822,7 +2822,7 @@ def test_to_surface__dest_default(self):
28222822

28232823
def test_to_surface__area_param(self):
28242824
"""Ensures to_surface accepts an area arg/kwarg."""
2825-
expected_ref_count = 2
2825+
expected_ref_count = (1, 2)
28262826
expected_flag = SRCALPHA
28272827
expected_depth = 32
28282828
default_surface_color = (0, 0, 0, 0)
@@ -2845,7 +2845,7 @@ def test_to_surface__area_param(self):
28452845

28462846
self.assertIsInstance(to_surface, pygame.Surface)
28472847
if not IS_PYPY:
2848-
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
2848+
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
28492849
self.assertTrue(to_surface.get_flags() & expected_flag)
28502850
self.assertEqual(to_surface.get_bitsize(), expected_depth)
28512851
self.assertEqual(to_surface.get_size(), size)
@@ -3594,7 +3594,7 @@ def test_to_surface__default_surface_with_param_combinations(self):
35943594
This tests many parameter combinations with full and empty
35953595
masks.
35963596
"""
3597-
expected_ref_count = 2
3597+
expected_ref_count = (1, 2)
35983598
expected_flag = SRCALPHA
35993599
expected_depth = 32
36003600
size = (5, 3)
@@ -3661,7 +3661,7 @@ def test_to_surface__default_surface_with_param_combinations(self):
36613661

36623662
self.assertIsInstance(to_surface, pygame.Surface)
36633663
if not IS_PYPY:
3664-
self.assertEqual(
3664+
self.assertIn(
36653665
sys.getrefcount(to_surface), expected_ref_count
36663666
)
36673667
self.assertTrue(to_surface.get_flags() & expected_flag)
@@ -3678,7 +3678,7 @@ def test_to_surface__surface_with_param_combinations(self):
36783678
This tests many parameter combinations with full and empty
36793679
masks.
36803680
"""
3681-
expected_ref_count = 4
3681+
expected_ref_count = (3, 4)
36823682
expected_flag = SRCALPHA
36833683
expected_depth = 32
36843684
size = (5, 3)
@@ -3748,7 +3748,7 @@ def test_to_surface__surface_with_param_combinations(self):
37483748

37493749
self.assertIs(to_surface, surface)
37503750
if not IS_PYPY:
3751-
self.assertEqual(
3751+
self.assertIn(
37523752
sys.getrefcount(to_surface), expected_ref_count
37533753
)
37543754
self.assertTrue(to_surface.get_flags() & expected_flag)
@@ -5529,7 +5529,7 @@ def test_to_surface__area_off_mask_with_setsurface_unsetsurface(self):
55295529

55305530
def test_to_surface__surface_with_zero_size(self):
55315531
"""Ensures zero sized surfaces are handled correctly."""
5532-
expected_ref_count = 3
5532+
expected_ref_count = (2, 3)
55335533
size = (0, 0)
55345534
surface = pygame.Surface(size)
55355535
mask = pygame.mask.Mask((3, 4), fill=True)
@@ -5538,12 +5538,12 @@ def test_to_surface__surface_with_zero_size(self):
55385538

55395539
self.assertIs(to_surface, surface)
55405540
if not IS_PYPY:
5541-
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
5541+
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
55425542
self.assertEqual(to_surface.get_size(), size)
55435543

55445544
def test_to_surface__setsurface_with_zero_size(self):
55455545
"""Ensures zero sized setsurfaces are handled correctly."""
5546-
expected_ref_count = 2
5546+
expected_ref_count = (1, 2)
55475547
expected_flag = SRCALPHA
55485548
expected_depth = 32
55495549
expected_color = pygame.Color("white") # Default setcolor.
@@ -5555,15 +5555,15 @@ def test_to_surface__setsurface_with_zero_size(self):
55555555

55565556
self.assertIsInstance(to_surface, pygame.Surface)
55575557
if not IS_PYPY:
5558-
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
5558+
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
55595559
self.assertTrue(to_surface.get_flags() & expected_flag)
55605560
self.assertEqual(to_surface.get_bitsize(), expected_depth)
55615561
self.assertEqual(to_surface.get_size(), mask_size)
55625562
assertSurfaceFilled(self, to_surface, expected_color)
55635563

55645564
def test_to_surface__unsetsurface_with_zero_size(self):
55655565
"""Ensures zero sized unsetsurfaces are handled correctly."""
5566-
expected_ref_count = 2
5566+
expected_ref_count = (1, 2)
55675567
expected_flag = SRCALPHA
55685568
expected_depth = 32
55695569
expected_color = pygame.Color("black") # Default unsetcolor.
@@ -5575,7 +5575,7 @@ def test_to_surface__unsetsurface_with_zero_size(self):
55755575

55765576
self.assertIsInstance(to_surface, pygame.Surface)
55775577
if not IS_PYPY:
5578-
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
5578+
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
55795579
self.assertTrue(to_surface.get_flags() & expected_flag)
55805580
self.assertEqual(to_surface.get_bitsize(), expected_depth)
55815581
self.assertEqual(to_surface.get_size(), mask_size)

test/rwobject_test.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import pathlib
2+
import platform
3+
import sys
24
import unittest
35

46
from pygame import encode_file_path, encode_string
57

8+
IS_PYPY = "PyPy" == platform.python_implementation()
69

7-
class RWopsEncodeStringTest(unittest.TestCase):
8-
global getrefcount
910

11+
class RWopsEncodeStringTest(unittest.TestCase):
1012
def test_obj_None(self):
1113
encoded_string = encode_string(None)
1214

@@ -68,22 +70,15 @@ def test_string_with_null_bytes(self):
6870
self.assertIs(encoded_string, b)
6971
self.assertEqual(encoded_decode_string, b)
7072

71-
try:
72-
from sys import getrefcount as _g
73-
74-
getrefcount = _g # This nonsense is for Python 3.x
75-
except ImportError:
76-
pass
77-
else:
78-
79-
def test_refcount(self):
80-
bpath = b" This is a string that is not cached."[1:]
81-
upath = bpath.decode("ascii")
82-
before = getrefcount(bpath)
83-
bpath = encode_string(bpath)
84-
self.assertEqual(getrefcount(bpath), before)
85-
bpath = encode_string(upath)
86-
self.assertEqual(getrefcount(bpath), before)
73+
@unittest.skipIf(IS_PYPY, "getrefcount not available on pypy")
74+
def test_refcount(self):
75+
bpath = b" This is a string that is not cached."[1:]
76+
upath = bpath.decode("ascii")
77+
before = sys.getrefcount(bpath)
78+
bpath = encode_string(bpath)
79+
self.assertEqual(sys.getrefcount(bpath), before)
80+
bpath = encode_string(upath)
81+
self.assertIn(sys.getrefcount(bpath), (before, before - 1))
8782

8883
def test_smp(self):
8984
utf_8 = b"a\xf0\x93\x82\xa7b"

0 commit comments

Comments
 (0)