Commit 90f04fa1 authored by Nanshu Chen's avatar Nanshu Chen Committed by Facebook GitHub Bot

make IOBuf.__iter__ yield Python builtin memoryview

Summary:
So it may be easier to use in Python code.

A lot of Python code will check the type with this:
```
if isinstance(obj, (bytes, memoryview)):
  ...
```

Currently, the `IOBuf.__iter__` returns a `cython.view.memoryview` c-type (which can be only "cimport" in cython but not able to "import), and to make it useable in scenarios like above, we have to explicitly cast it to builtin `memoryview`.

With this change, the use of the iterator is more natural.

Reviewed By: fried

Differential Revision: D25577842

fbshipit-source-id: 6b22028d06bba639fca32f7de3a063e1fb1baa15
parent b1433701
......@@ -28,9 +28,7 @@ class IOBuf(Hashable):
def __bytes__(self) -> bytes: ...
def __bool__(self) -> bool: ...
def __len__(self) -> int: ...
def __iter__(
self
) -> Iterator[bytes]: ... # Lies, but typing likes it for b''.join()
def __iter__(self) -> Iterator[memoryview]: ...
def __hash__(self) -> int: ...
def __eq__(self, other: Any) -> bool: ...
def __ne__(self, other: Any) -> bool: ...
......
......@@ -12,12 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from builtins import memoryview as py_memoryview
from folly.executor cimport get_executor
from cpython cimport Py_buffer
from weakref import WeakValueDictionary
from cpython.object cimport Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, Py_GE
from cython.operator cimport dereference as deref
from cython.view cimport memoryview
__cache = WeakValueDictionary()
__all__ = ['IOBuf']
......@@ -135,10 +135,10 @@ cdef class IOBuf:
def __iter__(self):
"Iterates through the chain of buffers returning a memory view for each"
yield memoryview(self, PyBUF_C_CONTIGUOUS)
yield py_memoryview(self)
next = self.next
while next is not None and next is not self:
yield memoryview(next, PyBUF_C_CONTIGUOUS)
yield py_memoryview(next)
next = next.next
def __hash__(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment