Skip to content

Commit 26aa1c2

Browse files
committed
Add a "Mutating string objects" section
1 parent 115765c commit 26aa1c2

1 file changed

Lines changed: 40 additions & 42 deletions

File tree

‎Doc/c-api/unicode.rst‎

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,6 +1904,34 @@ The following API is deprecated.
19041904
:c:func:`PyUnicode_READY` is necessary.
19051905
19061906
1907+
.. _pyobject-new-mutating:
1908+
1909+
Mutating string objects
1910+
"""""""""""""""""""""""
1911+
1912+
The following functions allow creating a blank string object of a given size,
1913+
then filling in its contents.
1914+
They are :term:`soft deprecated`: they break the assumption that
1915+
strings are immutable, making them hard to use correctly.
1916+
1917+
Prefer using the :c:type:`PyUnicodeWriter` API,
1918+
or one of the ``PyUnicode_From*``
1919+
functions such as :c:func:`PyUnicode_FromStringAndSize`.
1920+
1921+
If you do use the functions below, take care to not "use" such a string while
1922+
it is being modified.
1923+
In particular, before it's filled with its final contents, a string:
1924+
1925+
- must not be hashed,
1926+
- must not be :c:func:`converted to UTF-8 <PyUnicode_AsUTF8AndSize>`,
1927+
or another non-"canonical" representation,
1928+
- must not have its reference count changed,
1929+
- must not be shared with code that might do one of the above.
1930+
1931+
This list is not exhaustive. Avoiding these uses is your responsibility;
1932+
Python does not always check these requirements.
1933+
1934+
19071935
.. c:function:: PyObject* PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar)
19081936
19091937
Create a new Unicode object. *maxchar* should be the true maximum code point
@@ -1912,34 +1940,12 @@ The following API is deprecated.
19121940
19131941
On error, set an exception and return ``NULL``.
19141942
1915-
After creation, the string can be filled by :c:func:`PyUnicode_WriteChar`,
1916-
:c:func:`PyUnicode_CopyCharacters`, :c:func:`PyUnicode_Fill`,
1917-
:c:func:`PyUnicode_WRITE` or similar.
1918-
Since strings are supposed to be immutable, take care to not “use” the
1919-
result while it is being modified. In particular, before it's filled
1920-
with its final contents, a string:
1921-
1922-
- must not be hashed,
1923-
- must not be :c:func:`converted to UTF-8 <PyUnicode_AsUTF8AndSize>`,
1924-
or another non-"canonical" representation,
1925-
- must not have its reference count changed,
1926-
- must not be shared with code that might do one of the above.
1927-
1928-
This list is not exhaustive. Avoiding these uses is your responsibility;
1929-
Python does not always check these requirements.
1930-
1931-
To avoid accidentally exposing a partially-written string object, prefer
1932-
using the :c:type:`PyUnicodeWriter` API, or one of the ``PyUnicode_From*``
1933-
functions below.
1934-
1935-
While :class:`str` objects are usually immutable in Python, this special C API
1936-
returns a :class:`str` object that can be mutated, except if *size* is zero, in which
1937-
case it returns the immutable empty string constant.
1943+
See :ref:`pyobject-new-mutating` for important warnings and caveats.
19381944
19391945
.. versionadded:: 3.3
19401946
19411947
.. soft-deprecated:: next
1942-
Use the :c:type:`PyUnicodeWriter` API instead.
1948+
See :ref:`pyobject-new-mutating`.
19431949
19441950
19451951
.. c:function:: void PyUnicode_WRITE(int kind, void *data, \
@@ -1956,8 +1962,8 @@ The following API is deprecated.
19561962
The function performs no checks for any of its requirements,
19571963
and is intended for usage in loops.
19581964
1959-
While :class:`str` objects are usually immutable in Python, this special C API allows
1960-
mutating a fresh :class:`str` object if the string has not been "used" yet.
1965+
The owning string must not be "used" yet.
1966+
See :ref:`pyobject-new-mutating` for details.
19611967
19621968
.. versionadded:: 3.3
19631969
@@ -1976,10 +1982,8 @@ The following API is deprecated.
19761982
possible. Returns ``-1`` and sets an exception on error, otherwise returns
19771983
the number of copied characters.
19781984
1979-
While :class:`str` objects are usually immutable in Python, this special C API allows
1980-
mutating a fresh :class:`str` object if the string has not been "used" yet.
1981-
1982-
See :c:func:`PyUnicode_New` for details.
1985+
The destination string must not be "used" yet.
1986+
See :ref:`pyobject-new-mutating` for details.
19831987
19841988
.. versionadded:: 3.3
19851989
@@ -2001,10 +2005,8 @@ The following API is deprecated.
20012005
The function doesn't check string content, the result may not be a
20022006
string in canonical representation.
20032007
2004-
While :class:`str` objects are usually immutable in Python, this special C API
2005-
can resize a :class:`str` object in-place if the string has not been "used" yet.
2006-
It returns a :class:`str` object which can be mutated, except if *size* is zero, in
2007-
which case it returns the immutable empty string constant.
2008+
*\*unicode* must not be "used" yet.
2009+
See :ref:`pyobject-new-mutating` for details.
20082010
20092011
.. soft-deprecated:: next
20102012
Use the :c:type:`PyUnicodeWriter` API instead.
@@ -2022,10 +2024,8 @@ The following API is deprecated.
20222024
Return the number of written characters, or return ``-1`` and raise an
20232025
exception on error.
20242026
2025-
While :class:`str` objects are usually immutable in Python, this special C API allows
2026-
mutating a fresh :class:`str` object if the string has not been "used" yet.
2027-
2028-
See :c:func:`PyUnicode_New` for details.
2027+
*unicode* must not be "used" yet.
2028+
See :ref:`pyobject-new-mutating` for details.
20292029
20302030
.. versionadded:: 3.3
20312031
@@ -2044,10 +2044,8 @@ The following API is deprecated.
20442044
See :c:func:`PyUnicode_WRITE` for a version that skips these checks,
20452045
making them your responsibility.
20462046
2047-
While :class:`str` objects are usually immutable in Python, this special C API allows
2048-
mutating a fresh :class:`str` object if the string has not been "used" yet.
2049-
2050-
See :c:func:`PyUnicode_New` for details.
2047+
*unicode* must not be "used" yet.
2048+
See :ref:`pyobject-new-mutating` for details.
20512049
20522050
.. versionadded:: 3.3
20532051

0 commit comments

Comments
 (0)