Skip to content

Add support for displaying numpy scalars #752

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 13 commits into from
Jul 13, 2025

Conversation

peytondmurray
Copy link
Contributor

This PR adds support for displaying the values of numpy scalars. Closes #744.

Following the pattern set forth elsewhere in python_data_access.rs, I used the raw pointer address of the numpy scalar to be printed. Since numpy scalars all have similar structs, e.g.

{
    ob_base: PyObject,
    obval: <value>
}

where obval can be of different sizes, I just advance the pointer to the scalar by the size of PyObject, and then use the value_type_name to know how many bytes to read. This approach avoids the need to generate numpy bindings, though if the structure of numpy scalars changes in the future we'll have to update.

Note that this only works for uint8-64, int8-64, float32-64, and bool types; other types are not printed (half-precision and 128-bit floats aren't supported with native rust types yet although there is an RFC for them, and neither are complex numbers).

@benfred benfred added the enhancement New feature or request label Jul 13, 2025
@benfred benfred changed the title [ENH] Add support for displaying numpy scalars Add support for displaying numpy scalars Jul 13, 2025
@benfred benfred merged commit 2395e2c into benfred:master Jul 13, 2025
97 checks passed
@peytondmurray
Copy link
Contributor Author

Hey, just saw this - thanks for helping this across the finish line 🎉

@peytondmurray peytondmurray deleted the 744-numpy-scalar-support branch July 14, 2025 04:27
@benfred
Copy link
Owner

benfred commented Jul 15, 2025

thanks for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

py-spy dump --locals should support numpy primitives
2 participants