Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-129149: Create a macro PYLONG_FROM_SIGNED, and leverage it in functions#129215

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
srinivasreddy wants to merge3 commits intopython:mainfromsrinivasreddy:gh-129149-1

Conversation

@srinivasreddy
Copy link
Contributor

@srinivasreddysrinivasreddy commentedJan 23, 2025
edited
Loading

gh-129149: Create a macro PYLONG_FROM_SIGNED, and leverage it in functions PyLong_FromLong, PyLong_FromLongLong and PyLong_FromSsize_t so that repetitive code is removed

…g_FromLong, PyLong_FromLongLong and PyLong_FromSsize_t so the repetitive code is removed.
@srinivasreddysrinivasreddy changed the titleCreate a macro PYLONG_FROM_SIGNED, and leverage it in functions PyLong_FromLong, PyLong_FromLongLong and PyLong_FromSsize_t so that repetitive code is removed.gh-129149: Create a macro PYLONG_FROM_SIGNED, and leverage it in functions PyLong_FromLong, PyLong_FromLongLong and PyLong_FromSsize_t so that repetitive code is removed.Jan 23, 2025
@srinivasreddysrinivasreddy changed the titlegh-129149: Create a macro PYLONG_FROM_SIGNED, and leverage it in functions PyLong_FromLong, PyLong_FromLongLong and PyLong_FromSsize_t so that repetitive code is removed.gh-129149: Create a macro PYLONG_FROM_SIGNED, and leverage it in functionsJan 23, 2025
@srinivasreddysrinivasreddy marked this pull request as ready for reviewJanuary 23, 2025 08:45
@skirpichev
Copy link
Contributor

Test failures seems relevant. BTW, if you include PyLong_FromSsize_t change - please add news entry.

srinivasreddy reacted with thumbs up emoji

@srinivasreddysrinivasreddy marked this pull request as draftJanuary 23, 2025 09:48
@picnixz
Copy link
Member

  • I don't think this should be under the same issue as it's a refactoring issue and not a performance issue.
  • I don't think this adds value and there seems to be undefined behaviours here and there caught by GHA.

I would prefer not changing the current code. It works and there is no need to have a macro which would add useless tests/casts for some functions.

@skirpichev
Copy link
Contributor

it's a refactoring issue and not a performance issue

Py_ssize_t case might be.

@picnixz
Copy link
Member

Py_ssize_t case might be.

In this case, I think we should only focus on thePy_ssize_t case and don't need to convert it into a macro.

@chris-eibl
Copy link
Member

chris-eibl commentedJan 23, 2025
edited
Loading

Py_ssize_t case might be.

In this case, I think we should only focus on thePy_ssize_t case and don't need to convert it into a macro.

Then, we would just introduce roughly the same code block forPyLong_FromSsize_t() a third time.

Sure, that will do it's job to get us the fast path for medium-size integers, but the macro would be nice to remove the repetitive code and get symmetry with the already existingPYLONG_FROM_UINT?

Those two macros would also be a pretty good fit forPyLong_FromInt32(),PyLong_FromUInt32() andPyLong_FromUInt64().
They do the conversion totally different and have no fast path for medium-sized integers, too.

IMHO, all of thosePyLong_From* functions are easier to reason about and maintain if they share the same code base.

Then, there are less spots to consider when altering them.

After all, I think this might be the reason why the fast path for medium-size integers was not done for all of them?

@picnixz
Copy link
Member

Well.., I wouldn't mind if the 3 functions shared the exact same implementation, but it appears that there is an issue on Windows platforms. I actually don't mind the macro form but I'm -0 (I'm enclined to say yes because it would add symmetry but I'm also enclined to say no because overflow checks are apparently a bit different forssize_t).

@chris-eibl
Copy link
Member

Test failures seems relevant. BTW, if you include PyLong_FromSsize_t change - please add news entry.

issue on Windows

They most probably relate to

size_tabs_ival;/* Use size_t for unsigned operations */ \size_tt; \

which IMHO should read

unsignedINT_TYPEabs_ival,t;/* Use size_t for unsigned operations */ \

Which will make the macro harder to use forPyLong_FromSsize_t(), but this could be solved by using two parameters likeUNSIGNED_INT_TYPE andINT_TYPE.

Another option might be, to implementPyLong_FromSsize_t() using eitherPyLong_FromLong() orPyLong_FromLongLong() depending on the size ofPy_ssize_t.

@picnixz
Copy link
Member

but this could be solved by using two parameters like UNSIGNED_INT_TYPE and INT_TYPE.

Which in this case won't be symmetric anymore with the other macro. It would also affect readability. Currently, the code is fine as it is, albeit we're missing a fast path. Wecan add that fast path but we don't need to make it everything as macros. I guess we can use a macro for the long values but forssize_t, I would prefer keeping it as is.

@chris-eibl
Copy link
Member

They are not totally symmetric, anyway, because e.g. the signed and unsigned path are different in the fast path check:

if (-(INT_TYPE)PyLong_MASK <= (ival)&& (ival) <= (INT_TYPE)PyLong_MASK) { \return_PyLong_FromMedium((sdigit)(ival)); \

versus

if ((ival) <=PyLong_MASK) { \return_PyLong_FromMedium((sdigit)(ival)); \        } \

But they are very similar.

I trust you core devs here what to prefer (macro vs repetition).

@skirpichev
Copy link
Contributor

In this case, I think we should only focus on the Py_ssize_t case and don't need to convert it into a macro.

I think it's a good opportunity to refactor repetitive code.

@chris-eibl
Copy link
Member

How shall we proceed?

@srinivasreddy do you plan to fix the errors?
Shall I post a code block here that compiles and passes the tests on my machine?

Or start over in a new PR?

@srinivasreddy
Copy link
ContributorAuthor

@chris-eibl please feel free to start afresh .

@chris-eibl
Copy link
Member

Done:#129301

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@srinivasreddy@skirpichev@picnixz@chris-eibl

[8]ページ先頭

©2009-2025 Movatter.jp