Repository navigation
python: Add explicit index and bytes support #4388
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1034,6 +1034,22 @@ void init_array(nb::module_& m) { | |
| [](mx::array& a) { | ||
| return nb::cast<std::complex<double>>(to_scalar(a)); | ||
| }) | ||
| .def( | ||
| "__index__", | ||
| [](mx::array& a) { | ||
| if (!mx::issubdtype(a.dtype(), mx::integer) || a.ndim() != 0) { | ||
| throw nb::type_error( | ||
| "Only 0-dimensional integer arrays can be converted to an index."); | ||
| } | ||
| return nb::int_(to_scalar(a)); | ||
| }) | ||
| .def( | ||
| "__bytes__", | ||
| [](mx::array& a) { | ||
| a.eval(); | ||
| return nb::bytes( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going through the code when I realized this: If a refers to transpose/slice/view of some array b. byte(a) might be wrong silently. Example: x = mx.arange(10, int32) # 0 1 2 3 4 5 6 7 8 9
y = x[::2] # 0 2 4 6 8
bytes(y) #This would generate a false dump since y is not packed still. It'd take 0 1 2 3 4I'll file a new issue with more detail |
||
| reinterpret_cast<const char*>(a.data<void>()), a.nbytes()); | ||
| }) | ||
| .def( | ||
| "__format__", | ||
| [](mx::array& a, nb::object format_spec) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.