Skip to content

Commit ac37b77

Browse files
gh-137044: Fix test_resource on 32-bit Linux (GH-137941)
1 parent 379161d commit ac37b77

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

Lib/test/test_resource.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,33 +116,40 @@ def test_fsize_not_too_big(self):
116116
self.addCleanup(resource.setrlimit, resource.RLIMIT_FSIZE, (cur, max))
117117

118118
def expected(cur):
119-
return (min(cur, resource.RLIM_INFINITY), max)
119+
# The glibc wrapper functions use a 64-bit rlim_t data type,
120+
# even on 32-bit platforms. If a program tried to set a resource
121+
# limit to a value larger than can be represented in a 32-bit
122+
# unsigned long, then the glibc setrlimit() wrapper function
123+
# silently converted the limit value to RLIM_INFINITY.
124+
if sys.maxsize < 2**32 <= cur <= resource.RLIM_INFINITY:
125+
return [(resource.RLIM_INFINITY, max), (cur, max)]
126+
return [(min(cur, resource.RLIM_INFINITY), max)]
120127

121128
resource.setrlimit(resource.RLIMIT_FSIZE, (2**31-5, max))
122129
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), (2**31-5, max))
123130
resource.setrlimit(resource.RLIMIT_FSIZE, (2**31, max))
124-
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**31))
131+
self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**31))
125132
resource.setrlimit(resource.RLIMIT_FSIZE, (2**32-5, max))
126-
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**32-5))
133+
self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**32-5))
127134

128135
try:
129136
resource.setrlimit(resource.RLIMIT_FSIZE, (2**32, max))
130137
except OverflowError:
131138
pass
132139
else:
133-
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**32))
140+
self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**32))
134141

135142
resource.setrlimit(resource.RLIMIT_FSIZE, (2**63-5, max))
136-
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**63-5))
143+
self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**63-5))
137144
try:
138145
resource.setrlimit(resource.RLIMIT_FSIZE, (2**63, max))
139146
except ValueError:
140147
# There is a hard limit on macOS.
141148
pass
142149
else:
143-
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**63))
150+
self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**63))
144151
resource.setrlimit(resource.RLIMIT_FSIZE, (2**64-5, max))
145-
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**64-5))
152+
self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**64-5))
146153

147154
@unittest.skipIf(sys.platform == "vxworks",
148155
"setting RLIMIT_FSIZE is not supported on VxWorks")

0 commit comments

Comments
 (0)