Index: Doc/c-api/arg.rst =================================================================== --- Doc/c-api/arg.rst (révision 81589) +++ Doc/c-api/arg.rst (copie de travail) @@ -53,13 +53,13 @@ drop int support. It is best to always define :cmacro:`PY_SSIZE_T_CLEAN`. -``s`` (Unicode object) [const char \*] +``s`` (:class:`str`) [const char \*] Convert a Unicode object to a C pointer to a character string. A pointer to an existing string is stored in the character pointer variable whose address you pass. The C string is NUL-terminated. The Python string must not contain embedded NUL bytes; if it does, a :exc:`TypeError` exception is raised. Unicode objects are converted - to C strings using the default encoding. If this conversion fails, a + to C strings using ``'utf-8'`` encoding. If this conversion fails, a :exc:`UnicodeError` is raised. .. note:: @@ -68,111 +68,112 @@ preferrable to use the ``O&`` format with :cfunc:`PyUnicode_FSConverter` as *converter*. -``s*`` (Unicode object or any buffer compatible object) [Py_buffer] +``s*`` (:class:`str`, :class:`bytes`, :class:`bytearray` or buffer compatible object) [Py_buffer] This format accepts Unicode objects as well as objects supporting the - buffer protocol (such as :class:`bytes` or :class:`bytearray` objects). + buffer protocol. It fills a :ctype:`Py_buffer` structure provided by the caller. - Unicode objects are converted to C strings using the default encoding. In this case the resulting C string may contain embedded NUL bytes. + Unicode objects are converted to C strings using ``'utf-8'`` encoding. -``s#`` (string, Unicode or any read buffer compatible object) [const char \*, int or :ctype:`Py_ssize_t`] +``s#`` (:class:`str`, :class:`bytes` or read-only buffer compatible object) [const char \*, int or :ctype:`Py_ssize_t`] Like ``s*``, except that it doesn't accept mutable buffer-like objects such as :class:`bytearray`. The result is stored into two C variables, the first one a pointer to a C string, the second one its length. - The string may contain embedded null bytes. + The string may contain embedded null bytes. Unicode objects are converted + to C strings using ``'utf-8'`` encoding. -``z`` (Unicode object or ``None``) [const char \*] +``z`` (:class:`str` or ``None``) [const char \*] Like ``s``, but the Python object may also be ``None``, in which case the C pointer is set to *NULL*. -``z*`` (Unicode object or ``None`` or any buffer compatible object) [Py_buffer] +``z*`` (:class:`str`, :class:`bytes`, :class:`bytearray`, buffer compatible object or ``None``) [Py_buffer] Like ``s*``, but the Python object may also be ``None``, in which case the ``buf`` member of the :ctype:`Py_buffer` structure is set to *NULL*. -``z#`` (Unicode object or ``None`` or any read buffer compatible object) [const char \*, int] +``z#`` (:class:`str`, :class:`bytes`, read-only buffer compatible object or ``None``) [const char \*, int] Like ``s#``, but the Python object may also be ``None``, in which case the C pointer is set to *NULL*. -``y`` (bytes object) [const char \*] +``y`` (:class:`bytes`) [const char \*] This format converts a bytes-like object to a C pointer to a character string; it does not accept Unicode objects. The bytes buffer must not contain embedded NUL bytes; if it does, a :exc:`TypeError` exception is raised. -``y*`` (any buffer compatible object) [Py_buffer \*] +``y*`` (:class:`bytes`, :class:`bytearray` or buffer compatible object) [Py_buffer \*] This variant on ``s*`` doesn't accept Unicode objects, only objects supporting the buffer protocol. **This is the recommended way to accept binary data.** -``y#`` (bytes object) [const char \*, int] +``y#`` (:class:`bytes`) [const char \*, int] This variant on ``s#`` doesn't accept Unicode objects, only bytes-like objects. -``S`` (bytes object) [PyBytesObject \*] +``S`` (:class:`bytes`) [PyBytesObject \*] Requires that the Python object is a :class:`bytes` object, without attempting any conversion. Raises :exc:`TypeError` if the object is not a bytes object. The C variable may also be declared as :ctype:`PyObject\*`. -``Y`` (bytearray object) [PyByteArrayObject \*] +``Y`` (:class:`bytearray` object) [PyByteArrayObject \*] Requires that the Python object is a :class:`bytearray` object, without attempting any conversion. Raises :exc:`TypeError` if the object is not - a bytearray object. The C variable may also be declared as :ctype:`PyObject\*`. + a :class:`bytearray` object. The C variable may also be declared as :ctype:`PyObject\*`. -``u`` (Unicode object) [Py_UNICODE \*] +``u`` (:class:`str`) [Py_UNICODE \*] Convert a Python Unicode object to a C pointer to a NUL-terminated buffer of Unicode characters. You must pass the address of a :ctype:`Py_UNICODE` pointer variable, which will be filled with the pointer to an existing Unicode buffer. Please note that the width of a :ctype:`Py_UNICODE` character depends on compilation options (it is either 16 or 32 bits). - ..note :: + .. note:: Since ``u`` doesn't give you back the length of the string, and it may contain embedded NUL characters, it is recommended to use ``u#`` or ``U`` instead. -``u#`` (Unicode object) [Py_UNICODE \*, int] +``u#`` (:class:`str`) [Py_UNICODE \*, int] This variant on ``u`` stores into two C variables, the first one a pointer to a Unicode data buffer, the second one its length. Non-Unicode objects are handled by interpreting their read-buffer pointer as pointer to a :ctype:`Py_UNICODE` array. -``Z`` (Unicode or ``None``) [Py_UNICODE \*] +``Z`` (:class:`str` or ``None``) [Py_UNICODE \*] Like ``u``, but the Python object may also be ``None``, in which case the :ctype:`Py_UNICODE` pointer is set to *NULL*. -``Z#`` (Unicode or ``None``) [Py_UNICODE \*, int] +``Z#`` (:class:`str` or ``None``) [Py_UNICODE \*, int] Like ``u#``, but the Python object may also be ``None``, in which case the :ctype:`Py_UNICODE` pointer is set to *NULL*. -``U`` (Unicode object) [PyUnicodeObject \*] +``U`` (:class:`str`) [PyUnicodeObject \*] Requires that the Python object is a Unicode object, without attempting any conversion. Raises :exc:`TypeError` if the object is not a Unicode object. The C variable may also be declared as :ctype:`PyObject\*`. -``t#`` (read-only character buffer) [char \*, int] +``t#`` (:class:`bytes`, :class:`bytearray` or read-only character buffer) [char \*, int] Like ``s#``, but accepts any object which implements the read-only buffer interface. The :ctype:`char\*` variable is set to point to the first byte of the buffer, and the :ctype:`int` is set to the length of the buffer. Only single-segment buffer objects are accepted; :exc:`TypeError` is raised for all others. -``w`` (read-write character buffer) [char \*] +``w`` (:class:`bytearray` or read-write character buffer) [char \*] Similar to ``s``, but accepts any object which implements the read-write buffer interface. The caller must determine the length of the buffer by other means, or use ``w#`` instead. Only single-segment buffer objects are accepted; :exc:`TypeError` is raised for all others. -``w*`` (read-write byte-oriented buffer) [Py_buffer] +``w*`` (:class:`bytearray` or read-write byte-oriented buffer) [Py_buffer] This is to ``w`` what ``s*`` is to ``s``. -``w#`` (read-write character buffer) [char \*, int] +``w#`` (:class:`bytearray` or read-write character buffer) [char \*, int] Like ``s#``, but accepts any object which implements the read-write buffer interface. The :ctype:`char \*` variable is set to point to the first byte of the buffer, and the :ctype:`int` is set to the length of the buffer. Only single-segment buffer objects are accepted; :exc:`TypeError` is raised for all others. -``es`` (string, Unicode object or character buffer compatible object) [const char \*encoding, char \*\*buffer] +``es`` (:class:`str`) [const char \*encoding, char \*\*buffer] This variant on ``s`` is used for encoding Unicode and objects convertible to Unicode into a character buffer. It only works for encoded data without embedded NUL bytes. @@ -190,12 +191,12 @@ allocated storage. The caller is responsible for calling :cfunc:`PyMem_Free` to free the allocated buffer after use. -``et`` (string, Unicode object or character buffer compatible object) [const char \*encoding, char \*\*buffer] - Same as ``es`` except that 8-bit string objects are passed through without - recoding them. Instead, the implementation assumes that the string object uses +``et`` (:class:`str`, :class:`bytes` or :class:`bytearray`) [const char \*encoding, char \*\*buffer] + Same as ``es`` except that byte string objects are passed through without + recoding them. Instead, the implementation assumes that the byte string object uses the encoding passed in as parameter. -``es#`` (string, Unicode object or character buffer compatible object) [const char \*encoding, char \*\*buffer, int \*buffer_length] +``es#`` (:class:`str`) [const char \*encoding, char \*\*buffer, int \*buffer_length] This variant on ``s#`` is used for encoding Unicode and objects convertible to Unicode into a character buffer. Unlike the ``es`` format, this variant allows input data which contains NUL characters. @@ -226,9 +227,9 @@ In both cases, *\*buffer_length* is set to the length of the encoded data without the trailing NUL byte. -``et#`` (string, Unicode object or character buffer compatible object) [const char \*encoding, char \*\*buffer, int \*buffer_length] - Same as ``es#`` except that string objects are passed through without recoding - them. Instead, the implementation assumes that the string object uses the +``et#`` (:class:`str`, :class:`bytes` or :class:`bytearray`) [const char \*encoding, char \*\*buffer, int \*buffer_length] + Same as ``es#`` except that byte string objects are passed through without recoding + them. Instead, the implementation assumes that the byte string object uses the encoding passed in as parameter. Numbers @@ -276,12 +277,12 @@ ``n`` (integer) [Py_ssize_t] Convert a Python integer to a C :ctype:`Py_ssize_t`. -``c`` (bytes object of length 1) [char] +``c`` (:class:`bytes` of length 1) [char] Convert a Python byte, represented as a :class:`bytes` object of length 1, to a C :ctype:`char`. ``C`` (Unicode object of length 1) [int] - Convert a Python character, represented as a :class:`str`: object of + Convert a Python character, represented as a :class:`str` object of length 1, to a C :ctype:`int`. ``f`` (float) [float] @@ -498,44 +499,42 @@ not within format units such as ``s#``). This can be used to make long format strings a tad more readable. - ``s`` (string) [char \*] - Convert a null-terminated C string to a Python object. If the C string pointer + ``s`` (:class:`str`) [char \*] + Convert a null-terminated C string to a Python object using ``'utf-8'`` encoding. If the C string pointer is *NULL*, ``None`` is used. - ``s#`` (string) [char \*, int] - Convert a C string and its length to a Python object. If the C string pointer + ``s#`` (:class:`str`) [char \*, int] + Convert a C string and its length to a Python object using ``'utf-8'`` encoding. If the C string pointer is *NULL*, the length is ignored and ``None`` is returned. - ``y`` (bytes) [char \*] - This converts a C string to a Python :func:`bytes` object. If the C + ``y`` (:class:`bytes`) [char \*] + This converts a C string to a Python :func:`bytes` object. If the C string pointer is *NULL*, ``None`` is returned. - ``y#`` (bytes) [char \*, int] - This converts a C string and its lengths to a Python object. If the C + ``y#`` (:class:`bytes`) [char \*, int] + This converts a C string and its lengths to a Python :func:`bytes` object. If the C string pointer is *NULL*, ``None`` is returned. - ``z`` (string or ``None``) [char \*] + ``z`` (:class:`str` or ``None``) [char \*] Same as ``s``. - ``z#`` (string or ``None``) [char \*, int] + ``z#`` (:class:`str` or ``None``) [char \*, int] Same as ``s#``. - ``u`` (Unicode string) [Py_UNICODE \*] + ``u`` (:class:`str`) [Py_UNICODE \*] Convert a null-terminated buffer of Unicode (UCS-2 or UCS-4) data to a Python Unicode object. If the Unicode buffer pointer is *NULL*, ``None`` is returned. - ``u#`` (Unicode string) [Py_UNICODE \*, int] + ``u#`` (:class:`str`) [Py_UNICODE \*, int] Convert a Unicode (UCS-2 or UCS-4) data buffer and its length to a Python Unicode object. If the Unicode buffer pointer is *NULL*, the length is ignored and ``None`` is returned. - ``U`` (string) [char \*] - Convert a null-terminated C string to a Python unicode object. If the C string - pointer is *NULL*, ``None`` is used. + ``U`` (:class:`str`) [char \*] + Deprecated alias to ``s``. - ``U#`` (string) [char \*, int] - Convert a C string and its length to a Python unicode object. If the C string - pointer is *NULL*, the length is ignored and ``None`` is returned. + ``U#`` (:class:`str`) [char \*, int] + Deprecated alias to ``s#``. ``i`` (integer) [int] Convert a plain C :ctype:`int` to a Python integer object. @@ -572,13 +571,13 @@ ``n`` (int) [Py_ssize_t] Convert a C :ctype:`Py_ssize_t` to a Python integer. - ``c`` (string of length 1) [char] - Convert a C :ctype:`int` representing a byte to a Python byte string of + ``c`` (:class:`bytes` of length 1) [char] + Convert a C :ctype:`int` representing a byte to a Python :class:`bytes` object of length 1. - ``C`` (string of length 1) [int] - Convert a C :ctype:`int` representing a character to Python unicode - string of length 1. + ``C`` (:class:`str` of length 1) [int] + Convert a C :ctype:`int` representing a character to Python :class:`str` + object of length 1. ``d`` (float) [double] Convert a C :ctype:`double` to a Python floating point number.