diff --git a/Lib/multiprocessing/shared_memory.py b/Lib/multiprocessing/shared_memory.py index 99a8ce3320ad4e..4c0bf87dabda48 100644 --- a/Lib/multiprocessing/shared_memory.py +++ b/Lib/multiprocessing/shared_memory.py @@ -305,15 +305,22 @@ def _extract_recreation_code(value): else: return 3 # NoneType + def _get_str_or_bytes_format(self, item): + """Used to ensure the correct length is inserted into the + the formatted _types_mapping for multibyte utf-8 characters.""" + length = len( + item if isinstance(item, bytes) else item.encode(_encoding) + ) + aligned_length = self._alignment * (length // self._alignment + 1) + return self._types_mapping[type(item)] % aligned_length + def __init__(self, sequence=None, *, name=None): if name is None or sequence is not None: sequence = sequence or () _formats = [ self._types_mapping[type(item)] if not isinstance(item, (str, bytes)) - else self._types_mapping[type(item)] % ( - self._alignment * (len(item) // self._alignment + 1), - ) + else self._get_str_or_bytes_format(item) for item in sequence ] self._list_len = len(_formats) diff --git a/Misc/ACKS b/Misc/ACKS index 2a68b69f161041..02313e80ae9f8a 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -84,6 +84,7 @@ Andres Ayala Cathy Avery John Aycock Donovan Baarda +Donnelly Baart Arne Babenhauserheide Attila Babo Matt Bachmann diff --git a/Misc/NEWS.d/next/Library/2024-08-19-22-29-04.gh-issue-88336.ngnNjP.rst b/Misc/NEWS.d/next/Library/2024-08-19-22-29-04.gh-issue-88336.ngnNjP.rst new file mode 100644 index 00000000000000..35e98d95a80b45 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-08-19-22-29-04.gh-issue-88336.ngnNjP.rst @@ -0,0 +1,2 @@ +Fix a bug where the length of multibyte utf-8 characters was not taken into +account in :class:`multiprocessing.shared_memory.ShareableList`.