-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
GH-145247: Implement _PyTuple_FromPair #145325
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
Draft
sergey-miryanov
wants to merge
6
commits into
python:main
Choose a base branch
from
sergey-miryanov:145247-pytuple-from-pair
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−1
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f2d3ab8
Add initial version of _PyTuple_FromPair
sergey-miryanov 8459769
Add initial tests for _PyTuple_FromPair
sergey-miryanov b51ffc6
Fix warnings
sergey-miryanov b6c8727
Simplify tests
sergey-miryanov 9408400
Remove unnecessary assert
sergey-miryanov 5862613
Update Lib/test/test_capi/test_tuple.py
sergey-miryanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,9 +1,11 @@ | ||||||
| import unittest | ||||||
| import gc | ||||||
| from sys import getrefcount | ||||||
| from test.support import import_helper | ||||||
|
|
||||||
| _testcapi = import_helper.import_module('_testcapi') | ||||||
| _testlimitedcapi = import_helper.import_module('_testlimitedcapi') | ||||||
| _testinternalcapi = import_helper.import_module('_testinternalcapi') | ||||||
|
|
||||||
| NULL = None | ||||||
| PY_SSIZE_T_MIN = _testcapi.PY_SSIZE_T_MIN | ||||||
|
|
@@ -118,6 +120,37 @@ def test_tuple_pack(self): | |||||
| # CRASHES pack(1, NULL) | ||||||
| # CRASHES pack(2, [1]) | ||||||
|
|
||||||
| 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)) | ||||||
|
|
||||||
| for name, ctor in ctors: | ||||||
| with self.subTest(name): | ||||||
| self.assertEqual(type(ctor(1, 2)), tuple) | ||||||
| self.assertEqual(ctor(1, 2), (1, 2)) | ||||||
sergey-miryanov marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The series of asserts contained only immortal elements for the tuple. Changing |
||||||
| self.assertEqual(ctor(None, None), (None, None)) | ||||||
| self.assertEqual(ctor(True, False), (True, False)) | ||||||
|
|
||||||
| # user class supports gc | ||||||
| class Temp: | ||||||
| pass | ||||||
| temp = Temp() | ||||||
| temp_rc = getrefcount(temp) | ||||||
| self.assertEqual(ctor(temp, temp), (temp, temp)) | ||||||
| self.assertEqual(getrefcount(temp), temp_rc) | ||||||
|
|
||||||
| self.assertRaises(TypeError, ctor, 1, 2, 3) | ||||||
| self.assertRaises(TypeError, ctor, 1) | ||||||
| self.assertRaises(TypeError, ctor) | ||||||
|
|
||||||
| self.assertFalse(gc.is_tracked(ctor(1, 2))) | ||||||
| self.assertFalse(gc.is_tracked(ctor(None, None))) | ||||||
| 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([], {}))) | ||||||
|
|
||||||
| def test_tuple_size(self): | ||||||
| # Test PyTuple_Size() | ||||||
| size = _testlimitedcapi.tuple_size | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #include "parts.h" | ||
|
|
||
| #include "pycore_tuple.h" | ||
|
|
||
|
|
||
| static PyObject * | ||
| _tuple_from_pair(PyObject *Py_UNUSED(module), PyObject *args) | ||
| { | ||
| PyObject *one, *two; | ||
| if (!PyArg_ParseTuple(args, "OO", &one, &two)) { | ||
| return NULL; | ||
| } | ||
|
|
||
| return _PyTuple_FromPair(one, two); | ||
| } | ||
|
|
||
| static PyObject * | ||
| _tuple_from_pair_steal(PyObject *Py_UNUSED(module), PyObject *args) | ||
| { | ||
| PyObject *one, *two; | ||
| if (!PyArg_ParseTuple(args, "OO", &one, &two)) { | ||
| return NULL; | ||
| } | ||
|
|
||
| return _PyTuple_FromPairSteal(Py_NewRef(one), Py_NewRef(two)); | ||
| } | ||
|
|
||
|
|
||
| static PyMethodDef test_methods[] = { | ||
| {"_tuple_from_pair", _tuple_from_pair, METH_VARARGS}, | ||
| {"_tuple_from_pair_steal", _tuple_from_pair_steal, METH_VARARGS}, | ||
| {NULL}, | ||
| }; | ||
|
|
||
| int | ||
| _PyTestInternalCapi_Init_Tuple(PyObject *m) | ||
| { | ||
| if (PyModule_AddFunctions(m, test_methods) < 0) { | ||
| return -1; | ||
| } | ||
|
|
||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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_stealthat wraps_PyTuple_FromPairStealincrements references to the arguments. For that reason there is no way to make a distinction between the_testinternalcapi._tuple_from_pairand_testinternalcapi._tuple_from_pair._tuple_from_pair_stealin the tests)I would either change the name of
_tuple_from_pair_stealor add a comment to make this clear.There was a problem hiding this comment.
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
-Roption.There was a problem hiding this comment.
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_stealis 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.