Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'main' into feat/exc/unicode-error-refactor-127787
  • Loading branch information
picnixz committed Dec 13, 2024
commit 3078f233e47ef9b1719a0e16926c5d2d2d80d330
124 changes: 86 additions & 38 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2690,6 +2690,33 @@ as_unicode_error_attribute(PyObject *attr, const char *name, int as_bytes)
}


#define _PyUnicodeError_CAST(PTR) ((PyUnicodeErrorObject *)(PTR))
#define PyUnicodeError_Check(PTR) \
PyObject_TypeCheck((PTR), (PyTypeObject *)PyExc_UnicodeError)
#define PyUnicodeError_CAST(PTR) \
(assert(PyUnicodeError_Check(PTR)), _PyUnicodeError_CAST(PTR))


static inline int
check_unicode_error_type(PyObject *self, const char *expect_type)
{
if (!PyUnicodeError_Check(self)) {
PyErr_Format(PyExc_TypeError,
"expecting a %s object, got %T", expect_type, self);
return -1;
}
return 0;
}


static inline PyUnicodeErrorObject *
as_unicode_error(PyObject *self, const char *expect_type)
{
int rc = check_unicode_error_type(self, expect_type);
return rc < 0 ? NULL : _PyUnicodeError_CAST(self);
}


/*
* Return the underlying (str) 'encoding' attribute of a Unicode Error object.
*
Expand All @@ -2698,9 +2725,7 @@ as_unicode_error_attribute(PyObject *attr, const char *name, int as_bytes)
static inline PyObject *
unicode_error_get_encoding_impl(PyObject *self)
{
// TODO(picnixz): do an assert-only type-check when gh-127694 is merged
// (the caller function must do an eager type-check)
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
return as_unicode_error_attribute(exc->encoding, "encoding", false);
}

Expand All @@ -2714,9 +2739,7 @@ unicode_error_get_encoding_impl(PyObject *self)
static inline PyObject *
unicode_error_get_object_impl(PyObject *self, int as_bytes)
{
// TODO(picnixz): do an assert-only type-check when gh-127694 is merged
// (the caller function must do an eager type-check)
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
return as_unicode_error_attribute(exc->object, "object", as_bytes);
}

Expand All @@ -2729,9 +2752,7 @@ unicode_error_get_object_impl(PyObject *self, int as_bytes)
static inline PyObject *
unicode_error_get_reason_impl(PyObject *self)
{
// TODO(picnixz): do an assert-only type-check when gh-127694 is merged
// (the caller function must do an eager type-check)
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
return as_unicode_error_attribute(exc->reason, "reason", false);
}

Expand All @@ -2750,9 +2771,7 @@ unicode_error_set_reason_impl(PyObject *self, const char *reason)
if (!value) {
return -1;
}
// TODO(picnixz): do an assert-only type-check when gh-127694 is merged
// (the caller function must do an eager type-check)
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
Py_XSETREF(exc->reason, value);
return 0;
}
Expand All @@ -2768,9 +2787,7 @@ unicode_error_set_reason_impl(PyObject *self, const char *reason)
static inline int
unicode_error_set_start_impl(PyObject *self, Py_ssize_t start)
{
// TODO(picnixz): do an assert-only type-check when gh-127694 is merged
// (the caller function must do an eager type-check)
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
exc->start = start;
return 0;
}
Expand All @@ -2786,9 +2803,7 @@ unicode_error_set_start_impl(PyObject *self, Py_ssize_t start)
static inline int
unicode_error_set_end_impl(PyObject *self, Py_ssize_t end)
{
// TODO(picnixz): do an assert-only type-check when gh-127694 is merged
// (the caller function must do an eager type-check)
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
exc->end = end;
return 0;
}
Expand Down Expand Up @@ -2862,9 +2877,7 @@ _PyUnicodeError_GetParams(PyObject *self,
int as_bytes)
{
assert(as_bytes == 0 || as_bytes == 1);
// TODO(picnixz): do an assert-only type-check when gh-127694 is merged
// (the caller function must do an eager type-check)
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
PyObject *r = as_unicode_error_attribute(exc->object, "object", as_bytes);
if (r == NULL) {
return -1;
Expand Down Expand Up @@ -2898,57 +2911,71 @@ _PyUnicodeError_GetParams(PyObject *self,
PyObject *
PyUnicodeEncodeError_GetEncoding(PyObject *self)
{
return unicode_error_get_encoding_impl(self);
int rc = check_unicode_error_type(self, "UnicodeEncodeError");
return rc < 0 ? NULL : unicode_error_get_encoding_impl(self);
}


PyObject *
PyUnicodeDecodeError_GetEncoding(PyObject *self)
{
return unicode_error_get_encoding_impl(self);
int rc = check_unicode_error_type(self, "UnicodeDecodeError");
return rc < 0 ? NULL : unicode_error_get_encoding_impl(self);
}

// --- PyUnicodeEncodeObject: 'object' getters --------------------------------

PyObject *
PyUnicodeEncodeError_GetObject(PyObject *self)
{
return unicode_error_get_object_impl(self, false);
int rc = check_unicode_error_type(self, "UnicodeEncodeError");
return rc < 0 ? NULL : unicode_error_get_object_impl(self, false);
}


PyObject *
PyUnicodeDecodeError_GetObject(PyObject *self)
{
return unicode_error_get_object_impl(self, true);
int rc = check_unicode_error_type(self, "UnicodeDecodeError");
return rc < 0 ? NULL : unicode_error_get_object_impl(self, true);
}


PyObject *
PyUnicodeTranslateError_GetObject(PyObject *self)
{
return unicode_error_get_object_impl(self, false);
int rc = check_unicode_error_type(self, "UnicodeTranslateError");
return rc < 0 ? NULL : unicode_error_get_object_impl(self, false);
}

// --- PyUnicodeEncodeObject: 'start' getters ---------------------------------

int
PyUnicodeEncodeError_GetStart(PyObject *self, Py_ssize_t *start)
{
if (check_unicode_error_type(self, "UnicodeEncodeError") < 0) {
return -1;
}
return _PyUnicodeError_GetParams(self, NULL, NULL, start, NULL, false);
}


int
PyUnicodeDecodeError_GetStart(PyObject *self, Py_ssize_t *start)
{
if (check_unicode_error_type(self, "UnicodeDecodeError") < 0) {
return -1;
}
return _PyUnicodeError_GetParams(self, NULL, NULL, start, NULL, true);
}


int
PyUnicodeTranslateError_GetStart(PyObject *self, Py_ssize_t *start)
{
if (check_unicode_error_type(self, "UnicodeTranslateError") < 0) {
return -1;
}
return _PyUnicodeError_GetParams(self, NULL, NULL, start, NULL, false);
}

Expand All @@ -2957,42 +2984,54 @@ PyUnicodeTranslateError_GetStart(PyObject *self, Py_ssize_t *start)
int
PyUnicodeEncodeError_SetStart(PyObject *self, Py_ssize_t start)
{
return unicode_error_set_start_impl(self, start);
int rc = check_unicode_error_type(self, "UnicodeEncodeError");
return rc < 0 ? -1 : unicode_error_set_start_impl(self, start);
}


int
PyUnicodeDecodeError_SetStart(PyObject *self, Py_ssize_t start)
{
return unicode_error_set_start_impl(self, start);
int rc = check_unicode_error_type(self, "UnicodeDecodeError");
return rc < 0 ? -1 : unicode_error_set_start_impl(self, start);
}


int
PyUnicodeTranslateError_SetStart(PyObject *self, Py_ssize_t start)
{
return unicode_error_set_start_impl(self, start);
int rc = check_unicode_error_type(self, "UnicodeTranslateError");
return rc < 0 ? -1 : unicode_error_set_start_impl(self, start);
}

// --- PyUnicodeEncodeObject: 'end' getters -----------------------------------

int
PyUnicodeEncodeError_GetEnd(PyObject *self, Py_ssize_t *end)
{
if (check_unicode_error_type(self, "UnicodeEncodeError") < 0) {
return -1;
}
return _PyUnicodeError_GetParams(self, NULL, NULL, NULL, end, false);
}


int
PyUnicodeDecodeError_GetEnd(PyObject *self, Py_ssize_t *end)
{
if (check_unicode_error_type(self, "UnicodeDecodeError") < 0) {
return -1;
}
return _PyUnicodeError_GetParams(self, NULL, NULL, NULL, end, true);
}


int
PyUnicodeTranslateError_GetEnd(PyObject *self, Py_ssize_t *end)
{
if (check_unicode_error_type(self, "UnicodeTranslateError") < 0) {
return -1;
}
return _PyUnicodeError_GetParams(self, NULL, NULL, NULL, end, false);
}

Expand All @@ -3001,65 +3040,74 @@ PyUnicodeTranslateError_GetEnd(PyObject *self, Py_ssize_t *end)
int
PyUnicodeEncodeError_SetEnd(PyObject *self, Py_ssize_t end)
{
return unicode_error_set_end_impl(self, end);
int rc = check_unicode_error_type(self, "UnicodeEncodeError");
return rc < 0 ? -1 : unicode_error_set_end_impl(self, end);
}


int
PyUnicodeDecodeError_SetEnd(PyObject *self, Py_ssize_t end)
{
return unicode_error_set_end_impl(self, end);
int rc = check_unicode_error_type(self, "UnicodeDecodeError");
return rc < 0 ? -1 : unicode_error_set_end_impl(self, end);
}


int
PyUnicodeTranslateError_SetEnd(PyObject *self, Py_ssize_t end)
{
return unicode_error_set_end_impl(self, end);
int rc = check_unicode_error_type(self, "UnicodeTranslateError");
return rc < 0 ? -1 : unicode_error_set_end_impl(self, end);
}

// --- PyUnicodeEncodeObject: 'reason' getters --------------------------------

PyObject *
PyUnicodeEncodeError_GetReason(PyObject *self)
{
return unicode_error_get_reason_impl(self);
int rc = check_unicode_error_type(self, "UnicodeEncodeError");
return rc < 0 ? NULL : unicode_error_get_reason_impl(self);
}


PyObject *
PyUnicodeDecodeError_GetReason(PyObject *self)
{
return unicode_error_get_reason_impl(self);
int rc = check_unicode_error_type(self, "UnicodeDecodeError");
return rc < 0 ? NULL : unicode_error_get_reason_impl(self);
}


PyObject *
PyUnicodeTranslateError_GetReason(PyObject *self)
{
return unicode_error_get_reason_impl(self);
int rc = check_unicode_error_type(self, "UnicodeTranslateError");
return rc < 0 ? NULL : unicode_error_get_reason_impl(self);
}

// --- PyUnicodeEncodeObject: 'reason' setters --------------------------------

int
PyUnicodeEncodeError_SetReason(PyObject *self, const char *reason)
{
return unicode_error_set_reason_impl(self, reason);
int rc = check_unicode_error_type(self, "UnicodeEncodeError");
return rc < 0 ? -1 : unicode_error_set_reason_impl(self, reason);
}


int
PyUnicodeDecodeError_SetReason(PyObject *self, const char *reason)
{
return unicode_error_set_reason_impl(self, reason);
int rc = check_unicode_error_type(self, "UnicodeDecodeError");
return rc < 0 ? -1 : unicode_error_set_reason_impl(self, reason);
}


int
PyUnicodeTranslateError_SetReason(PyObject *self, const char *reason)
{
return unicode_error_set_reason_impl(self, reason);
int rc = check_unicode_error_type(self, "UnicodeTranslateError");
return rc < 0 ? -1 : unicode_error_set_reason_impl(self, reason);
}


Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.