Skip to content

Commit b133bb4

Browse files
Issue python#27481: Docummented that ValueError is now raised instead of TypeError
in case of embedded null characters/bytes. Patch by Xiang Zhang.
2 parents b209392 + 6f379f4 commit b133bb4

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

Doc/c-api/arg.rst

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ Unless otherwise stated, buffers are not NUL-terminated.
5959
Convert a Unicode object to a C pointer to a character string.
6060
A pointer to an existing string is stored in the character pointer
6161
variable whose address you pass. The C string is NUL-terminated.
62-
The Python string must not contain embedded NUL bytes; if it does,
63-
a :exc:`TypeError` exception is raised. Unicode objects are converted
62+
The Python string must not contain embedded null characters; if it does,
63+
a :exc:`ValueError` exception is raised. Unicode objects are converted
6464
to C strings using ``'utf-8'`` encoding. If this conversion fails, a
6565
:exc:`UnicodeError` is raised.
6666

@@ -71,6 +71,10 @@ Unless otherwise stated, buffers are not NUL-terminated.
7171
preferable to use the ``O&`` format with :c:func:`PyUnicode_FSConverter`
7272
as *converter*.
7373

74+
.. versionchanged:: 3.5
75+
Previously, :exc:`TypeError` was raised when embedded null characters
76+
were encountered in the Python string.
77+
7478
``s*`` (:class:`str` or :term:`bytes-like object`) [Py_buffer]
7579
This format accepts Unicode objects as well as bytes-like objects.
7680
It fills a :c:type:`Py_buffer` structure provided by the caller.
@@ -99,9 +103,13 @@ Unless otherwise stated, buffers are not NUL-terminated.
99103
``y`` (read-only :term:`bytes-like object`) [const char \*]
100104
This format converts a bytes-like object to a C pointer to a character
101105
string; it does not accept Unicode objects. The bytes buffer must not
102-
contain embedded NUL bytes; if it does, a :exc:`TypeError`
106+
contain embedded null bytes; if it does, a :exc:`ValueError`
103107
exception is raised.
104108

109+
.. versionchanged:: 3.5
110+
Previously, :exc:`TypeError` was raised when embedded null bytes were
111+
encountered in the bytes buffer.
112+
105113
``y*`` (:term:`bytes-like object`) [Py_buffer]
106114
This variant on ``s*`` doesn't accept Unicode objects, only
107115
bytes-like objects. **This is the recommended way to accept
@@ -127,14 +135,18 @@ Unless otherwise stated, buffers are not NUL-terminated.
127135
pointer variable, which will be filled with the pointer to an existing
128136
Unicode buffer. Please note that the width of a :c:type:`Py_UNICODE`
129137
character depends on compilation options (it is either 16 or 32 bits).
130-
The Python string must not contain embedded NUL characters; if it does,
131-
a :exc:`TypeError` exception is raised.
138+
The Python string must not contain embedded null characters; if it does,
139+
a :exc:`ValueError` exception is raised.
132140

133141
.. note::
134142
Since ``u`` doesn't give you back the length of the string, and it
135143
may contain embedded NUL characters, it is recommended to use ``u#``
136144
or ``U`` instead.
137145

146+
.. versionchanged:: 3.5
147+
Previously, :exc:`TypeError` was raised when embedded null characters
148+
were encountered in the Python string.
149+
138150
``u#`` (:class:`str`) [Py_UNICODE \*, int]
139151
This variant on ``u`` stores into two C variables, the first one a pointer to a
140152
Unicode data buffer, the second one its length.

Doc/c-api/bytes.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ called with a non-bytes parameter.
158158
159159
If *length* is *NULL*, the bytes object
160160
may not contain embedded null bytes;
161-
if it does, the function returns ``-1`` and a :exc:`TypeError` is raised.
161+
if it does, the function returns ``-1`` and a :exc:`ValueError` is raised.
162162
163163
The buffer refers to an internal buffer of *obj*, which includes an
164164
additional null byte at the end (not counted in *length*). The data
@@ -167,6 +167,10 @@ called with a non-bytes parameter.
167167
*obj* is not a bytes object at all, :c:func:`PyBytes_AsStringAndSize`
168168
returns ``-1`` and raises :exc:`TypeError`.
169169
170+
.. versionchanged:: 3.5
171+
Previously, :exc:`TypeError` was raised when embedded null bytes were
172+
encountered in the bytes object.
173+
170174
171175
.. c:function:: void PyBytes_Concat(PyObject **bytes, PyObject *newpart)
172176

0 commit comments

Comments
 (0)