You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incomplete test coverage of PyNumber_ToBase #93884
I discovered that the tests for PyNumber_ToBase only ever check one small int value's conversions. The function description and implementation makes it quite clear that the intent is to support arbitrary ints, as would be expected for a function working with Python ints. Tests should be added for some large negative and positive values in different bases to ensure that that's indeed the case and also for the PyNumber_Index conversion part.
the tests for PyNumber_ToBase only ever check one small int value's conversions
Please could you clarify which tests you're looking at? I'm not seeing any direct C-level tests of PyNumber_ToBase (e.g., like those in test_capi), but there are indirect tests via tests for the bin, oct and hex built-in functions and for string formatting, including tests for numbers larger than 2**64.
I guess you are trying to add the following test cases. I have run those cases on Windows and they work well. :)
Do these test cases need to be supplemented?
@JustAnotherArchivist@CharlieZhao95 I think you're both referring to test cases that are in PyPy's test suite, not CPython's. In CPython the PyNumber_ToBase C-API function is well-tested through the various pieces of Python code that use it. In contrast, it looks as though in PyPy, PyNumber_ToBase would not be being tested through hex, oct, bin, string formatting and the like, so it makes sense that PyPy would want extra tests here.
In particular, I don't think there's a whole lot of value adding direct C-API tests for PyNumber_ToBase in CPython: those C-API tests would take the form of writing a Python wrapper for PyNumber_ToBase and testing that wrapper, but the hex, oct and bin functions are already direct wrappers for PyNumber_ToBase, and string formatting is a slightly less direct test for the decimal case.
That said, the tests for hex, bin and oct are pretty minimal; a few more testcases might not hurt. But IIUC extra testcases for those functions wouldn't have caught the PyPy issue.
@CharlieZhao95 That's pretty much what I had in mind, yes. Plus tests for the PyNumber_Index part (edited that into my message above after initial submission), which I guess requires defining a little class with an __index__ method like here. The index test should also be run for both small and large numbers. And an error test for __index__ not returning an int is probably a good idea as well.
(Side note: PyNumber_Index(o) in C is the equivalent to o.__index__() in Python, right? The docs currently don't mention this, unlike for PyNumber_Float etc.)
The index test should also be run for both small and large numbers. And an error test for __index__ not returning an int is probably a good idea as well.
@JustAnotherArchivist I find that those cases you're talking about are already mentioned in file Lib\test\test_index.py. Here are some code snippets.
@CharlieZhao95 Yeah, they're tested directly, but I meant testing that PyNumber_ToBase correctly retrieves and handles the index for objects that aren't int (or a subclass of int), including values beyond the usual C limits. Otherwise, an implementation that e.g. overflows when handling the index would not be caught by the tests.
As for the TypeError, I've been bitten by functions accidentally not handling errors occurring in C API functions they call, so I figured it might be good to test that the possible exception from the PyNumber_Index call correctly bubbles up through PyNumber_ToBase. In other words, test coverage for the if (!index) return NULL; branch.
Activity
mdickinson commentedon Jun 16, 2022
Please could you clarify which tests you're looking at? I'm not seeing any direct C-level tests of
PyNumber_ToBase(e.g., like those intest_capi), but there are indirect tests via tests for thebin,octandhexbuilt-in functions and for string formatting, including tests for numbers larger than2**64.CharlieZhao95 commentedon Jun 16, 2022
I guess you are trying to add the following test cases. I have run those cases on Windows and they work well. :)
Do these test cases need to be supplemented?
mdickinson commentedon Jun 16, 2022
@JustAnotherArchivist @CharlieZhao95 I think you're both referring to test cases that are in PyPy's test suite, not CPython's. In CPython the
PyNumber_ToBaseC-API function is well-tested through the various pieces of Python code that use it. In contrast, it looks as though in PyPy,PyNumber_ToBasewould not be being tested throughhex,oct,bin, string formatting and the like, so it makes sense that PyPy would want extra tests here.In particular, I don't think there's a whole lot of value adding direct C-API tests for
PyNumber_ToBasein CPython: those C-API tests would take the form of writing a Python wrapper forPyNumber_ToBaseand testing that wrapper, but thehex,octandbinfunctions are already direct wrappers forPyNumber_ToBase, and string formatting is a slightly less direct test for the decimal case.That said, the tests for
hex,binandoctare pretty minimal; a few more testcases might not hurt. But IIUC extra testcases for those functions wouldn't have caught the PyPy issue.JustAnotherArchivist commentedon Jun 16, 2022
@mdickinson The
Lib/test/test_capi.pytests (in CPython, not PyPy) are what I was referring to.@CharlieZhao95 That's pretty much what I had in mind, yes. Plus tests for the
PyNumber_Indexpart (edited that into my message above after initial submission), which I guess requires defining a little class with an__index__method like here. The index test should also be run for both small and large numbers. And an error test for__index__not returning anintis probably a good idea as well.JustAnotherArchivist commentedon Jun 16, 2022
(Side note:
PyNumber_Index(o)in C is the equivalent too.__index__()in Python, right? The docs currently don't mention this, unlike forPyNumber_Floatetc.)mdickinson commentedon Jun 16, 2022
Thanks! Sorry about that - I missed that I was looking at an old checkout locally.
mdickinson commentedon Jun 16, 2022
More or less, yes. There's a difference for subclasses of
int. See #61776 (comment) for a full description of the behaviour.CharlieZhao95 commentedon Jun 17, 2022
@JustAnotherArchivist I find that those cases you're talking about are already mentioned in file
Lib\test\test_index.py. Here are some code snippets.Maybe we could focus more on testing the
PyNumber_IndexC-API function than__index__, but I'm not sure it's beneficial.JustAnotherArchivist commentedon Jun 17, 2022
@CharlieZhao95 Yeah, they're tested directly, but I meant testing that
PyNumber_ToBasecorrectly retrieves and handles the index for objects that aren'tint(or a subclass ofint), including values beyond the usual C limits. Otherwise, an implementation that e.g. overflows when handling the index would not be caught by the tests.As for the
TypeError, I've been bitten by functions accidentally not handling errors occurring in C API functions they call, so I figured it might be good to test that the possible exception from thePyNumber_Indexcall correctly bubbles up throughPyNumber_ToBase. In other words, test coverage for theif (!index) return NULL;branch.Merge branch 'main' into pythongh-93884/improve_test_coverage
gh-93884: Improve test coverage of `PyNumber_ToBase` (GH-93932)