Nir Soffer
2019-Nov-23 01:27 UTC
[Libguestfs] [PATCH] python: Support buffer protocol for pread() result
pread() can return now any object supporting the buffer protocol[1] such as bytearray, bytes, or memoryview. [1] https://docs.python.org/3/c-api/buffer.html --- When the new python tests will be in master we can add some automated tests. For now I tested this manually using the example plugin as is and with these changes: def pread(h, count, offset): global disk return bytes(disk[offset:offset+count]) def pread(h, count, offset): global disk return memoryview(disk)[offset:offset+count] plugins/python/python.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/plugins/python/python.c b/plugins/python/python.c index 148097f..ed299ff 100644 --- a/plugins/python/python.c +++ b/plugins/python/python.c @@ -445,10 +445,12 @@ py_pread (void *handle, void *buf, PyObject *obj = handle; PyObject *fn; PyObject *r; + Py_buffer view = {0}; + int ret = -1; if (!callback_defined ("pread", &fn)) { nbdkit_error ("%s: missing callback: %s", script, "pread"); - return -1; + return ret; } PyErr_Clear (); @@ -456,25 +458,30 @@ py_pread (void *handle, void *buf, r = PyObject_CallFunction (fn, "OiL", obj, count, offset, NULL); Py_DECREF (fn); if (check_python_failure ("pread") == -1) - return -1; + return ret; - if (!PyByteArray_Check (r)) { - nbdkit_error ("%s: value returned from pread method is not a byte array", + if (PyObject_GetBuffer (r, &view, PyBUF_SIMPLE) == -1) { + nbdkit_error ("%s: value returned from pread does not support the " + "buffer protocol", script); - Py_DECREF (r); - return -1; + goto out; } - if (PyByteArray_Size (r) < count) { - nbdkit_error ("%s: byte array returned from pread is too small", script); - Py_DECREF (r); - return -1; + if (view.len < count) { + nbdkit_error ("%s: buffer returned from pread is too small", script); + goto out; } - memcpy (buf, PyByteArray_AsString (r), count); + memcpy (buf, view.buf, count); + ret = 0; + +out: + if (view.obj) + PyBuffer_Release (&view); + Py_DECREF (r); - return 0; + return ret; } static int -- 2.21.0
Richard W.M. Jones
2019-Nov-23 13:21 UTC
Re: [Libguestfs] [PATCH] python: Support buffer protocol for pread() result
On Sat, Nov 23, 2019 at 03:27:54AM +0200, Nir Soffer wrote:> pread() can return now any object supporting the buffer protocol[1] such > as bytearray, bytes, or memoryview. > > [1] https://docs.python.org/3/c-api/buffer.htmlACK, I have pushed this.> --- > > When the new python tests will be in master we can add some automated tests.Don't worry about updating the tests, I've already done it and will post a renewed series testing this feature shortly. Thanks, Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
Maybe Matching Threads
- [PATCH nbdkit 1/2] python: For v2 API, avoid copy by passing a buffer to pread.
- [PATCH nbdkit 0/2] python: Implement pread passing buffer for v2 API.
- [PATCH nbdkit] python: Pass memoryview to pwrite()
- [PATCH]: add libxl python binding
- samba-tool user syncpasswords crashes with python3