Skip to content

Fix _tuplegetter() accepting missing and keyword arguments - #1261

Open
jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/collections-tuplegetter-arity
Open

jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/collections-tuplegetter-arity

Conversation

@jseop-lim

Copy link
Copy Markdown
Contributor

Description

Fixes #1249

The tp_new signature of _collections._tuplegetter declared index and doc with no minimum and no positional-only count, so a call with fewer than two arguments built a getter for index 0, and both arguments could be passed by keyword. CPython's tuplegetter_new rejects keywords and requires exactly two positional arguments (_collectionsmodule.c.h#L607-L613).

AS-IS

>>> from collections import _tuplegetter
>>> _tuplegetter(0)
<_collections._tuplegetter object at 0x...>
>>> _tuplegetter()
<_collections._tuplegetter object at 0x...>
>>> _tuplegetter(0, doc=None)
<_collections._tuplegetter object at 0x...>

TO-BE

>>> _tuplegetter(0)
TypeError: _tuplegetter() missing 1 required positional argument: 'doc'
>>> _tuplegetter()
TypeError: _tuplegetter() missing 2 required positional arguments: 'index' and 'doc'
>>> _tuplegetter(0, doc=None)
TypeError: _tuplegetter() got some positional-only arguments passed as keyword arguments: 'doc'

(matching CPython's exception type; the messages are GraalPy's usual argument errors)

Changes

  • Add minNumOfPositionalArgs = 3, numOfPositionalOnlyArgs = 3 to the @SlotSignature of TupleGetterBuiltins.TupleGetterNode.
  • Add test_tuplegetter_arguments to tests/test_collections.py.

Testing

mx python-jvm on darwin-aarch64, then tests/test_collections.py (2 passed). The new test fails on the unpatched build. _tuplegetter(0, None) still creates the getter, _tuplegetter(1, 'd').__get__((10, 20)) returns 20, and namedtuple field access and field docs work as before.

The tp_new signature declared no minimum and no positional-only
parameters, so _tuplegetter(0) and _tuplegetter() built a descriptor
from defaults, and index and doc were accepted as keywords. CPython
requires exactly two positional arguments and rejects keywords.

Signed-off-by: Jeongseop Lim <jeongseop_lim@korea.ac.kr>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Oct 31, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: collections._tuplegetter accepts fewer than two arguments and keyword arguments

1 participant