Skip to content

Commit 3ed2e6d

Browse files
committed
TEST: Add fixture for relaxing digit limits
1 parent fce4911 commit 3ed2e6d

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

nibabel/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
13
import numpy as np
24
import pytest
35

@@ -12,3 +14,18 @@ def legacy_printoptions():
1214

1315
if Version(np.__version__) >= Version('1.22'):
1416
np.set_printoptions(legacy='1.21')
17+
18+
19+
@pytest.fixture
20+
def max_digits():
21+
# Set maximum number of digits for int/str conversion for
22+
# duration of a test
23+
try:
24+
orig_max_str_digits = sys.get_int_max_str_digits()
25+
yield sys.set_int_max_str_digits
26+
sys.set_int_max_str_digits(orig_max_str_digits)
27+
except AttributeError:
28+
# Nothing to do for versions of Python that lack these methods
29+
# They were added as DoS protection in Python 3.11 and backported to
30+
# some other versions.
31+
yield lambda x: None

nibabel/tests/test_floating.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ def test_floor_exact_64():
172172
assert floor_exact(test_val, np.float64) == 2 ** (e + 1) - int(gap)
173173

174174

175-
def test_floor_exact():
175+
def test_floor_exact(max_digits):
176+
max_digits(4950) # max longdouble is ~10**4932
177+
176178
to_test = IEEE_floats + [float]
177179
try:
178180
type_info(np.longdouble)['nmant']
@@ -188,11 +190,11 @@ def test_floor_exact():
188190
for t in to_test:
189191
# A number bigger than the range returns the max
190192
info = type_info(t)
191-
assert floor_exact(2**5000, t) == np.inf
192-
assert ceil_exact(2**5000, t) == np.inf
193+
assert floor_exact(10**4933, t) == np.inf
194+
assert ceil_exact(10**4933, t) == np.inf
193195
# A number more negative returns -inf
194-
assert floor_exact(-(2**5000), t) == -np.inf
195-
assert ceil_exact(-(2**5000), t) == -np.inf
196+
assert floor_exact(-(10**4933), t) == -np.inf
197+
assert ceil_exact(-(10**4933), t) == -np.inf
196198
# Check around end of integer precision
197199
nmant = info['nmant']
198200
for i in range(nmant + 1):

0 commit comments

Comments
 (0)