Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 16 additions & 1 deletion Modules/clinic/mmapmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 9 additions & 11 deletions Modules/mmapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1256,8 +1256,16 @@ mmap_mmap___exit___impl(mmap_object *self, PyObject *exc_type,
return mmap_mmap_close_impl(self);
}

/*[clinic input]
@critical_section
mmap.mmap.__repr__ as mmap__repr__method

op: self(type="PyObject *")
[clinic start generated code]*/

static PyObject *
mmap__repr__method_lock_held(PyObject *op)
mmap__repr__method_impl(PyObject *op)
/*[clinic end generated code: output=c896c8c4cd07228c input=a030361fbcfe851a]*/
{
mmap_object *mobj = mmap_object_CAST(op);

Expand Down Expand Up @@ -1301,16 +1309,6 @@ mmap__repr__method_lock_held(PyObject *op)
}
}

static PyObject *
mmap__repr__method(PyObject *op)
{
PyObject *result;
Py_BEGIN_CRITICAL_SECTION(op);
result = mmap__repr__method_lock_held(op);
Py_END_CRITICAL_SECTION();
return result;
}

#ifdef MS_WINDOWS
/*[clinic input]
@critical_section
Expand Down
20 changes: 9 additions & 11 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1182,8 +1182,16 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
return -1;
}

/*[clinic input]
@critical_section
bytearray.__repr__

op: self(type="PyObject *")
[clinic start generated code]*/

static PyObject *
bytearray_repr_lock_held(PyObject *op)
bytearray_repr_impl(PyObject *op)
/*[clinic end generated code: output=4086ab6928ebab1d input=d46ad3c1f3071b27]*/
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op);
const char *className = _PyType_Name(Py_TYPE(op));
Expand All @@ -1198,16 +1206,6 @@ bytearray_repr_lock_held(PyObject *op)
return res;
}

static PyObject *
bytearray_repr(PyObject *op)
{
PyObject *ret;
Py_BEGIN_CRITICAL_SECTION(op);
ret = bytearray_repr_lock_held(op);
Py_END_CRITICAL_SECTION();
return ret;
}

static PyObject *
bytearray_str(PyObject *op)
{
Expand Down
17 changes: 16 additions & 1 deletion Objects/clinic/bytearrayobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion Objects/clinic/dictobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion Objects/clinic/listobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion Objects/clinic/setobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 9 additions & 11 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3772,23 +3772,21 @@ anydict_repr_impl(PyObject *self)
return NULL;
}

/*[clinic input]
@critical_section
dict.__repr__

self: self(type="PyObject *")
[clinic start generated code]*/

static PyObject *
dict_repr_lock_held(PyObject *self)
dict_repr_impl(PyObject *self)
/*[clinic end generated code: output=059263af818a321d input=f9c423cbdf534674]*/
{
ASSERT_DICT_LOCKED((PyDictObject *)self);
return anydict_repr_impl(self);
}

static PyObject *
dict_repr(PyObject *self)
{
PyObject *res;
Py_BEGIN_CRITICAL_SECTION(self);
res = dict_repr_lock_held(self);
Py_END_CRITICAL_SECTION();
return res;
}

static Py_ssize_t
dict_length(PyObject *self)
{
Expand Down
26 changes: 12 additions & 14 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,21 @@ list_dealloc(PyObject *self)
}
}

/*[clinic input]
@critical_section
list.__repr__

v: self(type="PyListObject *")
[clinic start generated code]*/

static PyObject *
list_repr_impl(PyListObject *v)
/*[clinic end generated code: output=c5ffe3b9cfee36ce input=f4b85c550d11de32]*/
{
if (PyList_GET_SIZE(v) == 0) {
return PyUnicode_FromString("[]");
}

int res = Py_ReprEnter((PyObject*)v);
if (res != 0) {
return (res > 0 ? PyUnicode_FromString("[...]") : NULL);
Expand Down Expand Up @@ -687,20 +699,6 @@ list_repr_impl(PyListObject *v)
return NULL;
}

static PyObject *
list_repr(PyObject *self)
{
if (PyList_GET_SIZE(self) == 0) {
return PyUnicode_FromString("[]");
}
PyListObject *v = (PyListObject *)self;
PyObject *ret = NULL;
Py_BEGIN_CRITICAL_SECTION(v);
ret = list_repr_impl(v);
Py_END_CRITICAL_SECTION();
return ret;
}

static Py_ssize_t
list_length(PyObject *a)
{
Expand Down
21 changes: 9 additions & 12 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,16 @@ set_dealloc(PyObject *self)
Py_TYPE(so)->tp_free(so);
}

/*[clinic input]
@critical_section
set.__repr__

so: setobject
[clinic start generated code]*/

static PyObject *
set_repr_lock_held(PySetObject *so)
set_repr_impl(PySetObject *so)
/*[clinic end generated code: output=5dfbb5e7f7c947ad input=f6743ec76163d7b3]*/
{
PyObject *result=NULL, *keys, *listrepr, *tmp;
int status = Py_ReprEnter((PyObject*)so);
Expand Down Expand Up @@ -807,17 +815,6 @@ set_repr_lock_held(PySetObject *so)
return result;
}

static PyObject *
set_repr(PyObject *self)
{
PySetObject *so = _PySet_CAST(self);
PyObject *result;
Py_BEGIN_CRITICAL_SECTION(so);
result = set_repr_lock_held(so);
Py_END_CRITICAL_SECTION();
return result;
}

static Py_ssize_t
set_len(PyObject *self)
{
Expand Down
7 changes: 2 additions & 5 deletions Tools/clinic/libclinic/dsl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
__radd__
__rand__
__rdivmod__
__repr__
__rfloordiv__
__rlshift__
__rmatmul__
Expand Down Expand Up @@ -920,10 +919,6 @@ def state_parameters_start(self, line: str) -> None:
assert self.function is not None
if self.function.kind is GETTER:
fail("@getter methods cannot define parameters")
if self.function.kind is SLOT:
assert self.function.slot is not None
if not self.function.slot.takes_parameters:
fail(f"{self.function.name!r} methods cannot define parameters")

self.parameter_continuation = ''
return self.next(self.state_parameter, line)
Expand Down Expand Up @@ -1680,6 +1675,8 @@ def check_slot_parameters(self, func: Function) -> None:
"""Check that the parameters of a slot method select a C slot."""
assert func.slot is not None
params = list(func.parameters.values())[1:]
if params and not func.slot.takes_parameters:
fail(f"{func.name!r} methods cannot define parameters")
for p in params:
if not p.is_positional_only():
fail(f"The parameters of {func.name!r} must be positional-only")
Expand Down
3 changes: 3 additions & 0 deletions Tools/clinic/libclinic/slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ def describe_signatures(self) -> str:
'__next__': SlotSpec('__next__', 'PyObject *', 'object', {
(): 'tp_iternext',
}),
'__repr__': SlotSpec('__repr__', 'PyObject *', 'object', {
(): 'tp_repr',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I found this definition design is weird. I will submit the new PR for this. Can you rebase it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, tag me for a review.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After #160012 is merged,

you can simply declare with

    '__repr__': (
        SlotSpec(return_converter='object'),
    ),

}),
}
Loading