Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,21 +1364,16 @@ def wrapper(self):
return wrapper
return decorator

def nomemtest(f):
def nomemtest(test):
"""Check that we can use this test with `_testcapi.set_nomemory`."""
from .import_helper import import_module

@functools.wraps(f)
@functools.wraps(test)
def internal(*args, **kwargs):
import_module('_testcapi')
return f(*args, **kwargs)
return test(*args, **kwargs)

return unittest.skipIf(
# Python built with Py_TRACE_REFS fail with a fatal error in
# _PyRefchain_Trace() on memory allocation error.
Py_TRACE_REFS,
'cannot test Py_TRACE_REFS build',
)(cpython_only(internal))
return cpython_only(internal)

def bigaddrspacetest(f):
"""Decorator for tests that fill the address space."""
Expand Down
67 changes: 67 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import array
import contextlib
import operator
import os
import re
Expand Down Expand Up @@ -48,6 +49,19 @@ def __index__(self):
return self.value


@contextlib.contextmanager
def inject_memory_error(testcase, start):
# Raise SkipTest if _testcapi extension module is missing
_testcapi = import_helper.import_module('_testcapi')

with testcase.assertRaises(MemoryError):
try:
_testcapi.set_nomemory(start)
yield
finally:
_testcapi.remove_mem_hooks()


class BaseBytesTest:

def assertTypedEqual(self, actual, expected):
Expand Down Expand Up @@ -1555,6 +1569,36 @@ def test_resize(self):
self.assertRaises(MemoryError, bytearray().resize, sys.maxsize)
self.assertRaises(MemoryError, bytearray(1000).resize, sys.maxsize)

@support.nomemtest
def test_resize_error(self):
# gh-157242: If bytearray.resize() fails (MemoryError),
# the bytearray must be left unchanged.

offset = 3
for logical_offset in (False, True):
with self.subTest(logical_offset=logical_offset):
# grow bytearray
ba = bytearray(b'0123456789')
if logical_offset:
expected = ba[offset:]
del ba[:offset]
else:
expected = ba.copy()
with inject_memory_error(self, 0):
ba.resize(1024)
self.assertEqual(ba, expected)

# shrink bytearray
ba = bytearray(b'0123456789')
if logical_offset:
expected = ba[offset:]
del ba[:offset]
else:
expected = ba.copy()
with inject_memory_error(self, 0):
ba.resize(1)
self.assertEqual(ba, expected)

def test_take_bytes(self):
ba = bytearray(b'ab')
self.assertEqual(ba.take_bytes(), b'ab')
Expand Down Expand Up @@ -1619,6 +1663,29 @@ def test_take_bytes(self):
self.assertEqual(ba, bytearray(b'A'))
self.assertEqual(ord(b'c'), ord('c'))

@support.nomemtest
def test_take_bytes_error(self):
# gh-157242: If bytearray.take_bytes() fails (MemoryError),
# the bytearray must be left unchanged.

for logical_offset, to_take, mem_errors in (
(True, 5, (0, 1)),
(False, 5, (0, 1)),
(True, None, (0,)),
):
for mem_error in mem_errors:
with self.subTest(logical_offset=logical_offset,
to_take=to_take, mem_error=mem_error):
ba = bytearray(b'0123456789')
if logical_offset:
expected = ba[3:]
del ba[:3]
else:
expected = ba.copy()
with inject_memory_error(self, mem_error):
ba.take_bytes(to_take)
self.assertEqual(ba, expected)

@support.cpython_only # tests an implementation detail
def test_take_bytes_optimization(self):
# Validate optimization around taking lots of little chunks out of a
Expand Down
38 changes: 20 additions & 18 deletions Lib/test/test_capi/test_bytes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import unittest
from test import support
from test.support import import_helper

_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
Expand Down Expand Up @@ -389,6 +390,25 @@ def test_resize(self):
writer.resize(len(b'number=123456'), b'456')
self.assertEqual(writer.finish(), self.result_type(b'number=123456'))

@support.nomemtest
def test_resize_error(self):
small_buffer = _testcapi.PyBytesWriter_small_buffer
init = b'x' * (small_buffer * 2)
writer = self.create_writer(len(init), init)
size = len(init) + 100
try:
with self.assertRaises(MemoryError):
_testcapi.set_nomemory(0)
writer.resize(size, b'')
finally:
_testcapi.remove_mem_hooks()
suffix = b'still working'
writer.write_bytes(suffix, -1)
self.assertEqual(writer.finish(), self.result_type(init + suffix))

# Note: PyBytesWriter_Resize() leaves the buffer unchanged (no resize)
# if the new size is smaller than the allocated size

def test_format_i(self):
# Test PyBytesWriter_Format()
writer = self.create_writer()
Expand Down Expand Up @@ -446,24 +466,6 @@ def test_example_resize(self):
def test_example_highlevel(self):
self.assertEqual(_testcapi.byteswriter_highlevel(), b'Hello World!')

def test_resize_error(self):
small_buffer = _testcapi.PyBytesWriter_small_buffer
init = b'x' * (small_buffer * 2)
writer = self.create_writer(len(init), init)
size = len(init) + 100
try:
with self.assertRaises(MemoryError):
_testcapi.set_nomemory(0)
writer.resize(size, b'')
finally:
_testcapi.remove_mem_hooks()
suffix = b'still working'
writer.write_bytes(suffix, -1)
self.assertEqual(writer.finish(), self.result_type(init + suffix))

# Note: PyBytesWriter_Resize() leaves the buffer unchanged (no resize)
# if the new size is smaller than the allocated size


class ByteArrayWriterTest(BaseWriterTest, unittest.TestCase):
result_type = bytearray
Expand Down
37 changes: 37 additions & 0 deletions Lib/test/test_free_threading/test_bytes_object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import unittest
from threading import Thread, Barrier
from test.support import threading_helper

threading_helper.requires_working_threading(module=True)


class BytesThreading(unittest.TestCase):
@threading_helper.reap_threads
def test_conversion_from_mutating_list(self):
number_of_threads = 10
number_of_iterations = 10
barrier = Barrier(number_of_threads)

x = [1, 2, 3, 4, 5]
extends = [(ii,) * (2 + ii) for ii in range(number_of_threads)]

def work(ii):
barrier.wait()
for _ in range(100):
bytes(x)
x.extend(extends[ii])
if len(x) > 10:
x[:] = [0]

for it in range(number_of_iterations):
worker_threads = []
for ii in range(number_of_threads):
worker_threads.append(Thread(target=work, args=[ii]))
with threading_helper.start_threads(worker_threads):
pass

barrier.reset()


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Speed up :class:`bytes` creation from :class:`list` and :class:`tuple` of integers.

Patch by Ben Hsing and Pieter Eendebak
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
If :meth:`bytearray.resize` or :meth:`bytearray.take_bytes` fails, leave the
:class:`bytearray` unchanged, instead of clearing it. Patch by Victor
Stinner.
Loading
Loading