-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-108444: Add PyLong_AsInt() public function #108445
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,36 @@ def test_compact_known(self): | |
self.assertEqual(_testcapi.call_long_compact_api(sys.maxsize), | ||
(False, -1)) | ||
|
||
def test_long_asint(self): | ||
PyLong_AsInt = _testcapi.PyLong_AsInt | ||
INT_MIN = _testcapi.INT_MIN | ||
INT_MAX = _testcapi.INT_MAX | ||
|
||
# round trip (object -> int -> object) | ||
for value in (INT_MIN, INT_MAX, -1, 0, 1, 123): | ||
with self.subTest(value=value): | ||
self.assertEqual(PyLong_AsInt(value), value) | ||
|
||
# use __index__(), not __int__() | ||
class MyIndex: | ||
def __index__(self): | ||
return 10 | ||
def __int__(self): | ||
return 22 | ||
self.assertEqual(PyLong_AsInt(MyIndex()), 10) | ||
|
||
# bound checking | ||
with self.assertRaises(OverflowError): | ||
PyLong_AsInt(INT_MIN - 1) | ||
with self.assertRaises(OverflowError): | ||
PyLong_AsInt(INT_MAX + 1) | ||
|
||
# invalid type | ||
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. Please add also tests for objects with 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. Ok, I added a test |
||
for value in (1.0, b'2', '3'): | ||
with self.subTest(value=value): | ||
with self.assertRaises(TypeError): | ||
PyLong_AsInt(value) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,4 @@ | ||||||
Add :c:func:`PyLong_AsInt` function: similar to :c:func:`PyLong_AsLong`, but | ||||||
store the result in a C :c:expr:`int` instead of a C :c:expr:`long`. | ||||||
Previously, it was known as the the private function :c:func:`!_PyLong_AsInt` | ||||||
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
|
||||||
(with an underscore prefix). Patch by by Victor Stinner. | ||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2450,3 +2450,5 @@ | |
added = '3.13' | ||
[function.PyDict_GetItemStringRef] | ||
added = '3.13' | ||
[function.PyLong_AsInt] | ||
added = '3.13' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.