[Python-ideas] New 3.x restriction on number of keyword arguments

M.-A. Lemburg mal at egenix.com
Thu Oct 21 19:31:48 CEST 2010

Alexander Belopolsky wrote:
> On Thu, Oct 21, 2010 at 11:41 AM, M.-A. Lemburg <mal at egenix.com> wrote:
> ..
>> Looking at the AST code, the limitation appears somewhat arbitrary.
>> There's no comment in the code suggesting a reason for the limit and
>> it's still possible to pass in more arguments via *args and **kws -
>> but without the built-in argument checking.
>> Could someone provide some insight ?
> My understanding is that the limitation comes from bytecode generation
> phase, not AST.
> See also Guido's http://bugs.python.org/issue1636#msg58760.
> According to Python manual section for opcodes,
> Calls a function. The low byte of argc indicates the number of
> positional parameters, the high byte the number of keyword parameters.
> On the stack, the opcode finds the keyword parameters first. For each
> keyword argument, the value is on top of the key. Below the keyword
> parameters, the positional parameters are on the stack, with the
> right-most parameter on top. Below the parameters, the function object
> to call is on the stack. Pops all function arguments, and the function
> itself off the stack, and pushes the return value.
> http://docs.python.org/dev/py3k/library/dis.html?highlight=opcode#opcode-CALL_FUNCTION

Thanks for the insight.

Even with the one byte per position and keywords arguments
limitation imposed by the byte code, the checks in ast.c are
a bit too simple, since they apply a limit on the sum of positional
and keyword args, whereas the byte code and VM can deal with up
to 255 positional and 255 keyword arguments.

    if (nposargs + nkwonlyargs > 255) {
        ast_error(n, "more than 255 arguments");
        return NULL;

I think this should be:

    if (nposargs > 255)
        ast_error(n, "more than 255 positional arguments");
        return NULL;
    if (nkwonlyargs > 255)
        ast_error(n, "more than 255 keyword arguments");
        return NULL;

There's a patch somewhere that turns Python's VM into a 16 or
32-bit byte code machine. Perhaps it's time to have a look at that

Do other Python implementations have such limitations ?

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Oct 21 2010)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611

More information about the Python-ideas mailing list