Skip to content

Commit 01b5f22

Browse files
committed
unify even more the interface using a generic getter
1 parent a4b01f0 commit 01b5f22

File tree

2 files changed

+79
-98
lines changed

2 files changed

+79
-98
lines changed

Include/cpython/pyerrors.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ PyAPI_FUNC(void) _PyErr_ChainExceptions1(PyObject *);
9494

9595
/* In exceptions.c */
9696

97+
PyAPI_FUNC(int) _PyUnicodeError_GetParams(
98+
PyObject *self,
99+
PyObject **obj, Py_ssize_t *objlen,
100+
Py_ssize_t *start, Py_ssize_t *end, int *consistent,
101+
int as_bytes);
102+
97103
PyAPI_FUNC(PyObject*) PyUnstable_Exc_PrepReraiseStar(
98104
PyObject *orig,
99105
PyObject *excs);

Objects/exceptions.c

Lines changed: 73 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,48 +2759,41 @@ unicode_error_set_reason_impl(PyObject *self, const char *reason)
27592759

27602760

27612761
/*
2762-
* Get the underlying 'object' attribute of a Unicode Error object
2763-
* as a bytes or a string instance, depending on the 'as_bytes' flag.
2762+
* Set the 'start' attribute of a Unicode Error object.
2763+
*
2764+
* The caller is responsible to ensure that 'self' is a PyUnicodeErrorObject.
27642765
*
2765-
* The result is stored in 'result' and its size in 'size',
2766-
* which can be NULL to indicate that the value would be
2767-
* discarded after the call.
2766+
* Return 0 on success and -1 on failure.
2767+
*/
2768+
static inline int
2769+
unicode_error_set_start_impl(PyObject *self, Py_ssize_t start)
2770+
{
2771+
// TODO(picnixz): do an assert-only type-check when gh-127694 is merged
2772+
// (the caller function must do an eager type-check)
2773+
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
2774+
exc->start = start;
2775+
return 0;
2776+
}
2777+
2778+
2779+
/*
2780+
* Set the 'end' attribute of a Unicode Error object.
27682781
*
27692782
* The caller is responsible to ensure that 'self' is a PyUnicodeErrorObject.
27702783
*
27712784
* Return 0 on success and -1 on failure.
27722785
*/
2773-
static int
2774-
unicode_error_get_object_and_size(PyObject *self,
2775-
PyObject **result, Py_ssize_t *size,
2776-
int as_bytes)
2786+
static inline int
2787+
unicode_error_set_end_impl(PyObject *self, Py_ssize_t end)
27772788
{
2778-
assert(as_bytes == 0 || as_bytes == 1);
27792789
// TODO(picnixz): do an assert-only type-check when gh-127694 is merged
27802790
// (the caller function must do an eager type-check)
27812791
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
2782-
PyObject *obj = as_unicode_error_attribute(exc->object, "object", as_bytes);
2783-
if (obj == NULL) {
2784-
if (result != NULL) {
2785-
*result = NULL;
2786-
}
2787-
if (size != NULL) {
2788-
*size = -1;
2789-
}
2790-
return -1;
2791-
}
2792-
if (size != NULL) {
2793-
*size = as_bytes ? PyBytes_GET_SIZE(obj) : PyUnicode_GET_LENGTH(obj);
2794-
}
2795-
if (result != NULL) {
2796-
*result = obj;
2797-
}
2798-
else {
2799-
Py_DECREF(obj);
2800-
}
2792+
exc->end = end;
28012793
return 0;
28022794
}
28032795

2796+
// --- PyUnicodeEncodeObject: internal getters --------------------------------
28042797

28052798
/*
28062799
* Adjust the (inclusive) 'start' value of a UnicodeError object.
@@ -2845,85 +2838,67 @@ unicode_error_adjust_end(Py_ssize_t end, Py_ssize_t objlen)
28452838

28462839

28472840
/*
2848-
* Retrieve and adjust the 'start' attribute of a Unicode Error object.
2841+
* Get various common parameters of a Unicode Error object.
28492842
*
28502843
* The caller is responsible to ensure that 'self' is a PyUnicodeErrorObject.
28512844
*
28522845
* Return 0 on success and -1 on failure.
2853-
*/
2854-
static inline int
2855-
unicode_error_get_start_impl(PyObject *self, Py_ssize_t *start, int as_bytes)
2856-
{
2857-
Py_ssize_t size;
2858-
if (unicode_error_get_object_and_size(self, NULL, &size, as_bytes) < 0) {
2859-
assert(size == -1);
2860-
return -1;
2861-
}
2862-
// TODO(picnixz): do an assert-only type-check when gh-127694 is merged
2863-
// (the caller function must do an eager type-check)
2864-
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
2865-
*start = unicode_error_adjust_start(exc->start, size);
2866-
assert(*start >= 0 && *start <= size);
2867-
return 0;
2868-
}
2869-
2870-
2871-
/*
2872-
* Set the 'start' attribute of a Unicode Error object.
28732846
*
2874-
* The caller is responsible to ensure that 'self' is a PyUnicodeErrorObject.
2847+
* Parameters
28752848
*
2876-
* Return 0 on success and -1 on failure.
2849+
* obj The retrieved underlying 'object'.
2850+
* objlen The 'object' length.
2851+
* start The clipped 'start' attribute.
2852+
* end The clipped 'end' attribute.
2853+
* consistent Indicate whetehr 'start' and 'end' are consistent.
2854+
* as_bytes Indicate whether the underlying 'object' is a bytes object.
2855+
*
2856+
* The 'obj', 'objlen', 'start', 'end' and 'consistent' parameters may
2857+
* be NULL to indicate that the parameter does not need to be stored.
2858+
*
2859+
* The 'consistent' value is only set if 'start', and 'end' are retrieved.
28772860
*/
2878-
static inline int
2879-
unicode_error_set_start_impl(PyObject *self, Py_ssize_t start)
2861+
int
2862+
_PyUnicodeError_GetParams(PyObject *self,
2863+
PyObject **obj, Py_ssize_t *objlen,
2864+
Py_ssize_t *start, Py_ssize_t *end, int *consistent,
2865+
int as_bytes)
28802866
{
2867+
assert(as_bytes == 0 || as_bytes == 1);
28812868
// TODO(picnixz): do an assert-only type-check when gh-127694 is merged
28822869
// (the caller function must do an eager type-check)
28832870
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
2884-
exc->start = start;
2885-
return 0;
2886-
}
2871+
PyObject *r = as_unicode_error_attribute(exc->object, "object", as_bytes);
2872+
if (r == NULL) {
2873+
return -1;
2874+
}
28872875

2876+
Py_ssize_t n = as_bytes ? PyBytes_GET_SIZE(r) : PyUnicode_GET_LENGTH(r);
2877+
if (objlen != NULL) {
2878+
*objlen = n;
2879+
}
28882880

2889-
/*
2890-
* Retrieve and adjust the 'end' attribute of a Unicode Error object.
2891-
*
2892-
* The caller is responsible to ensure that 'self' is a PyUnicodeErrorObject.
2893-
*
2894-
* Return 0 on success and -1 on failure.
2895-
*/
2896-
static inline int
2897-
unicode_error_get_end_impl(PyObject *self, Py_ssize_t *end, int as_bytes)
2898-
{
2899-
Py_ssize_t size;
2900-
if (unicode_error_get_object_and_size(self, NULL, &size, as_bytes) < 0) {
2901-
assert(size == -1);
2902-
return -1;
2881+
if (start != NULL) {
2882+
*start = unicode_error_adjust_start(exc->start, n);
2883+
assert(*start >= 0);
2884+
assert(*start <= n);
2885+
}
2886+
if (end != NULL) {
2887+
*end = unicode_error_adjust_end(exc->end, n);
2888+
assert(*end >= 0);
2889+
assert(*end <= n);
29032890
}
2904-
// TODO(picnixz): do an assert-only type-check when gh-127694 is merged
2905-
// (the caller function must do an eager type-check)
2906-
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
2907-
*end = unicode_error_adjust_end(exc->end, size);
2908-
assert(*end >= 0 && *end <= size);
2909-
return 0;
2910-
}
29112891

2892+
if (start != NULL && end != NULL && consistent != NULL) {
2893+
*consistent = *start < *end ? 1 : 0;
2894+
}
29122895

2913-
/*
2914-
* Set the 'end' attribute of a Unicode Error object.
2915-
*
2916-
* The caller is responsible to ensure that 'self' is a PyUnicodeErrorObject.
2917-
*
2918-
* Return 0 on success and -1 on failure.
2919-
*/
2920-
static inline int
2921-
unicode_error_set_end_impl(PyObject *self, Py_ssize_t end)
2922-
{
2923-
// TODO(picnixz): do an assert-only type-check when gh-127694 is merged
2924-
// (the caller function must do an eager type-check)
2925-
PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
2926-
exc->end = end;
2896+
if (obj != NULL) {
2897+
*obj = r;
2898+
}
2899+
else {
2900+
Py_DECREF(r);
2901+
}
29272902
return 0;
29282903
}
29292904

@@ -2969,21 +2944,21 @@ PyUnicodeTranslateError_GetObject(PyObject *self)
29692944
int
29702945
PyUnicodeEncodeError_GetStart(PyObject *self, Py_ssize_t *start)
29712946
{
2972-
return unicode_error_get_start_impl(self, start, false);
2947+
return _PyUnicodeError_GetParams(self, NULL, NULL, start, NULL, NULL, false);
29732948
}
29742949

29752950

29762951
int
29772952
PyUnicodeDecodeError_GetStart(PyObject *self, Py_ssize_t *start)
29782953
{
2979-
return unicode_error_get_start_impl(self, start, true);
2954+
return _PyUnicodeError_GetParams(self, NULL, NULL, start, NULL, NULL, true);
29802955
}
29812956

29822957

29832958
int
29842959
PyUnicodeTranslateError_GetStart(PyObject *self, Py_ssize_t *start)
29852960
{
2986-
return unicode_error_get_start_impl(self, start, false);
2961+
return _PyUnicodeError_GetParams(self, NULL, NULL, start, NULL, NULL, false);
29872962
}
29882963

29892964
// --- PyUnicodeEncodeObject: 'start' setters ---------------------------------
@@ -3013,21 +2988,21 @@ PyUnicodeTranslateError_SetStart(PyObject *self, Py_ssize_t start)
30132988
int
30142989
PyUnicodeEncodeError_GetEnd(PyObject *self, Py_ssize_t *end)
30152990
{
3016-
return unicode_error_get_end_impl(self, end, false);
2991+
return _PyUnicodeError_GetParams(self, NULL, NULL, NULL, end, NULL, false);
30172992
}
30182993

30192994

30202995
int
30212996
PyUnicodeDecodeError_GetEnd(PyObject *self, Py_ssize_t *end)
30222997
{
3023-
return unicode_error_get_end_impl(self, end, true);
2998+
return _PyUnicodeError_GetParams(self, NULL, NULL, NULL, end, NULL, true);
30242999
}
30253000

30263001

30273002
int
30283003
PyUnicodeTranslateError_GetEnd(PyObject *self, Py_ssize_t *end)
30293004
{
3030-
return unicode_error_get_end_impl(self, end, false);
3005+
return _PyUnicodeError_GetParams(self, NULL, NULL, NULL, end, NULL, false);
30313006
}
30323007

30333008
// --- PyUnicodeEncodeObject: 'end' setters -----------------------------------

0 commit comments

Comments
 (0)