Skip to content

Commit

Permalink
pythongh-104389: Argument Clinic now wraps unused args with Py_UNUSED
Browse files Browse the repository at this point in the history
Use the unused keyword param in the converter to explicitly
mark an argument as unused:

    /*[clinic input]
    BaseClass.interface
        flag: bool(unused=True)
  • Loading branch information
erlend-aasland committed May 11, 2023
1 parent e629ab6 commit 2def279
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Doc/howto/clinic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,9 @@ of these arguments, along with their meanings:

To accept ``None``, add ``NoneType`` to this set.

``unused``
Wrap the argument with :c:macro:`Py_UNUSED` in the impl function signature.

``bitwise``
Only supported for unsigned integers. The native integer value of this
Python argument will be written to the parameter without any range checking,
Expand Down
25 changes: 16 additions & 9 deletions Modules/_io/clinic/textio.c.h

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

7 changes: 4 additions & 3 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ _io__TextIOBase_detach_impl(PyObject *self, PyTypeObject *cls)
/*[clinic input]
_io._TextIOBase.read
cls: defining_class
size: int(unused=True) = -1
/
*args: object
Read at most size characters from stream.
Expand All @@ -79,8 +79,9 @@ If size is negative or omitted, read until EOF.
[clinic start generated code]*/

static PyObject *
_io__TextIOBase_read_impl(PyObject *self, PyTypeObject *cls, PyObject *args)
/*[clinic end generated code: output=3adf28998831f461 input=cee1e84664a20de0]*/
_io__TextIOBase_read_impl(PyObject *self, PyTypeObject *cls,
int Py_UNUSED(size))
/*[clinic end generated code: output=51a5178a309ce647 input=f5e37720f9fc563f]*/
{
_PyIO_State *state = IO_STATE();
return _unsupported(state, "read");
Expand Down
20 changes: 19 additions & 1 deletion Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2599,6 +2599,9 @@ class CConverter(metaclass=CConverterAutoRegister):
# Every non-abstract subclass should supply a valid value.
c_ignored_default = 'NULL'

# If true, wrap with Py_UNUSED.
unused = False

# The C converter *function* to be used, if any.
# (If this is not None, format_unit must be 'O&'.)
converter = None
Expand Down Expand Up @@ -2651,9 +2654,22 @@ class CConverter(metaclass=CConverterAutoRegister):
signature_name = None

# keep in sync with self_converter.__init__!
def __init__(self, name, py_name, function, default=unspecified, *, c_default=None, py_default=None, annotation=unspecified, **kwargs):
def __init__(self,
# Positional args:
name,
py_name,
function,
default=unspecified,
*, # Keyword only args:
c_default=None,
py_default=None,
annotation=unspecified,
unused=False,
**kwargs
):
self.name = ensure_legal_c_identifier(name)
self.py_name = py_name
self.unused = unused

if default is not unspecified:
if self.default_type and not isinstance(default, (self.default_type, Unknown)):
Expand Down Expand Up @@ -2800,6 +2816,8 @@ def simple_declaration(self, by_reference=False, *, in_parser=False):
name = self.parser_name
else:
name = self.name
if self.unused:
name = f"Py_UNUSED({name})"
prototype.append(name)
return "".join(prototype)

Expand Down

0 comments on commit 2def279

Please sign in to comment.