Skip to content

GH-145247: Implement _PyTuple_FromPair#145325

Draft
sergey-miryanov wants to merge 6 commits intopython:mainfrom
sergey-miryanov:145247-pytuple-from-pair
Draft

GH-145247: Implement _PyTuple_FromPair#145325
sergey-miryanov wants to merge 6 commits intopython:mainfrom
sergey-miryanov:145247-pytuple-from-pair

Conversation

@sergey-miryanov
Copy link
Contributor

@sergey-miryanov sergey-miryanov commented Feb 27, 2026

Copy link
Contributor

@eendebakpt eendebakpt left a comment

Choose a reason for hiding this comment

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

To minor comments on the test, but overall this looks good!

def test_tuple_from_pair(self):
# Test _PyTuple_FromPair, _PyTuple_FromPairSteal
ctors = (("_PyTuple_FromPair", _testinternalcapi._tuple_from_pair),
("_PyTuple_FromPairSteal", _testinternalcapi._tuple_from_pair_steal))
Copy link
Contributor

Choose a reason for hiding this comment

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

The _testinternalcapi._tuple_from_pair_steal that wraps _PyTuple_FromPairSteal increments references to the arguments. For that reason there is no way to make a distinction between the _testinternalcapi._tuple_from_pair and _testinternalcapi._tuple_from_pair._tuple_from_pair_steal in the tests)

I would either change the name of _tuple_from_pair_steal or add a comment to make this clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I understand what you mean. Could you elaborate please?
You are right that tests seem equals from point of view, but we need to test both functions to check a) their behavior and b) absence of the leak if we run it with -R option.

Copy link
Contributor

Choose a reason for hiding this comment

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

The test is good indeed.

What I meant is that _testinternalcapi._tuple_from_pair_steal is not stealing references (since the arguments are incremented before passing to _PyTuple_FromPairSteal. So a (way too long) descriptive name would be _testinternalcapi._wraps_tuple_from_pair_steal_but_does_not_actually_steal.

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
for name, ctor in ctors:
with self.subTest(name):
self.assertEqual(type(ctor(1, 2)), tuple)
self.assertEqual(ctor(1, 2), (1, 2))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.assertEqual(ctor(1, 2), (1, 2))
self.assertEqual(ctor(1, 145325), (1, 145325))

The series of asserts contained only immortal elements for the tuple. Changing 2 to a semi-random higher number that is not part of the cpython small ints.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants