-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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-129149: Create a macro PYLONG_FROM_SIGNED, and leverage it in functions #129215
Conversation
…g_FromLong, PyLong_FromLongLong and PyLong_FromSsize_t so the repetitive code is removed.
Test failures seems relevant. BTW, if you include PyLong_FromSsize_t change - please add news entry. |
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. |
Py_ssize_t case might be. |
In this case, I think we should only focus on the |
Then, we would just introduce roughly the same code block for 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 existing Those two macros would also be a pretty good fit for IMHO, all of those 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? |
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 for |
They most probably relate to size_t abs_ival; /* Use size_t for unsigned operations */ \
size_t t; \ which IMHO should read unsigned INT_TYPE abs_ival, t; /* Use size_t for unsigned operations */ \ Which will make the macro harder to use for Another option might be, to implement |
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. We can 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 for |
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). |
I think it's a good opportunity to refactor repetitive code. |
How shall we proceed? @srinivasreddy do you plan to fix the errors? Or start over in a new PR? |
@chris-eibl please feel free to start afresh . |
Done: #129301 |
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