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
Consider adding public PyLong_FlipSign() function #120446
For small integers it will fallback to PyLong_FromLong() (like _PyLong_Negate() does), for the rest - just _PyLong_FlipSign() to change sign in place.
Alternatively, we could just expose a direct wrapper of _PyLong_FlipSign() and warn users to not use this function for small ints (that doesn't make sense for me anyway).
In both variants, on success - flip the sign of obj and return 0. On failure - return -1 with exception set. Shouldn't fail on PyLongObject or it's subtype.
I'll work on a patch if this proposal does make sense.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Activity
serhiy-storchaka commentedon Jun 13, 2024
I planned to propose adding
PyLong_NegativeInPlace()andPyLong_AbsoluteInPlace().skirpichev commentedon Jun 13, 2024
Hmm, it seems that my first variant can't be implemented with same signature as the second. Rather it should be (like
_PyLong_Negate()):pythongh-120446: add PyLong_FlipSign() public function
skirpichev commentedon Jun 14, 2024
I think this naming better fits case, when there will be also other
PyNumber_*-like functions. But I'm not sure that we have practical need for that.FYI, PR: #120489 (as a draft; IIUC, this should be discussed in C-API WG)
skirpichev commentedon Jul 4, 2024
PR closed in favor of #121339.
Now I don't have a strong motivation for this function. Kept open as @serhiy-storchaka planned a similar proposal.
vstinner commentedon Jan 27, 2025
Python int objects are immutable. I dislike APIs which seem to treat them as mutable objects.
What's the status of this issue? PEP 757 has been implementation in Python 3.14. Can we close this issue?
encukou commentedon Jan 28, 2025
Proposal should go to the C API WG (ideally after public discussion)