@@ -2675,7 +2675,7 @@ SimpleExtendsException(PyExc_ValueError, UnicodeError,
26752675 * The 'name' is the attribute name and is only used for error reporting.
26762676 *
26772677 * On success, this returns a strong reference on 'attr'.
2678- * On failure, this sets an exception and returns NULL.
2678+ * On failure, this sets a TypeError and returns NULL.
26792679 */
26802680static PyObject *
26812681as_unicode_error_attribute (PyObject * attr , const char * name , int as_bytes )
@@ -2709,10 +2709,18 @@ as_unicode_error_attribute(PyObject *attr, const char *name, int as_bytes)
27092709
27102710
27112711/*
2712- * Check that 'self' is of a Unicode Error object.
2712+ * Check that 'self' is of a UnicodeError object.
27132713 *
27142714 * On success, this returns 0.
27152715 * On failure, this sets a TypeError exception and returns -1.
2716+ *
2717+ * The 'expect_type' is the name of the expected type, which is
2718+ * only used for error reporting.
2719+ *
2720+ * As an implementation detail, the `PyUnicode*Error_*` functions
2721+ * currently allow *any* subclass of UnicodeError as 'self'.
2722+ *
2723+ * Use one of the `Py_UNICODE_*_ERROR_NAME` macros to avoid typos.
27162724 */
27172725static inline int
27182726check_unicode_error_type (PyObject * self , const char * expect_type )
@@ -2727,11 +2735,14 @@ check_unicode_error_type(PyObject *self, const char *expect_type)
27272735}
27282736
27292737
2738+ // --- PyUnicodeEncodeObject: internal helpers --------------------------------
2739+ //
2740+ // In the helpers below, the caller is responsible to ensure that 'self'
2741+ // is a PyUnicodeErrorObject, although this condition is verified by this
2742+ // function on DEBUG builds through PyUnicodeError_CAST().
2743+
27302744/*
2731- * Return the underlying (str) 'encoding' attribute of a Unicode Error object.
2732- *
2733- * The caller is responsible to ensure that 'self' is a PyUnicodeErrorObject,
2734- * although this condition is verified by this function on DEBUG builds.
2745+ * Return the underlying (str) 'encoding' attribute of a UnicodeError object.
27352746 */
27362747static inline PyObject *
27372748unicode_error_get_encoding_impl (PyObject * self )
@@ -2743,11 +2754,8 @@ unicode_error_get_encoding_impl(PyObject *self)
27432754
27442755
27452756/*
2746- * Return the underlying 'object' attribute of a Unicode Error object
2757+ * Return the underlying 'object' attribute of a UnicodeError object
27472758 * as a bytes or a string instance, depending on the 'as_bytes' flag.
2748- *
2749- * The caller is responsible to ensure that 'self' is a PyUnicodeErrorObject,
2750- * although this condition is verified by this function on DEBUG builds.
27512759 */
27522760static inline PyObject *
27532761unicode_error_get_object_impl (PyObject * self , int as_bytes )
@@ -2759,9 +2767,7 @@ unicode_error_get_object_impl(PyObject *self, int as_bytes)
27592767
27602768
27612769/*
2762- * Return the underlying (str) 'reason' attribute of a Unicode Error object.
2763- *
2764- * The caller is responsible to ensure that 'self' is a PyUnicodeErrorObject.
2770+ * Return the underlying (str) 'reason' attribute of a UnicodeError object.
27652771 */
27662772static inline PyObject *
27672773unicode_error_get_reason_impl (PyObject * self )
@@ -2773,10 +2779,7 @@ unicode_error_get_reason_impl(PyObject *self)
27732779
27742780
27752781/*
2776- * Set the underlying (str) 'reason' attribute of a Unicode Error object.
2777- *
2778- * The caller is responsible to ensure that 'self' is a PyUnicodeErrorObject,
2779- * although this condition is verified by this function on DEBUG builds.
2782+ * Set the underlying (str) 'reason' attribute of a UnicodeError object.
27802783 *
27812784 * Return 0 on success and -1 on failure.
27822785 */
@@ -2795,10 +2798,7 @@ unicode_error_set_reason_impl(PyObject *self, const char *reason)
27952798
27962799
27972800/*
2798- * Set the 'start' attribute of a Unicode Error object.
2799- *
2800- * The caller is responsible to ensure that 'self' is a PyUnicodeErrorObject,
2801- * although this condition is verified by this function on DEBUG builds.
2801+ * Set the 'start' attribute of a UnicodeError object.
28022802 *
28032803 * Return 0 on success and -1 on failure.
28042804 */
@@ -2813,10 +2813,7 @@ unicode_error_set_start_impl(PyObject *self, Py_ssize_t start)
28132813
28142814
28152815/*
2816- * Set the 'end' attribute of a Unicode Error object.
2817- *
2818- * The caller is responsible to ensure that 'self' is a PyUnicodeErrorObject,
2819- * although this condition is verified by this function on DEBUG builds.
2816+ * Set the 'end' attribute of a UnicodeError object.
28202817 *
28212818 * Return 0 on success and -1 on failure.
28222819 */
@@ -2874,23 +2871,30 @@ unicode_error_adjust_end(Py_ssize_t end, Py_ssize_t objlen)
28742871
28752872
28762873/*
2877- * Get various common parameters of a Unicode Error object.
2874+ * Get various common parameters of a UnicodeError object.
28782875 *
28792876 * The caller is responsible to ensure that 'self' is a PyUnicodeErrorObject,
28802877 * although this condition is verified by this function on DEBUG builds.
28812878 *
28822879 * Return 0 on success and -1 on failure.
28832880 *
2884- * Parameters
2881+ * Output parameters:
28852882 *
2886- * obj The retrieved underlying 'object'.
2883+ * obj A strong reference to the 'object' attribute .
28872884 * objlen The 'object' length.
28882885 * start The clipped 'start' attribute.
28892886 * end The clipped 'end' attribute.
2890- * as_bytes Indicate whether the underlying 'object' is a bytes object.
28912887 *
2892- * The 'obj', 'objlen', 'start' and 'end' parameters may be NULL
2893- * to indicate that the parameter does not need to be stored.
2888+ * An output parameter can be NULL to indicate that
2889+ * the corresponding value does not need to be stored.
2890+ *
2891+ * Input parameter:
2892+ *
2893+ * as_bytes If 1, the error's 'object' attribute must be a bytes object,
2894+ * i.e. the call is for a `UnicodeDecodeError`. Otherwise, the
2895+ * 'object' attribute must be a string.
2896+ *
2897+ * A TypeError is raised if the 'object' type is incompatible.
28942898 */
28952899int
28962900_PyUnicodeError_GetParams (PyObject * self ,
0 commit comments