[issue34235] PyArg_ParseTupleAndKeywords: support required keyword arguments

Serhiy Storchaka report at bugs.python.org
Fri Feb 15 03:29:22 EST 2019


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

I thought about this variant. The problem is that you need to scan the format string to the end to determine whether arguments following $ are optional or not. This adds performance penalty for all existing uses of PyArg_ParseTupleAndKeywords() with $. Your PR changes the current behavior, and this is unacceptable too, because it can lead to crashes and other errors in existing code.

It is possible to add this feature without breakage and performance loss. Just allow $ to be used twice, for required and optional arguments. Then

    PyArg_ParseTupleAndKeywords(args, kwds, "O$O|O$O", kwlist, &a, &c, &b, &d)

will define four parameters in the following order: required positional-or-keyword, required keyword-only, optional positional-or-keyword, optional keyword-only.

My doubts are that this order is different from the order of parameters as defined in Python functions: required positional-or-keyword, optional positional-or-keyword, required keyword-only, optional keyword-only. I afraid this can cause confusions.

Although, the first order (all required parameters are first) is more efficient for processing. So perhaps for compatibility, performance, and the simplicity of the implementation we will accept it.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34235>
_______________________________________


More information about the Python-bugs-list mailing list