Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 20d3255

Browse files
committed
Issue #15984: Correct PyUnicode_FromObject() and _FromEncodedObject() docs
1 parent 6245cb3 commit 20d3255

2 files changed

Lines changed: 15 additions & 21 deletions

File tree

Doc/c-api/unicode.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -556,14 +556,13 @@ APIs:
556556
.. c:function:: PyObject* PyUnicode_FromEncodedObject(PyObject *obj, \
557557
const char *encoding, const char *errors)
558558
559-
Coerce an encoded object *obj* to a Unicode object and return a reference with
560-
incremented refcount.
559+
Decode an encoded object *obj* to a Unicode object.
561560
562561
:class:`bytes`, :class:`bytearray` and other
563562
:term:`bytes-like objects <bytes-like object>`
564563
are decoded according to the given *encoding* and using the error handling
565564
defined by *errors*. Both can be *NULL* to have the interface use the default
566-
values (see the next section for details).
565+
values (see :ref:`builtincodecs` for details).
567566
568567
All other objects, including Unicode objects, cause a :exc:`TypeError` to be
569568
set.
@@ -745,8 +744,11 @@ Extension modules can continue using them, as they will not be removed in Python
745744
746745
.. c:function:: PyObject* PyUnicode_FromObject(PyObject *obj)
747746
748-
Shortcut for ``PyUnicode_FromEncodedObject(obj, NULL, "strict")`` which is used
749-
throughout the interpreter whenever coercion to Unicode is needed.
747+
Copy an instance of a Unicode subtype to a new true Unicode object if
748+
necessary. If *obj* is already a true Unicode object (not a subtype),
749+
return the reference with incremented refcount.
750+
751+
Objects other than Unicode or its subtypes will cause a :exc:`TypeError`.
750752
751753
752754
Locale Encoding

Include/unicodeobject.h

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -844,17 +844,13 @@ PyAPI_FUNC(int) PyUnicode_Resize(
844844
Py_ssize_t length /* New length */
845845
);
846846

847-
/* Coerce obj to a Unicode object and return a reference with
848-
*incremented* refcount.
847+
/* Decode obj to an Unicode object.
849848
850-
Coercion is done in the following way:
849+
bytes, bytearray and other bytes-like objects are decoded according to the
850+
given encoding and error handler. The encoding and error handler can be
851+
NULL to have the interface use UTF-8 and "strict".
851852
852-
1. bytes, bytearray and other bytes-like objects are decoded
853-
under the assumptions that they contain data using the UTF-8
854-
encoding. Decoding is done in "strict" mode.
855-
856-
2. All other objects (including Unicode objects) raise an
857-
exception.
853+
All other objects (including Unicode objects) raise an exception.
858854
859855
The API returns NULL in case of an error. The caller is responsible
860856
for decref'ing the returned objects.
@@ -867,13 +863,9 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromEncodedObject(
867863
const char *errors /* error handling */
868864
);
869865

870-
/* Coerce obj to a Unicode object and return a reference with
871-
*incremented* refcount.
872-
873-
Unicode objects are passed back as-is (subclasses are converted to
874-
true Unicode objects), all other objects are delegated to
875-
PyUnicode_FromEncodedObject(obj, NULL, "strict") which results in
876-
using UTF-8 encoding as basis for decoding the object.
866+
/* Copy an instance of a Unicode subtype to a new true Unicode object if
867+
necessary. If obj is already a true Unicode object (not a subtype), return
868+
the reference with *incremented* refcount.
877869
878870
The API returns NULL in case of an error. The caller is responsible
879871
for decref'ing the returned objects.

0 commit comments

Comments
 (0)