Skip to content

Commit 0107731

Browse files
[3.12] gh-106939: document ShareableList nul-strip quirk. (GH-107266) (#107269)
gh-106939: document ShareableList nul-strip quirk. (GH-107266) * gh-106939: document ShareableList nul-strip quirk. * Mention the `int` size constraint. (cherry picked from commit 70dc009) Co-authored-by: Gregory P. Smith <greg@krypto.org>
1 parent c3c8916 commit 0107731

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

Doc/library/multiprocessing.shared_memory.rst

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,17 @@ shared memory blocks created using that manager are all released when the
255255
:keyword:`with` statement's code block finishes execution.
256256

257257

258-
.. class:: ShareableList(sequence=None, *, name=None)
258+
.. class:: ShareableList(sequence=None, \*, name=None)
259259

260260
Provides a mutable list-like object where all values stored within are
261261
stored in a shared memory block. This constrains storable values to
262-
only the ``int``, ``float``, ``bool``, ``str`` (less than 10M bytes each),
263-
``bytes`` (less than 10M bytes each), and ``None`` built-in data types.
264-
It also notably differs from the built-in ``list`` type in that these
265-
lists can not change their overall length (i.e. no append, insert, etc.)
266-
and do not support the dynamic creation of new :class:`ShareableList`
267-
instances via slicing.
262+
only the ``int`` (signed 64-bit), ``float``, ``bool``, ``str`` (less
263+
than 10M bytes each when encoded as utf-8), ``bytes`` (less than 10M
264+
bytes each), and ``None`` built-in data types. It also notably
265+
differs from the built-in ``list`` type in that these lists can not
266+
change their overall length (i.e. no append, insert, etc.) and do not
267+
support the dynamic creation of new :class:`ShareableList` instances
268+
via slicing.
268269

269270
*sequence* is used in populating a new ``ShareableList`` full of values.
270271
Set to ``None`` to instead attach to an already existing
@@ -275,6 +276,35 @@ shared memory blocks created using that manager are all released when the
275276
existing ``ShareableList``, specify its shared memory block's unique
276277
name while leaving ``sequence`` set to ``None``.
277278

279+
.. note::
280+
281+
A known issue exists for :class:`bytes` and :class:`str` values.
282+
If they end with ``\x00`` nul bytes or characters, those may be
283+
*silently stripped* when fetching them by index from the
284+
:class:`ShareableList`. This ``.rstrip(b'\x00')`` behavior is
285+
considered a bug and may go away in the future. See :gh:`106939`.
286+
287+
For applications where rstripping of trailing nulls is a problem,
288+
work around it by always unconditionally appending an extra non-0
289+
byte to the end of such values when storing and unconditionally
290+
removing it when fetching:
291+
292+
.. doctest::
293+
294+
>>> from multiprocessing import shared_memory
295+
>>> nul_bug_demo = shared_memory.ShareableList(['?\x00', b'\x03\x02\x01\x00\x00\x00'])
296+
>>> nul_bug_demo[0]
297+
'?'
298+
>>> nul_bug_demo[1]
299+
b'\x03\x02\x01'
300+
>>> nul_bug_demo.shm.unlink()
301+
>>> padded = shared_memory.ShareableList(['?\x00\x07', b'\x03\x02\x01\x00\x00\x00\x07'])
302+
>>> padded[0][:-1]
303+
'?\x00'
304+
>>> padded[1][:-1]
305+
b'\x03\x02\x01\x00\x00\x00'
306+
>>> padded.shm.unlink()
307+
278308
.. method:: count(value)
279309

280310
Returns the number of occurrences of ``value``.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy