Skip to content
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

gh-126061: Add PyLong_IsPositive/Zero/Negative() functions #126065

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

rruuaanng
Copy link
Contributor

@rruuaanng rruuaanng commented Oct 28, 2024

Copy link
Member

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

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

This should have tests and an entry in Doc/whatsnew/3.14.rst

Doc/c-api/long.rst Outdated Show resolved Hide resolved
Doc/c-api/long.rst Outdated Show resolved Hide resolved
Include/longobject.h Outdated Show resolved Hide resolved
@ZeroIntensity
Copy link
Member

I'll get to reviewing this sometime today.

@skirpichev skirpichev self-requested a review October 28, 2024 09:51
@skirpichev
Copy link
Member

@rruuaanng, please avoid force-pushes.

@rruuaanng
Copy link
Contributor Author

rruuaanng commented Oct 28, 2024

Sorry, there are a lot of changes that need to be rolled back. I will pay attention. :(

@skirpichev
Copy link
Member

skirpichev commented Oct 28, 2024 via email

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Nitpick: Use op for names, not obj

Objects/longobject.c Outdated Show resolved Hide resolved
Objects/longobject.c Outdated Show resolved Hide resolved
Objects/longobject.c Outdated Show resolved Hide resolved
Include/cpython/longobject.h Show resolved Hide resolved
Doc/whatsnew/3.14.rst Outdated Show resolved Hide resolved
Doc/c-api/long.rst Outdated Show resolved Hide resolved
Doc/c-api/long.rst Outdated Show resolved Hide resolved
Doc/c-api/long.rst Outdated Show resolved Hide resolved
@rruuaanng
Copy link
Contributor Author

rruuaanng commented Oct 29, 2024

Given the number of comments,I'll reply here. I apologize for any inconvenience. The error handling implementation mentioned above was based on discussions in Capigroup, as victor expected. It'sItsavior should be consistent with GetSign. Regarding the comments, their style follows the format used in the same file. So, I apologize, but I may not make changes to them.

Edit
about the document, I'll change it.

@ZeroIntensity
Copy link
Member

The error handling implementation mentioned above was based on discussions in Capigroup

Yes, I'm not asking to change how errors are handled. I'm worried about the error message.

@skirpichev
Copy link
Member

Nitpick: Use op for names, not obj

Looking on other functions, rather v. But I don't think it's a big deal.

skirpichev and others added 2 commits October 29, 2024 05:28
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM in general. Please add tests. Look at Modules/_testcapi/long.c and Lib/test/test_capi/test_long.py.

Objects/longobject.c Outdated Show resolved Hide resolved
Doc/c-api/long.rst Outdated Show resolved Hide resolved
Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

I do see Serhiy's point of using the type name rather than the repr, but it's nice when "invalid type" errors are more unified, so I would prefer if it matched what was raised when using PyArg*. AFAICS, the only other function using the current message is PyLong_GetSign.

@skirpichev
Copy link
Member

AFAICS, the only other function using the current message is PyLong_GetSign.

No. PyLong_AsNativeBytes, PyLong_AsUnsignedLongLong, PyLong_AsDouble, PyLong_AsSsize_t, etc.

@ZeroIntensity
Copy link
Member

Ah, I see it in PyLong_AsNativeBytes, but that looks like the only other location. Could you point me to where it's thrown in the other functions? (GitHub search might be acting up.)

Modules/_testcapi/long.c Outdated Show resolved Hide resolved
Modules/_testcapi/long.c Outdated Show resolved Hide resolved
Modules/_testcapi/long.c Outdated Show resolved Hide resolved
Lib/test/test_capi/test_long.py Show resolved Hide resolved
Lib/test/test_capi/test_long.py Show resolved Hide resolved
Lib/test/test_capi/test_long.py Show resolved Hide resolved
Lib/test/test_capi/test_long.py Show resolved Hide resolved
Lib/test/test_capi/test_long.py Show resolved Hide resolved
Lib/test/test_capi/test_long.py Show resolved Hide resolved
Lib/test/test_capi/test_long.py Show resolved Hide resolved
@skirpichev
Copy link
Member

skirpichev commented Oct 29, 2024 via email

@ZeroIntensity
Copy link
Member

Ok, so that's not expect int, got type. Let's change the error to an integer is required (or better yet an integer is required, but got %T)

@rruuaanng
Copy link
Contributor Author

I've made changes :)

@erlend-aasland
Copy link
Contributor

Ok, so that's not expect int, got type. Let's change the error to an integer is required (or better yet an integer is required, but got %T)

See git grep "expected.*got". I think it would be fine to just change the word "expect" to "expected".

@rruuaanng
Copy link
Contributor Author

The # CRACHES is...(NULL) comments seem to be wrong, as it does not appear such calls cause a crash. Can you confirm?

It exists only to keep in sync with test_long_getsign :)

@devdanzin
Copy link
Contributor

It exists only to keep in sync with test_long_getsign :)

Ah, it's probably a way to signal that PyLong_IsPositive(NULL); crashes then? Anyway, if the comment should stay, I suggest correcting the typo "CRACHES" -> "CRASHES".

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Per the comments from Sergey and Erlend, it should be expected, not expect.

Objects/longobject.c Outdated Show resolved Hide resolved
Objects/longobject.c Outdated Show resolved Hide resolved
Objects/longobject.c Outdated Show resolved Hide resolved
@rruuaanng
Copy link
Contributor Author

Per the comments from Sergey and Erlend, it should be expected, not expect.

I see, but I wonder if there is any reason to change GetSign as well.

@ZeroIntensity
Copy link
Member

There could be, but that's for another PR. I'm also happy with the phrase an integer is required here for consistency, but I like displaying some information about the object because it's useful for debugging.

@rruuaanng
Copy link
Contributor Author

an integer is required

Maybe it's too long? Maybe keeping expected int is enough.
(I think)

@rruuaanng
Copy link
Contributor Author

rruuaanng commented Nov 1, 2024

There could be, but that's for another PR. I'm also happy with the phrase here for consistency, but I like displaying some information about the object because it's useful for debugging.an integer is required

That's say, we should change it to an integer is required? What should I do, can you give a diff.

Edit
It seems that the current one isn't bad.

expected int, got %T

@ZeroIntensity
Copy link
Member

I'm happy to approve this if you change it to an assertRaisesRegex like I asked.

Doc/c-api/long.rst Outdated Show resolved Hide resolved
Doc/c-api/long.rst Outdated Show resolved Hide resolved
@@ -571,6 +571,42 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
.. versionadded:: 3.14
.. c:function:: int PyLong_IsPositive(PyObject *obj)
Check if the integer object *obj* is positive.
Copy link
Member

Choose a reason for hiding this comment

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

it's very positive

Also it's very negative:D

Lets just stick with math terminolgy.

@rruuaanng
Copy link
Contributor Author

rruuaanng commented Nov 1, 2024

I'm happy to approve this if you change it to an assertRaisesRegex like I asked.

Sorry peter. I'm not sure we need to be consistent with the test for GetSign, changing the current test to assertRaisesRegex format seems inconsistent with the style. Maybe ask Sergey for his opinion. I'm happy to change the code, but I'm not sure if others would object to it.

@ZeroIntensity
Copy link
Member

I'm not sure we need to be consistent with the test for GetSign, changing the current test to assertRaisesRegex format seems inconsistent with the style.

No, assertRaisesRegex has nothing to do with the style of GetSign. It's to make sure that the TypeError stays correct and displays the proper message (because in theory, something else could raise an exception, but since we've documented that as the only way it can fail, that would be wrong).

@rruuaanng
Copy link
Contributor Author

Emmm..., Look at this, actually my point is the same. I think we just need to identify a problem, and it will trigger this when the type is wrong.
https://github.com/python/cpython/pull/126065/files#r1825465408

@@ -571,6 +571,39 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
.. versionadded:: 3.14
.. c:function:: int PyLong_IsPositive(PyObject *obj)
Check if the integer object *obj* is positive.
Copy link
Member

Choose a reason for hiding this comment

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

If you dislike strictly, I suggest:

Suggested change
Check if the integer object *obj* is positive.
Check if the integer object *obj* is positive: greater than zero.

Copy link
Member

Choose a reason for hiding this comment

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

I don't feel this addition is needed.

But at least this form is less confusing than "strictly positive". Maybe: "Check if the integer object obj is positive, i.e. greater than zero."

@skirpichev
Copy link
Member

@serhiy-storchaka, I think this is ready for final review.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

OK, I've ran out of things to nitpick 😄

I'm not particularly happy about the tests, but in hindsight it's probably fine. Though, if we break it in the future don't say I didn't warn you!

Thank you @rruuaanng for the responsiveness, and thank you @skirpichev for all the technical insight (and for dealing with my pickiness)!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants