Skip to content

Commit eb4ce8a

Browse files
docs: Update ShareableList documentation to reflect trailing null preservation
Added versionchanged directive for Python 3.15 noting that trailing null bytes are now preserved in both strings and bytes. Updated doctest example to show correct behavior and clarified workaround is only needed for 3.14 and earlier.
1 parent 2c0935c commit eb4ce8a

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

Doc/library/multiprocessing.shared_memory.rst

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,17 @@ finishes execution.
311311
existing :class:`!ShareableList`, specify its shared memory block's unique
312312
name while leaving *sequence* set to ``None``.
313313

314-
.. note::
314+
.. versionchanged:: 3.15
315+
Trailing null bytes (``\x00``) in :class:`bytes` and :class:`str` values
316+
are now preserved correctly. See :gh:`106939` and :gh:`145261`.
315317

316-
.. versionchanged:: 3.15
317-
Fixed a bug where :class:`bytes` and :class:`str` values ending with
318-
``\x00`` nul bytes or characters were silently stripped when fetching
319-
them by index. Trailing nulls are now preserved correctly.
320-
See :gh:`106939` and :gh:`145261`.
318+
.. note::
321319

322-
In Python 3.14 and earlier, a bug exists where :class:`bytes` and
323-
:class:`str` values ending with ``\x00`` nul bytes or characters may be
324-
*silently stripped* when fetching them by index from the
325-
:class:`!ShareableList`. This ``.rstrip(b'\x00')`` behavior has been
326-
fixed in Python 3.15.
320+
In Python 3.14 and earlier, a known issue exists for :class:`bytes` and
321+
:class:`str` values. If they end with ``\x00`` nul bytes or characters,
322+
those may be *silently stripped* when fetching them by index from the
323+
:class:`!ShareableList`. This ``.rstrip(b'\x00')`` behavior was fixed
324+
in Python 3.15.
327325

328326
For applications that need to work with Python 3.14 and earlier where
329327
rstripping of trailing nulls is a problem, work around it by always
@@ -333,13 +331,14 @@ finishes execution.
333331
.. doctest::
334332

335333
>>> from multiprocessing import shared_memory
336-
>>> nul_bug_demo = shared_memory.ShareableList(['?\x00', b'\x03\x02\x01\x00\x00\x00'])
337-
>>> nul_bug_demo[0]
334+
>>> # Python 3.15+: trailing nulls are preserved
335+
>>> sl = shared_memory.ShareableList(['?\x00', b'\x03\x02\x01\x00\x00\x00'])
336+
>>> sl[0]
338337
'?\x00'
339-
>>> nul_bug_demo[1]
338+
>>> sl[1]
340339
b'\x03\x02\x01\x00\x00\x00'
341-
>>> nul_bug_demo.shm.unlink()
342-
>>> # Workaround for Python 3.14 and earlier (not needed in 3.15+):
340+
>>> sl.shm.unlink()
341+
>>> # Workaround for Python 3.14 and earlier:
343342
>>> padded = shared_memory.ShareableList(['?\x00\x07', b'\x03\x02\x01\x00\x00\x00\x07'])
344343
>>> padded[0][:-1]
345344
'?\x00'

0 commit comments

Comments
 (0)