From f799a817e32a3be6a1ce7fbf90edcba14d5a569b Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 2 Oct 2026 14:50:43 -0500 Subject: [PATCH] PYTHON-6140 [v4.18] [Build Failure] TestBSON.test_encode_size_limit MemoryError on Windows (#3092) --- test/test_bson.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/test/test_bson.py b/test/test_bson.py index 191a706388..0800806b84 100644 --- a/test/test_bson.py +++ b/test/test_bson.py @@ -75,6 +75,25 @@ _NUMPY_AVAILABLE = importlib.util.find_spec("numpy") is not None +def _total_memory(): + """Return total physical memory in bytes, or None if it cannot be determined.""" + try: + return os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES") + except (ValueError, OSError, AttributeError): + return None + + +# PYTHON-6140: on Linux, skip if the host cannot allocate the ~4GiB this test +# needs. On other platforms, run the test and skip on MemoryError. +_TOTAL_MEMORY = _total_memory() +_ENCODE_SIZE_LIMIT_MIN_MEMORY = 6 << 30 +_SKIP_ENCODE_SIZE_LIMIT = ( + sys.platform == "linux" + and _TOTAL_MEMORY is not None + and _TOTAL_MEMORY < _ENCODE_SIZE_LIMIT_MIN_MEMORY +) + + class NotADict(abc.MutableMapping): """Non-dict type that implements the mapping protocol.""" @@ -692,12 +711,22 @@ def test_overflow(self): self.assertRaises(OverflowError, encode, {"x": -9223372036854775809}) @unittest.skipUnless(bson.has_c(), "This test requires the C extension") + @unittest.skipIf( + _SKIP_ENCODE_SIZE_LIMIT, + "Test requires at least 6GiB of memory (PYTHON-6140)", + ) def test_encode_size_limit(self): # PYTHON-5996: encoding must raise when a document's encoded size # exceeds the BSON size limit. - big_value = "a" * (1 << 30) - with self.assertRaises(ValueError): - encode({"a": big_value, "b": big_value, "c": big_value}) + try: + big_value = "a" * (1 << 30) + with self.assertRaises(ValueError): + encode({"a": big_value, "b": big_value, "c": big_value}) + except MemoryError: + # PYTHON-6140: some hosts report enough installed memory but + # cannot actually commit it (pagefile limits, mongod running on + # the same host). Skip rather than fail. + self.skipTest("Could not allocate enough memory (PYTHON-6140)") def test_small_long_encode_decode(self): encoded1 = encode({"x": 256})