Skip to content

Commit

Permalink
Fix buffer protocol buffer size designation (ml-explore#1010)
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloskath authored Apr 19, 2024
1 parent 090ff65 commit ef5f7d1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/src/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ extern "C" inline int getbuffer(PyObject* obj, Py_buffer* view, int flags) {
view->internal = info;
view->buf = a.data<void>();
view->itemsize = a.itemsize();
view->len = a.size();
view->len = a.nbytes();
view->readonly = false;
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
view->format = const_cast<char*>(info->format.c_str());
Expand Down
12 changes: 12 additions & 0 deletions python/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import operator
import pickle
import sys
import unittest
import weakref
from copy import copy, deepcopy
Expand Down Expand Up @@ -1497,6 +1498,17 @@ def test_buffer_protocol(self):
e = cm.exception
self.assertTrue("Item size 2 for PEP 3118 buffer format string" in str(e))

# Test buffer protocol with non-arrays ie bytes
a = ord("a") * 257 + mx.arange(10).astype(mx.int16)
ab = bytes(a)
self.assertEqual(len(ab), 20)
if sys.byteorder == "little":
self.assertEqual(b"aaaaaaaaaa", ab[1::2])
self.assertEqual(b"abcdefghij", ab[::2])
else:
self.assertEqual(b"aaaaaaaaaa", ab[::2])
self.assertEqual(b"abcdefghij", ab[1::2])

def test_buffer_protocol_ref_counting(self):
a = mx.arange(3)
wr = weakref.ref(a)
Expand Down

0 comments on commit ef5f7d1

Please sign in to comment.