[Python-Dev] The curious case of 255 function arguments
Stephen McDowell
sjm324 at cornell.edu
Tue Aug 7 19:13:45 EDT 2018
Hi Andrea and Serhiy,
Thank you for your responses and clarifying that it is specifically the
CALL_FUNCTION. I tested this in my megascript and it will fail when trying
to call the functions directly and receive an error then (Py 2.x: fail at
call invocation, Py 3.y w/ y<7: fail at function definition).
@Serhiy I looked through the commits and had found
https://github.com/python/cpython/commit/5bb8b9134b0bb35a73c76657f41cafa3e4361fcd#diff-4d35cf8992b795c5e97e9c8b6167cb34
but the commit that removed the 255 checks also explains that this is
specifically about the call function (
https://github.com/python/cpython/commit/214678e44bf7773c0ed9c3684818354001d8f9ca#diff-4d35cf8992b795c5e97e9c8b6167cb34
), so indeed I should have been able to answer this myself.
The reason why I originally had encountered this was (as discussed in one
of the bug reports) from code that was generating a class hierarchy to
represent Doxygen's XML schema. The class constructors had >255 arguments,
but in executing the code it actually does still work in python 2.x. The
reason is because all of the arguments are defaulted to None, and during
execution of typical sample XML files, the explicit construction with all
>255 arguments virtually never happens.
f.write("def foo_2({0}):\n".format(",
".join(["a{0}=None".format(str(i)) for i in range(300)])))
f.write(" print('foo_2 executed')\n\n")
# ... in generated __main__ ...
f.write(" foo_2()\n\n")
foo_2() will succeed in python 2.x because the CALL_FUNCTION is not
explicitly getting more than 255 parameters. Very interesting!
Thank you both again for your responses, I am grateful to finally
understand the way in which success / failure works here :)
-Stephen
On Mon, Aug 6, 2018 at 2:17 AM, Serhiy Storchaka <storchaka at gmail.com>
wrote:
> 06.08.18 08:13, Stephen McDowell пише:
>
>> I've looked at the C code for a while and it is entirely non-obvious what
>> would lead to python *2* /allowing/ >255 arguments. Anybody happen to know
>> how / why the python *2* versions *succeed*?
>>
>
> The error message is misleading. It should be "more than 255 parameters".
> This limitation is due to the optimization used in Python 3 for call
> variables (see https://bugs.python.org/issue12399 for details).
>
> In all versions <3.7 there is a limitation on the number of explicit
> function arguments because of the limitation of the CALL_FUNCTION opcode.
>
> Thank you for reading, this is not a problem, just a burning desire for
>> closure (even if anecdotal) as to how this can be. I deeply love python,
>> and am not complaining! I stumbled across this and found it truly
>> confounding, and thought the gurus here may happen to recall what changed
>> in 3.x that lead the the error condition actually being asserted :)
>>
>
> Read the history of the code. Commit messages usually contain explanations
> or references to issues.
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/sjm324%
> 40cornell.edu
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180807/70b16c13/attachment.html>
More information about the Python-Dev
mailing list