Skip to content

Updates to better support ormsgpack on GraalPy. #5121

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

Merged
merged 3 commits into from
May 23, 2025

Conversation

timfel
Copy link
Contributor

@timfel timfel commented May 7, 2025

I noticed these things mostly when trying to run ormsgpack on GraalPy, but I think they are useful in general, so putting it out here to get some feedback.

@timfel timfel force-pushed the tim/ormsgpack-graalpy-updates branch 3 times, most recently from 160af8f to 5f8690c Compare May 9, 2025 07:03
@timfel
Copy link
Contributor Author

timfel commented May 9, 2025

I'm not sure what to do about the total code coverage. With the added test, the patch coverage reports 100% of the diff is hit, but the overall coverage went down? How is that possible?

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks for this. The coverage has a known bug in #5080, which I'm working to debug.

I have added CI-build-full label which you'll need for the GraalPy test run anyway, the coverage should match main with that label applied.

Main comment which I have is that I'd prefer we did not expose _PyLong_Sign.

@davidhewitt davidhewitt mentioned this pull request May 9, 2025
@timfel timfel force-pushed the tim/ormsgpack-graalpy-updates branch from 5f8690c to 2c9ff86 Compare May 16, 2025 15:07
Comment on lines 27 to 34
#[cfg(not(Py_LIMITED_API))]
#[inline]
pub unsafe fn PyBytes_AS_STRING(op: *mut PyObject) -> *const c_char {
#[cfg(not(any(PyPy, GraalPy)))]
return &(*op.cast::<PyBytesObject>()).ob_sval as *const c_char;
#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))]
return crate::PyBytes_AsString(op);
}
Copy link
Member

Choose a reason for hiding this comment

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

Please do not put the shim here, put it in https://docs.rs/pyo3-ffi/latest/pyo3_ffi/compat/index.html

Copy link
Member

Choose a reason for hiding this comment

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

Also, this most likely does not do what you intend, since cfg(Py_LIMITED_API) is never true inside this function.

Copy link
Contributor Author

@timfel timfel May 19, 2025

Choose a reason for hiding this comment

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

Also, this most likely does not do what you intend, since cfg(Py_LIMITED_API) is never true inside this function.

True, the entire function shouldn't be there on the limited API, I'll just drop this inside the function.

Please do not put the shim here, put it in https://docs.rs/pyo3-ffi/latest/pyo3_ffi/compat/index.html

I'm not sure I follow. The docs for the compat module say "Some CPython C API functions added in recent versions of Python are inherently safer to use than older C API constructs. This module exposes functions available on all Python versions that wrap the old C API on old Python versions and wrap the function directly on newer Python versions.". PyBytes_AS_STRING is neither new nor safe. Should it really be in the compat module if the docs then give the impression that it's a "newer" and "safer" API than alternative?

In my mind PyBytes_AS_STRING is really analogous to things like PyTuple_GET_ITEM etc, which is in cpython/tupleobject.rs, so cpython/bytesobject.rs seems to make sense here imo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All that being said - I don't mind dropping it entirely if you think it shouldn't be there at all. I've just seen it used in both ormsgpack and orjson so I thought there's demand for this (and I wanted to try and minimize the diff I'll propose to those projects to support GraalPy)

Copy link
Member

Choose a reason for hiding this comment

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

What you've written here is a shim, and we want this part of pyo3-ffi to correspond to the cpython headers as much as possible. So either the conditonal compilation inside this function shouldn't exist or it should be in the compat module.

@timfel timfel force-pushed the tim/ormsgpack-graalpy-updates branch from 2c9ff86 to 75198eb Compare May 19, 2025 07:38
@timfel timfel force-pushed the tim/ormsgpack-graalpy-updates branch from 75198eb to e12a0b8 Compare May 19, 2025 09:23
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks, generally looks good, I think I just spotted one thing.

@@ -147,13 +147,13 @@ pub struct PyVarObject {
// skipped private _PyVarObject_CAST

#[inline]
#[cfg(not(all(PyPy, Py_3_10)))]
#[cfg(not(all(GraalPy, PyPy, Py_3_10)))]
Copy link
Member

Choose a reason for hiding this comment

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

This looks wrong, it's impossible to have graalpy && pypy.

Probably meant to be

Suggested change
#[cfg(not(all(GraalPy, PyPy, Py_3_10)))]
#[cfg(not(any(GraalPy, all(PyPy, Py_3_10))))]

#[cfg_attr(docsrs, doc(cfg(all())))]
pub unsafe fn Py_Is(x: *mut PyObject, y: *mut PyObject) -> c_int {
(x == y).into()
}

#[cfg(all(PyPy, Py_3_10))]
#[cfg(all(GraalPy, PyPy, Py_3_10))]
Copy link
Member

Choose a reason for hiding this comment

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

Similar I guess

Suggested change
#[cfg(all(GraalPy, PyPy, Py_3_10))]
#[cfg(any(GraalPy, all(PyPy, Py_3_10)))]

@timfel timfel force-pushed the tim/ormsgpack-graalpy-updates branch from e12a0b8 to 57349c3 Compare May 22, 2025 11:53
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks, this LGTM 👍

@davidhewitt davidhewitt added this pull request to the merge queue May 23, 2025
Merged via the queue into PyO3:main with commit f881298 May 23, 2025
93 of 95 checks passed
davidhewitt pushed a commit that referenced this pull request Jun 11, 2025
* Expose the vectorcall functions also on GraalPy

* Add PyBytes_AS_STRING, seen in ormsgpack

* Call Py_Is function on GraalPy
davidhewitt pushed a commit that referenced this pull request Jun 12, 2025
* Expose the vectorcall functions also on GraalPy

* Add PyBytes_AS_STRING, seen in ormsgpack

* Call Py_Is function on GraalPy
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.

3 participants