-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
PEP 757: PyLong_Export() can fail #4025
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
Conversation
Obviously, I would prefer to keep this constraints, unless other workarounds for @encukou concerns are impossible. Removing it makes API less convenient. |
Workarounds are possible, but IMO worse. I can think of:
|
I would reiterate, that But we could put (you original proposal) version to the
The version field looks much better. Users just have to put upper bound for CPython version. If new one is coming with a new variant of But suspect, that it's overkill and int64_t value will fit our needs for 20+ years. |
The alternative is to keep the "cannot fail" sentence and implementation a deprecation by using Py_DEPRECATED() macro on the function, and remove the function after a deprecation cycle (ex: 2 Python releases). It has been done in the past. It's not perfect, but better than nothing. |
Assuming this removal, how this helps for possible deprecation of this API?
In fact, the whole API is about big integers. @encukou, do you think we can do API for reading big integers with such constraint? If we can't offer zero-copy mechanism (i.e. mpz_limbs_read-like for reading) - such API will be useless. |
When a function is modified to emit a DeprecationWarning, the warning can be treated as error in which case, the function starts failing. |
Indeed. If the internal representation changes, this API will be useless. However, I realized the user must already branch to two cases (compact and big integers). How onerous would it be to add two more (failure with exception, and an “unknown/future format” case that can keep the API from becoming entirely useless if the representation changes)? Please see the discussion thread: https://discuss.python.org/t/63895/51 |
Currently, in the gmpy2 project, there are 27 calls to the If |
gmpy2 may choose to modify I guess we can guarantee the export won't fail in 3.14. But the point here is that it's not version-specific API any more. |
That does make sense if I like more your approach in the d.p.o thread, where API doesn't fail on integers and we just return some export code (might be new in the new release). |
PR #4026 updated to follow that proposal. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Well, I would prefer if we put some message to users like "this function either fail or not fail - for all integers". At least this could be a CPython implementation detail. But it seems this not worth to debate. People will have to figure out such stuff from sources - this is still better than private API.
But you should somehow adapt PyLong_Export example. Now it's just ignores the return value, assuming integer input. I'll adjust gmpy2 pr.
I made this change to abort the process if the function fails: - PyLong_Export(obj, &long_export);
+ if (PyLong_Export(obj, &long_export) < 0) {
+ // cannot report errors to the caller
+ Py_FatalError("PyLong_Export failed");
+ } Does it look good to you? |
Lets just change function signature and return -1 here and 0 otherwise. I will have to propagate this error up, so people will not loose their work with interpreter crash. |
Ok, done. |
Merged, thanks for the review @skirpichev. |
gmpy2: mpz_set_PyLong() now returns -1 on error.
📚 Documentation preview 📚: https://pep-previews--4025.org.readthedocs.build/