Skip to content

GH-145247: Implement _PyTuple_FromPair#145325

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

GH-145247: Implement _PyTuple_FromPair#145325
sergey-miryanov wants to merge 8 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>

static PyMethodDef test_methods[] = {
{"_tuple_from_pair", _tuple_from_pair, METH_VARARGS},
{"_tuple_from_pair_steal", _tuple_from_pair_steal, METH_VARARGS},
Copy link
Member

Choose a reason for hiding this comment

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

I don't think that the _ underscore prefix is useful here. Other _testinternalcapi functions don't use an _ underscore prefix.

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
Member

Choose a reason for hiding this comment

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

I would prefer a more flat structure:

    def check_tuple_from_pair(self, from_pair):
        ...

    def test_tuple_from_pair(self):
        # Test _PyTuple_FromPair()
        from_pair = _testinternalcapi._tuple_from_pair
        self.check_tuple_from_pair(from_pair)

    def test_tuple_from_pair_steal(self):
        # Test _PyTuple_FromPairSteal()
        from_pair = _testinternalcapi._tuple_from_pair_steal
        self.check_tuple_from_pair(from_pair)


self.assertRaises(TypeError, ctor, 1, 2, 3)
self.assertRaises(TypeError, ctor, 1)
self.assertRaises(TypeError, ctor)
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: i suggest moving these checks at the end.

self.assertFalse(gc.is_tracked(ctor(True, False)))
self.assertTrue(gc.is_tracked(ctor(temp, (1, 2))))
self.assertTrue(gc.is_tracked(ctor(temp, 1)))
self.assertTrue(gc.is_tracked(ctor([], {})))
Copy link
Member

Choose a reason for hiding this comment

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

You may reuse self._tracked() and self._not_tracked() methods for these checks.

sergey-miryanov and others added 2 commits March 5, 2026 10:32
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
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.

3 participants