https://github.com/python/cpython/commit/45538495bee3f9502effec1122f22556744eac36
commit: 45538495bee3f9502effec1122f22556744eac36
branch: main
author: Donghee Na <[email protected]>
committer: corona10 <[email protected]>
date: 2026-10-23T02:31:41Z
summary:

gh-114258: Update Argument Clinic for ``__str__``. (gh-160402)

files:
M Objects/clinic/exceptions.c.h
M Objects/clinic/frameobject.c.h
M Objects/exceptions.c
M Objects/frameobject.c
M Tools/clinic/libclinic/dsl_parser.py
M Tools/clinic/libclinic/slots.py

diff --git a/Objects/clinic/exceptions.c.h b/Objects/clinic/exceptions.c.h
index 31be6c3a2bb2d7..c06cdb0444ee97 100644
--- a/Objects/clinic/exceptions.c.h
+++ b/Objects/clinic/exceptions.c.h
@@ -5,6 +5,36 @@ preserve
 #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
 #include "pycore_modsupport.h"    // _PyArg_BadArgument()
 
+static PyObject *
+BaseException_str_impl(PyBaseExceptionObject *self);
+
+static PyObject *
+BaseException_str(PyObject *self)
+{
+    PyObject *return_value = NULL;
+
+    Py_BEGIN_CRITICAL_SECTION(self);
+    return_value = BaseException_str_impl((PyBaseExceptionObject *)self);
+    Py_END_CRITICAL_SECTION();
+
+    return return_value;
+}
+
+static PyObject *
+BaseException_repr_impl(PyBaseExceptionObject *self);
+
+static PyObject *
+BaseException_repr(PyObject *self)
+{
+    PyObject *return_value = NULL;
+
+    Py_BEGIN_CRITICAL_SECTION(self);
+    return_value = BaseException_repr_impl((PyBaseExceptionObject *)self);
+    Py_END_CRITICAL_SECTION();
+
+    return return_value;
+}
+
 PyDoc_STRVAR(BaseException___reduce____doc__,
 "__reduce__($self, /)\n"
 "--\n"
@@ -340,4 +370,4 @@ BaseExceptionGroup_subgroup(PyObject *self, PyObject 
*matcher_value)
 
 #define BASEEXCEPTION___CAUSE___GETSETDEF {"__cause__", 
(getter)BaseException___cause___get, (setter)BaseException___cause___set, NULL},
 
-/*[clinic end generated code: output=b14d57bb5ad3be0d input=a9049054013a1b77]*/
+/*[clinic end generated code: output=20829af0c376b1e9 input=a9049054013a1b77]*/
diff --git a/Objects/clinic/frameobject.c.h b/Objects/clinic/frameobject.c.h
index 5d4901833d1ba4..f7e764a0755ecf 100644
--- a/Objects/clinic/frameobject.c.h
+++ b/Objects/clinic/frameobject.c.h
@@ -287,6 +287,21 @@ frame___sizeof__(PyObject *self, PyObject 
*Py_UNUSED(ignored))
 
     return return_value;
 }
+
+static PyObject *
+frame_repr_impl(PyFrameObject *self);
+
+static PyObject *
+frame_repr(PyObject *self)
+{
+    PyObject *return_value = NULL;
+
+    Py_BEGIN_CRITICAL_SECTION(self);
+    return_value = frame_repr_impl((PyFrameObject *)self);
+    Py_END_CRITICAL_SECTION();
+
+    return return_value;
+}
 #define FRAME_F_LOCALS_GETSETDEF {"f_locals", (getter)frame_locals_get, 
(setter)NULL, frame_locals__doc__},
 
 #define FRAME_F_LINENO_GETSETDEF {"f_lineno", (getter)frame_lineno_get, 
(setter)frame_lineno_set, frame_lineno__doc__},
@@ -307,4 +322,4 @@ frame___sizeof__(PyObject *self, PyObject 
*Py_UNUSED(ignored))
 
 #define FRAME_F_GENERATOR_GETSETDEF {"f_generator", 
(getter)frame_generator_get, (setter)NULL, frame_generator__doc__},
 
-/*[clinic end generated code: output=dfa59114b6dbce08 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=e86fc251b0396a11 input=a9049054013a1b77]*/
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index 781b5142ec326e..b176a58aefdfe5 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -161,46 +161,40 @@ BaseException_traverse(PyObject *op, visitproc visit, 
void *arg)
     return 0;
 }
 
+/*[clinic input]
+@critical_section
+BaseException.__str__
+[clinic start generated code]*/
+
 static PyObject *
-BaseException_str(PyObject *op)
+BaseException_str_impl(PyBaseExceptionObject *self)
+/*[clinic end generated code: output=f9cf63c6d14a73fe input=cd946062d6786776]*/
 {
-    PyBaseExceptionObject *self = PyBaseExceptionObject_CAST(op);
-
-    PyObject *res;
-    Py_BEGIN_CRITICAL_SECTION(self);
     switch (PyTuple_GET_SIZE(self->args)) {
     case 0:
-        res = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
-        break;
+        return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
     case 1:
-        res = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
-        break;
+        return PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
     default:
-        res = PyObject_Str(self->args);
-        break;
+        return PyObject_Str(self->args);
     }
-    Py_END_CRITICAL_SECTION();
-    return res;
 }
 
+/*[clinic input]
+@critical_section
+BaseException.__repr__
+[clinic start generated code]*/
+
 static PyObject *
-BaseException_repr(PyObject *op)
+BaseException_repr_impl(PyBaseExceptionObject *self)
+/*[clinic end generated code: output=c885549a153a6007 input=20941168ad0ab577]*/
 {
-
-    PyBaseExceptionObject *self = PyBaseExceptionObject_CAST(op);
-
-    PyObject *res;
-    Py_BEGIN_CRITICAL_SECTION(self);
     const char *name = _PyType_Name(Py_TYPE(self));
     if (PyTuple_GET_SIZE(self->args) == 1) {
-        res = PyUnicode_FromFormat("%s(%R)", name,
+        return PyUnicode_FromFormat("%s(%R)", name,
                                     PyTuple_GET_ITEM(self->args, 0));
     }
-    else {
-        res = PyUnicode_FromFormat("%s%R", name, self->args);
-    }
-    Py_END_CRITICAL_SECTION();
-    return res;
+    return PyUnicode_FromFormat("%s%R", name, self->args);
 }
 
 /* Pickling support */
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
index a4cc14a6eaad45..61be2beca22d15 100644
--- a/Objects/frameobject.c
+++ b/Objects/frameobject.c
@@ -2121,19 +2121,20 @@ frame___sizeof___impl(PyFrameObject *self)
     return PyLong_FromSsize_t(res);
 }
 
+/*[clinic input]
+@critical_section
+frame.__repr__
+[clinic start generated code]*/
+
 static PyObject *
-frame_repr(PyObject *op)
+frame_repr_impl(PyFrameObject *self)
+/*[clinic end generated code: output=6dc361d24ba61076 input=cea0fd14e4749a55]*/
 {
-    PyFrameObject *f = PyFrameObject_CAST(op);
-    PyObject *result;
-    Py_BEGIN_CRITICAL_SECTION(f);
-    int lineno = PyFrame_GetLineNumber(f);
-    PyCodeObject *code = _PyFrame_GetCode(f->f_frame);
-    result = PyUnicode_FromFormat(
+    int lineno = PyFrame_GetLineNumber(self);
+    PyCodeObject *code = _PyFrame_GetCode(self->f_frame);
+    return PyUnicode_FromFormat(
         "<frame at %p, file %R, line %d, code %S>",
-        f, code->co_filename, lineno, code->co_name);
-    Py_END_CRITICAL_SECTION();
-    return result;
+        self, code->co_filename, lineno, code->co_name);
 }
 
 static PyMethodDef frame_methods[] = {
diff --git a/Tools/clinic/libclinic/dsl_parser.py 
b/Tools/clinic/libclinic/dsl_parser.py
index a55b0a6e427478..a7a78761a1aede 100644
--- a/Tools/clinic/libclinic/dsl_parser.py
+++ b/Tools/clinic/libclinic/dsl_parser.py
@@ -91,7 +91,6 @@
 __rtruediv__
 __rxor__
 __setattr__
-__str__
 __sub__
 __truediv__
 __xor__
diff --git a/Tools/clinic/libclinic/slots.py b/Tools/clinic/libclinic/slots.py
index 153272e7c24156..5b8c2ed9faf484 100644
--- a/Tools/clinic/libclinic/slots.py
+++ b/Tools/clinic/libclinic/slots.py
@@ -50,4 +50,5 @@ def make_return_converter(self) -> CReturnConverter:
     '__next__': SlotSpec(return_converter='object'),
     '__repr__': SlotSpec(return_converter='object'),
     '__setitem__': SlotSpec(return_converter='int'),
+    '__str__': SlotSpec(return_converter='object'),
 }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to