Skip to content

Commit 254230f

Browse files
fix: Also preserve trailing nulls in strings, not just bytes
Extended the fix to remove rstrip from strings as well and store actual byte lengths for both strings and bytes in format metadata.
1 parent 3f874bc commit 254230f

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

Lib/multiprocessing/shared_memory.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ class ShareableList:
286286
_alignment = 8
287287
_back_transforms_mapping = {
288288
0: lambda value: value, # int, float, bool
289-
1: lambda value: value.rstrip(b'\x00').decode(_encoding), # str
289+
1: lambda value: value.decode(_encoding), # str
290290
2: lambda value: value, # bytes
291291
3: lambda _value: None, # None
292292
}
@@ -355,9 +355,11 @@ def __init__(self, sequence=None, *, name=None):
355355
self._offset_data_start,
356356
*(v.encode(_enc) if isinstance(v, str) else v for v in sequence)
357357
)
358-
# For bytes, store actual length so retrieval is exact
358+
# For bytes and str, store actual byte length so retrieval is exact
359359
_stored_formats = [
360-
self._types_mapping[bytes] % (len(v),) if isinstance(v, bytes) else f
360+
(self._types_mapping[str] % (len(v.encode(_enc)),) if isinstance(v, str)
361+
else self._types_mapping[bytes] % (len(v),) if isinstance(v, bytes)
362+
else f)
361363
for v, f in zip(sequence, _formats)
362364
]
363365
struct.pack_into(
@@ -481,8 +483,9 @@ def __setitem__(self, position, value):
481483

482484
self._set_packing_format_and_transform(
483485
position,
484-
self._types_mapping[bytes] % (len(encoded_value),)
485-
if isinstance(value, bytes) else new_format,
486+
(self._types_mapping[bytes] % (len(encoded_value),) if isinstance(value, bytes)
487+
else self._types_mapping[str] % (len(encoded_value),) if isinstance(value, str)
488+
else new_format),
486489
value
487490
)
488491
struct.pack_into(new_format, self.shm.buf, offset, encoded_value)

0 commit comments

Comments
 (0)