Skip to content

Commit 4553849

Browse files
authored
gh-114258: Update Argument Clinic for __str__. (gh-160402)
1 parent dfd2c7c commit 4553849

6 files changed

Lines changed: 78 additions & 38 deletions

File tree

‎Objects/clinic/exceptions.c.h‎

Lines changed: 31 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Objects/clinic/frameobject.c.h‎

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Objects/exceptions.c‎

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -161,46 +161,40 @@ BaseException_traverse(PyObject *op, visitproc visit, void *arg)
161161
return 0;
162162
}
163163

164+
/*[clinic input]
165+
@critical_section
166+
BaseException.__str__
167+
[clinic start generated code]*/
168+
164169
static PyObject *
165-
BaseException_str(PyObject *op)
170+
BaseException_str_impl(PyBaseExceptionObject *self)
171+
/*[clinic end generated code: output=f9cf63c6d14a73fe input=cd946062d6786776]*/
166172
{
167-
PyBaseExceptionObject *self = PyBaseExceptionObject_CAST(op);
168-
169-
PyObject *res;
170-
Py_BEGIN_CRITICAL_SECTION(self);
171173
switch (PyTuple_GET_SIZE(self->args)) {
172174
case 0:
173-
res = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
174-
break;
175+
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
175176
case 1:
176-
res = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
177-
break;
177+
return PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
178178
default:
179-
res = PyObject_Str(self->args);
180-
break;
179+
return PyObject_Str(self->args);
181180
}
182-
Py_END_CRITICAL_SECTION();
183-
return res;
184181
}
185182

183+
/*[clinic input]
184+
@critical_section
185+
BaseException.__repr__
186+
[clinic start generated code]*/
187+
186188
static PyObject *
187-
BaseException_repr(PyObject *op)
189+
BaseException_repr_impl(PyBaseExceptionObject *self)
190+
/*[clinic end generated code: output=c885549a153a6007 input=20941168ad0ab577]*/
188191
{
189-
190-
PyBaseExceptionObject *self = PyBaseExceptionObject_CAST(op);
191-
192-
PyObject *res;
193-
Py_BEGIN_CRITICAL_SECTION(self);
194192
const char *name = _PyType_Name(Py_TYPE(self));
195193
if (PyTuple_GET_SIZE(self->args) == 1) {
196-
res = PyUnicode_FromFormat("%s(%R)", name,
194+
return PyUnicode_FromFormat("%s(%R)", name,
197195
PyTuple_GET_ITEM(self->args, 0));
198196
}
199-
else {
200-
res = PyUnicode_FromFormat("%s%R", name, self->args);
201-
}
202-
Py_END_CRITICAL_SECTION();
203-
return res;
197+
return PyUnicode_FromFormat("%s%R", name, self->args);
204198
}
205199

206200
/* Pickling support */

‎Objects/frameobject.c‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,19 +2121,20 @@ frame___sizeof___impl(PyFrameObject *self)
21212121
return PyLong_FromSsize_t(res);
21222122
}
21232123

2124+
/*[clinic input]
2125+
@critical_section
2126+
frame.__repr__
2127+
[clinic start generated code]*/
2128+
21242129
static PyObject *
2125-
frame_repr(PyObject *op)
2130+
frame_repr_impl(PyFrameObject *self)
2131+
/*[clinic end generated code: output=6dc361d24ba61076 input=cea0fd14e4749a55]*/
21262132
{
2127-
PyFrameObject *f = PyFrameObject_CAST(op);
2128-
PyObject *result;
2129-
Py_BEGIN_CRITICAL_SECTION(f);
2130-
int lineno = PyFrame_GetLineNumber(f);
2131-
PyCodeObject *code = _PyFrame_GetCode(f->f_frame);
2132-
result = PyUnicode_FromFormat(
2133+
int lineno = PyFrame_GetLineNumber(self);
2134+
PyCodeObject *code = _PyFrame_GetCode(self->f_frame);
2135+
return PyUnicode_FromFormat(
21332136
"<frame at %p, file %R, line %d, code %S>",
2134-
f, code->co_filename, lineno, code->co_name);
2135-
Py_END_CRITICAL_SECTION();
2136-
return result;
2137+
self, code->co_filename, lineno, code->co_name);
21372138
}
21382139

21392140
static PyMethodDef frame_methods[] = {

‎Tools/clinic/libclinic/dsl_parser.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
__rtruediv__
9292
__rxor__
9393
__setattr__
94-
__str__
9594
__sub__
9695
__truediv__
9796
__xor__

‎Tools/clinic/libclinic/slots.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ def make_return_converter(self) -> CReturnConverter:
5050
'__next__': SlotSpec(return_converter='object'),
5151
'__repr__': SlotSpec(return_converter='object'),
5252
'__setitem__': SlotSpec(return_converter='int'),
53+
'__str__': SlotSpec(return_converter='object'),
5354
}

0 commit comments

Comments
 (0)