Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-110628: Add tests for PyLong C API #110629

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

Merged
merged 5 commits into from
Oct 14, 2023

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Oct 10, 2023


PyObject *result = PyLong_FromString(str, &end, base);
if (result == NULL) {
// XXX 'end' is not always set.
Copy link
Contributor

Choose a reason for hiding this comment

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

What does XXX imply? Is it a FIXME or TODO comment? If it has no special meaning, I think it should be removed. If it has a special meaning, it should be expressed clearly.

Copy link
Member Author

Choose a reason for hiding this comment

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

It means that that we cannot add assert(end != UNINITIALIZED_PTR) here. end is not set if base is incorrect, and maybe in case of memory allocation error. I am not sure what to do with this. It is not correct to set end = str, because the first character can be valid. Maybe set it to NULL? This is a different issue, and I am going to think about this later.

XXX means "Look! Something not good here."


PyObject *result = PyLong_FromString(str, &end, base);
if (result == NULL) {
// XXX 'end' is not always set.
Copy link
Member

Choose a reason for hiding this comment

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

not good :-( can it be fixed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not in this issue. And it is not clear what fix may look like.

}
Py_RETURN_NONE;
}
return Py_NewRef((PyObject *)value);
Copy link
Member

Choose a reason for hiding this comment

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

Wait, is it an object? It's just a raw pointer, no?

Maybe return PyLong_FromVoidPtr(value)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it is a raw pointer. But Python does not have pointers. So these tests only test pointers to Python objects (and NULL). With PyLong_FromVoidPtr() and PyLong_AsVoidPtr()` we can round trip object -> int -> object.

float_max = sys.float_info.max
for value in (5.0, 5.1, 5.9, -5.1, -5.9, 0.0, -0.0, float_max, -float_max):
with self.subTest(value=value):
self.assertEqual(fromdouble(value), int(value))
Copy link
Member

Choose a reason for hiding this comment

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

This test is now suspicious. What if int() executes exactly the same code path, PyLong_FromDouble()?

I would prefer to hardcode the expected result:

for value, expected in (
    (5.0, 5),
    ...
    (float_max, int(float_max)),
):
    ...

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, it is the purpose. PyLong_FromDouble() works exactly as int() with float argument.

How does int() with float argument work is tested in other tests.

@serhiy-storchaka serhiy-storchaka merged commit 9d40ebf into python:main Oct 14, 2023
@miss-islington
Copy link
Contributor

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

@serhiy-storchaka serhiy-storchaka deleted the test_capi-test_long branch October 14, 2023 05:54
@miss-islington
Copy link
Contributor

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 9d40ebf1902812fad6aa85ede7b6f1fdff3c1291 3.12

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Oct 14, 2023
(cherry picked from commit 9d40ebf)

Co-authored-by: Serhiy Storchaka <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Oct 14, 2023

GH-110854 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Oct 14, 2023
@serhiy-storchaka serhiy-storchaka removed their assignment Oct 14, 2023
serhiy-storchaka added a commit that referenced this pull request Oct 14, 2023
@skirpichev skirpichev mentioned this pull request Nov 7, 2023
10 tasks
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants