Skip to content

Commit 1477a2c

Browse files
committed
gh-88336: Fix ShareableList utf-8 bug
Fix a bug where the length of multibyte utf-8 characters was not taken into account in multiprocessing.shared_memory.ShareableList.
1 parent 833c58b commit 1477a2c

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

Lib/multiprocessing/shared_memory.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,20 @@ def _extract_recreation_code(value):
305305
else:
306306
return 3 # NoneType
307307

308+
def _get_str_or_bytes_format(self, item):
309+
"""Used to ensure the correct length is inserted into the
310+
the formatted _types_mapping for multibyte utf-8 characters."""
311+
length = len(item if isinstance(item, bytes) else item.encode())
312+
aligned_length = self._alignment * (length // self._alignment + 1)
313+
return self._types_mapping[type(item)] % aligned_length
314+
308315
def __init__(self, sequence=None, *, name=None):
309316
if name is None or sequence is not None:
310317
sequence = sequence or ()
311318
_formats = [
312319
self._types_mapping[type(item)]
313320
if not isinstance(item, (str, bytes))
314-
else self._types_mapping[type(item)] % (
315-
self._alignment * (len(item) // self._alignment + 1),
316-
)
321+
else self._get_str_or_bytes_format(item)
317322
for item in sequence
318323
]
319324
self._list_len = len(_formats)

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Andres Ayala
8383
Cathy Avery
8484
John Aycock
8585
Donovan Baarda
86+
Donnelly Baart
8687
Arne Babenhauserheide
8788
Attila Babo
8889
Matt Bachmann
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug where the length of multibyte utf-8 characters was not taken into
2+
account in :class:`multiprocessing.shared_memory.ShareableList`.

0 commit comments

Comments
 (0)