1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
From: =?utf-8?q?Miro_Hron=C4=8Dok?= <miro@hroncok.cz>
Date: Sun, 26 Oct 2025 00:17:32 +0100
Subject: Relax all getrefcount tests to allow lower numbers
This is related to https://docs.python.org/3.14/whatsnew/3.14.html#whatsnew314-refcount
Created via:
sed -Ei 's/sys\.getrefcount\(([^\)]+)\) == ([0-9]+)/sys.getrefcount(\1) <= \2/' tests/test_*.py
Perhaps this is too strong a hammer, but it makes the tests pass on 3.14.
Origin: backport, https://github.com/jcrist/msgspec/pull/854
Bug-Debian: https://bugs.debian.org/1117910
Last-Update: 2025-10-26
---
tests/test_common.py | 26 +++++++++++++-------------
tests/test_convert.py | 18 +++++++++---------
tests/test_json.py | 4 ++--
tests/test_msgpack.py | 10 +++++-----
tests/test_struct.py | 10 +++++-----
5 files changed, 34 insertions(+), 34 deletions(-)
diff --git a/tests/test_common.py b/tests/test_common.py
index 38898be..1012794 100644
--- a/tests/test_common.py
+++ b/tests/test_common.py
@@ -1370,14 +1370,14 @@ class TestGenericStruct:
dec = proto.Decoder(typ)
info = typ.__msgspec_cache__
assert info is not None
- assert sys.getrefcount(info) == 4 # info + attr + decoder + func call
+ assert sys.getrefcount(info) <= 4 # info + attr + decoder + func call
dec2 = proto.Decoder(typ)
assert typ.__msgspec_cache__ is info
- assert sys.getrefcount(info) == 5
+ assert sys.getrefcount(info) <= 5
del dec
del dec2
- assert sys.getrefcount(info) == 3
+ assert sys.getrefcount(info) <= 3
def test_generic_struct_invalid_types_not_cached(self, proto):
class Ex(Struct, Generic[T]):
@@ -1545,7 +1545,7 @@ class TestStructPostInit:
res = proto.decode(buf, type=typ)
assert res == msg
assert count == 2 # 1 for Ex(), 1 for decode
- assert sys.getrefcount(singleton) == 2 # 1 for ref, 1 for call
+ assert sys.getrefcount(singleton) <= 2 # 1 for ref, 1 for call
@pytest.mark.parametrize("array_like", [False, True])
@pytest.mark.parametrize("union", [False, True])
@@ -1606,14 +1606,14 @@ class TestGenericDataclassOrAttrs:
dec = proto.Decoder(typ)
info = typ.__msgspec_cache__
assert info is not None
- assert sys.getrefcount(info) == 4 # info + attr + decoder + func call
+ assert sys.getrefcount(info) <= 4 # info + attr + decoder + func call
dec2 = proto.Decoder(typ)
assert typ.__msgspec_cache__ is info
- assert sys.getrefcount(info) == 5
+ assert sys.getrefcount(info) <= 5
del dec
del dec2
- assert sys.getrefcount(info) == 3
+ assert sys.getrefcount(info) <= 3
def test_generic_invalid_types_not_cached(self, decorator, proto):
@decorator
@@ -2179,14 +2179,14 @@ class TestTypedDict:
dec = proto.Decoder(typ)
info = typ.__msgspec_cache__
assert info is not None
- assert sys.getrefcount(info) == 4 # info + attr + decoder + func call
+ assert sys.getrefcount(info) <= 4 # info + attr + decoder + func call
dec2 = proto.Decoder(typ)
assert typ.__msgspec_cache__ is info
- assert sys.getrefcount(info) == 5
+ assert sys.getrefcount(info) <= 5
del dec
del dec2
- assert sys.getrefcount(info) == 3
+ assert sys.getrefcount(info) <= 3
def test_generic_typeddict_invalid_types_not_cached(self, proto):
TypedDict = pytest.importorskip("typing_extensions").TypedDict
@@ -2398,14 +2398,14 @@ class TestNamedTuple:
dec = proto.Decoder(typ)
info = typ.__msgspec_cache__
assert info is not None
- assert sys.getrefcount(info) == 4 # info + attr + decoder + func call
+ assert sys.getrefcount(info) <= 4 # info + attr + decoder + func call
dec2 = proto.Decoder(typ)
assert typ.__msgspec_cache__ is info
- assert sys.getrefcount(info) == 5
+ assert sys.getrefcount(info) <= 5
del dec
del dec2
- assert sys.getrefcount(info) == 3
+ assert sys.getrefcount(info) <= 3
def test_generic_namedtuple_invalid_types_not_cached(self, proto):
NamedTuple = pytest.importorskip("typing_extensions").NamedTuple
diff --git a/tests/test_convert.py b/tests/test_convert.py
index da1b664..afb668e 100644
--- a/tests/test_convert.py
+++ b/tests/test_convert.py
@@ -220,7 +220,7 @@ class TestConvert:
x = Custom()
res = convert(x, Any)
assert res is x
- assert sys.getrefcount(x) == 3 # x + res + 1
+ assert sys.getrefcount(x) <= 3 # x + res + 1
def test_custom_input_type_works_with_custom(self):
class Custom:
@@ -229,7 +229,7 @@ class TestConvert:
x = Custom()
res = convert(x, Custom)
assert res is x
- assert sys.getrefcount(x) == 3 # x + res + 1
+ assert sys.getrefcount(x) <= 3 # x + res + 1
def test_custom_input_type_works_with_dec_hook(self):
class Custom:
@@ -247,8 +247,8 @@ class TestConvert:
x = Custom()
res = convert(x, Custom2, dec_hook=dec_hook)
assert isinstance(res, Custom2)
- assert sys.getrefcount(res) == 2 # res + 1
- assert sys.getrefcount(x) == 2 # x + 1
+ assert sys.getrefcount(res) <= 2 # res + 1
+ assert sys.getrefcount(x) <= 2 # x + 1
def test_unsupported_output_type(self):
with pytest.raises(TypeError, match="more than one array-like"):
@@ -397,7 +397,7 @@ class TestInt:
x = MyInt(100)
sol = convert(x, MyInt)
assert sol is x
- assert sys.getrefcount(x) == 3 # x + sol + 1
+ assert sys.getrefcount(x) <= 3 # x + sol + 1
class TestFloat:
@@ -535,10 +535,10 @@ class TestBinary:
del sol
- assert sys.getrefcount(msg) == 2 # msg + 1
+ assert sys.getrefcount(msg) <= 2 # msg + 1
sol = convert(msg, MyBytes)
assert sol is msg
- assert sys.getrefcount(msg) == 3 # msg + sol + 1
+ assert sys.getrefcount(msg) <= 3 # msg + sol + 1
class TestDateTime:
@@ -828,7 +828,7 @@ class TestEnum:
msg = MyInt(1)
assert convert(msg, Ex) is Ex.x
- assert sys.getrefcount(msg) == 2 # msg + 1
+ assert sys.getrefcount(msg) <= 2 # msg + 1
assert convert(MyInt(2), Ex) is Ex.y
def test_enum_missing(self):
@@ -2223,7 +2223,7 @@ class TestStructPostInit:
res = convert(msg, type=typ, from_attributes=from_attributes)
assert type(res) is Ex
assert called
- assert sys.getrefcount(singleton) == 2 # 1 for ref, 1 for call
+ assert sys.getrefcount(singleton) <= 2 # 1 for ref, 1 for call
@pytest.mark.parametrize("union", [False, True])
@pytest.mark.parametrize("exc_class", [ValueError, TypeError, OSError])
diff --git a/tests/test_json.py b/tests/test_json.py
index 1d3776e..f3a562e 100644
--- a/tests/test_json.py
+++ b/tests/test_json.py
@@ -898,7 +898,7 @@ class TestDatetime:
tz2 = msgspec.json.decode(msg, type=datetime.datetime).tzinfo
assert tz is tz2
del tz2
- assert sys.getrefcount(tz) == 3 # 1 tz, 1 cache, 1 func call
+ assert sys.getrefcount(tz) <= 3 # 1 tz, 1 cache, 1 func call
for _ in range(10):
gc.collect() # cache is cleared every 10 full collections
@@ -2293,7 +2293,7 @@ class TestStruct:
assert x == Person("harry", "potter", 13, False)
# one for struct, one for output of getattr, and one for getrefcount
- assert sys.getrefcount(x.first) == 3
+ assert sys.getrefcount(x.first) <= 3
with pytest.raises(
msgspec.ValidationError, match="Expected `object`, got `int`"
diff --git a/tests/test_msgpack.py b/tests/test_msgpack.py
index 55db40f..e3d30d0 100644
--- a/tests/test_msgpack.py
+++ b/tests/test_msgpack.py
@@ -684,13 +684,13 @@ class TestTypedDecoder:
assert isinstance(res, memoryview)
assert bytes(res) == b"abcde"
if input_type is memoryview:
- assert sys.getrefcount(ref) == 3
+ assert sys.getrefcount(ref) <= 3
del msg
- assert sys.getrefcount(ref) == 3
+ assert sys.getrefcount(ref) <= 3
del res
- assert sys.getrefcount(ref) == 2
+ assert sys.getrefcount(ref) <= 2
elif input_type is bytes:
- assert sys.getrefcount(msg) == 3
+ assert sys.getrefcount(msg) <= 3
def test_datetime_aware_ext(self):
dec = msgspec.msgpack.Decoder(datetime.datetime)
@@ -815,7 +815,7 @@ class TestTypedDecoder:
res = dec.decode(enc.encode(x))
assert res == x
if res:
- assert sys.getrefcount(res[0]) == 3 # 1 tuple, 1 index, 1 func call
+ assert sys.getrefcount(res[0]) <= 3 # 1 tuple, 1 index, 1 func call
@pytest.mark.parametrize("typ", [tuple, Tuple, Tuple[Any, ...]])
def test_vartuple_any(self, typ):
diff --git a/tests/test_struct.py b/tests/test_struct.py
index 66971fa..9f0ec8f 100644
--- a/tests/test_struct.py
+++ b/tests/test_struct.py
@@ -931,16 +931,16 @@ def test_struct_reference_counting():
data = [1, 2, 3]
t = Test(data)
- assert sys.getrefcount(data) == 3
+ assert sys.getrefcount(data) <= 3
repr(t)
- assert sys.getrefcount(data) == 3
+ assert sys.getrefcount(data) <= 3
t2 = t.__copy__()
- assert sys.getrefcount(data) == 4
+ assert sys.getrefcount(data) <= 4
assert t == t2
- assert sys.getrefcount(data) == 4
+ assert sys.getrefcount(data) <= 4
def test_struct_gc_not_added_if_not_needed():
@@ -2581,7 +2581,7 @@ class TestPostInit:
Ex(1)
assert called
# Return value is decref'd
- assert sys.getrefcount(singleton) == 2 # 1 for ref, 1 for call
+ assert sys.getrefcount(singleton) <= 2 # 1 for ref, 1 for call
def test_post_init_errors(self):
class Ex(Struct):
|