Skip to content

Add support for Python tuples to the Python module #26946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Mar 24, 2025

Conversation

lydia-duncan
Copy link
Member

@lydia-duncan lydia-duncan commented Mar 19, 2025

[reviewed by @jabraham17]

Resolves Cray/chapel-private#7241

Add support for PyTuple. Covers:

  • writing the type
  • size
  • get
  • get with a range argument
    • including when the range uses a stride
  • contains
  • converting between it and Chapel tuples

Does not cover set as Python tuples are immutable, and the Python C interface only allows mutating a tuple if it knows it is the only one that owns the instance.

Adds a test of the new type and its methods, as well as extending the basicTypes correctness and memleaks tests to include it.

Passed a full paratest with futures

Does handle `toPython` and `fromPython` yet, but does add the PyTuple type, as
well as size and get methods for it (including a get method that takes a range)

----
Signed-off-by: Lydia Duncan <[email protected]>
Covers getting a tuple back from Python and calling get and size on it,
including some error cases.  More coverage is needed but I wanted a snapshot

----
Signed-off-by: Lydia Duncan <[email protected]>
Without adding the compilation error to this one, calls from it to the other
version were reporting it as the line number instead of the user's context

----
Signed-off-by: Lydia Duncan <[email protected]>
I think I can improve `toTuple` but I want to save what I have first

----
Signed-off-by: Lydia Duncan <[email protected]>
This explicitly exercises toTuple and fromTuple

----
Signed-off-by: Lydia Duncan <[email protected]>
…nt it out

Setter support for tuples is ... complicated.  So we're going to punt on it for
now.  TODO: OPEN AN ISSUE FOR IT LYDIA

----
Signed-off-by: Lydia Duncan <[email protected]>
…emented

Covers:
- error case when converting the slice to an int
- explicitly specifying PyTuple, homogeneous tuples, and PyList as the return
  type for the bounds case

----
Signed-off-by: Lydia Duncan <[email protected]>
… and get

This means toTuple no longer needs to take its argument by in intent.

----
Signed-off-by: Lydia Duncan <[email protected]>
Covers:
- homogeneous tuples
- heterogeneous tuples
- a tuple containing a list
- tuples containing strings
- a tuple containing bytes

The fix in the previous commit helped the strings and bytes cases (the others
were fine without it)

----
Signed-off-by: Lydia Duncan <[email protected]>
Covers:
- 1 tuples
- slices that should return 1 tuples
- slicing with an empty range

----
Signed-off-by: Lydia Duncan <[email protected]>
@lydia-duncan lydia-duncan changed the title Python tuples Add support for Python tuples to the Python module Mar 19, 2025
@lydia-duncan lydia-duncan marked this pull request as ready for review March 19, 2025 21:55
@jabraham17 jabraham17 self-requested a review March 19, 2025 23:00
Copy link
Member

@jabraham17 jabraham17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some comments about more potential edge cases, but this looks great!!

Covers:
- Specifying too large of a Chapel tuple
- Specifying too small of a Chapel tuple (where the latter is a future, see
  chapel-lang#26951)

----
Signed-off-by: Lydia Duncan <[email protected]>
… tests

This was done based on the dicussion in chapel-lang#26951

----
Signed-off-by: Lydia Duncan <[email protected]>
`Py_ssize_t` might be smaller than `int(64)` on 32 bit platforms, so use the
max/min of the type we expect to use to avoid potential issues.

Suggested by Jade, thanks!

----
Signed-off-by: Lydia Duncan <[email protected]>
----
Signed-off-by: Lydia Duncan <[email protected]>
Copy link
Member

@jabraham17 jabraham17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! thanks for making those changes

Thanks for noticing, Jade!

----
Signed-off-by: Lydia Duncan <[email protected]>
@lydia-duncan lydia-duncan merged commit d3cb57c into chapel-lang:main Mar 24, 2025
9 checks passed
@lydia-duncan lydia-duncan deleted the pythonTuples branch March 24, 2025 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants