Repository navigation
gh-119714: Add PyLong_GetNumBits() function #119715
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 1 commit
0b46817
9481136
c1f6310
fd8a3f2
afc665e
b08d260
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -737,6 +737,21 @@ def test_long_getsign(self): | |
|
|
||
| # CRASHES getsign(NULL) | ||
|
|
||
| def test_long_getnumbits(self): | ||
| numbits = _testcapi.pylong_getnumbits | ||
| self.assertEqual(numbits(0), 0) | ||
| self.assertEqual(numbits(7), 3) | ||
| self.assertEqual(numbits(-37), 6) | ||
| self.assertEqual(numbits(684_643_661_981), 40) | ||
| self.assertEqual(numbits(2**123-1), 123) | ||
|
Comment on lines
+743
to
+747
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. There are no tests for the two overflow cases.
Member
Author
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. I added a test, but it requires 1145324612.3 GiB of memory to run :-) On 32-bit systems, the test pass and only requires 273 MiB. |
||
|
|
||
| for invalid_type in (1.0, 1j, "abc"): | ||
| with self.subTest(invalid_type=invalid_type): | ||
| with self.assertRaises(TypeError): | ||
| numbits(invalid_type) | ||
|
|
||
| # CRASHES numbits(NULL) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add :c:func:`PyLong_GetNumBits` function to get the number of bits of an | ||
| integer. Patch by Victor Stinner. |
Uh oh!
There was an error while loading. Please reload this page.