[libvirt] [PATCH python 12/14] override: Switch virStreamSend wrapper to use libvirt_charPtrSizeUnwrap

2013-12-09 Thread Daniel P. Berrange
From: Daniel P. Berrange berra...@redhat.com

Instead of using a 'z#i' format string to receive byte array,
use 'O' and then libvirt_charPtrSizeUnwrap. This lets us hide
the Python 3 vs 2 differences in typewrappers.c

Signed-off-by: Daniel P. Berrange berra...@redhat.com
---
 libvirt-override.c | 11 ++-
 typewrappers.c | 19 +++
 typewrappers.h |  1 +
 3 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/libvirt-override.c b/libvirt-override.c
index 77c0af2..7e54cf6 100644
--- a/libvirt-override.c
+++ b/libvirt-override.c
@@ -6770,21 +6770,22 @@ libvirt_virStreamSend(PyObject *self ATTRIBUTE_UNUSED,
 {
 PyObject *py_retval;
 PyObject *pyobj_stream;
+PyObject *pyobj_data;
 virStreamPtr stream;
 char *data;
-int datalen;
+Py_ssize_t datalen;
 int ret;
-int nbytes;
 
-if (!PyArg_ParseTuple(args, (char *) Oz#i:virStreamRecv,
-  pyobj_stream, data, datalen, nbytes)) {
+if (!PyArg_ParseTuple(args, (char *) OO:virStreamRecv,
+  pyobj_stream, pyobj_data)) {
 DEBUG(%s failed to parse tuple\n, __FUNCTION__);
 return VIR_PY_INT_FAIL;
 }
 stream = PyvirStream_Get(pyobj_stream);
+libvirt_charPtrSizeUnwrap(pyobj_data, data, datalen);
 
 LIBVIRT_BEGIN_ALLOW_THREADS;
-ret = virStreamSend(stream, data, nbytes);
+ret = virStreamSend(stream, data, datalen);
 LIBVIRT_END_ALLOW_THREADS;
 
 DEBUG(StreamSend ret=%d\n, ret);
diff --git a/typewrappers.c b/typewrappers.c
index 7331cbd..a8cca30 100644
--- a/typewrappers.c
+++ b/typewrappers.c
@@ -385,6 +385,25 @@ libvirt_charPtrUnwrap(PyObject *obj, char **str)
 return 0;
 }
 
+int libvirt_charPtrSizeUnwrap(PyObject *obj, char **str, Py_ssize_t *size)
+{
+int ret;
+*str = NULL;
+*size = 0;
+if (!obj) {
+PyErr_SetString(PyExc_TypeError, unexpected type);
+return -1;
+}
+
+#if PY_MAJOR_VERSION  2
+ret = PyBytes_AsStringAndSize(obj, str, size);
+#else
+ret = PyString_AsStringAndSize(obj, str, size);
+#endif
+
+return ret;
+}
+
 PyObject *
 libvirt_virDomainPtrWrap(virDomainPtr node)
 {
diff --git a/typewrappers.h b/typewrappers.h
index 6bb193c..ed1e4a3 100644
--- a/typewrappers.h
+++ b/typewrappers.h
@@ -175,6 +175,7 @@ int libvirt_ulonglongUnwrap(PyObject *obj, unsigned long 
long *val);
 int libvirt_doubleUnwrap(PyObject *obj, double *val);
 int libvirt_boolUnwrap(PyObject *obj, bool *val);
 int libvirt_charPtrUnwrap(PyObject *obj, char **str);
+int libvirt_charPtrSizeUnwrap(PyObject *obj, char **str, Py_ssize_t *size);
 PyObject * libvirt_virConnectPtrWrap(virConnectPtr node);
 PyObject * libvirt_virDomainPtrWrap(virDomainPtr node);
 PyObject * libvirt_virNetworkPtrWrap(virNetworkPtr node);
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH python 12/14] override: Switch virStreamSend wrapper to use libvirt_charPtrSizeUnwrap

2013-12-09 Thread Doug Goldstein
On Mon, Dec 9, 2013 at 9:15 AM, Daniel P. Berrange berra...@redhat.com wrote:
 From: Daniel P. Berrange berra...@redhat.com

 Instead of using a 'z#i' format string to receive byte array,
 use 'O' and then libvirt_charPtrSizeUnwrap. This lets us hide
 the Python 3 vs 2 differences in typewrappers.c

 Signed-off-by: Daniel P. Berrange berra...@redhat.com
 ---
  libvirt-override.c | 11 ++-
  typewrappers.c | 19 +++
  typewrappers.h |  1 +
  3 files changed, 26 insertions(+), 5 deletions(-)

 diff --git a/libvirt-override.c b/libvirt-override.c
 index 77c0af2..7e54cf6 100644
 --- a/libvirt-override.c
 +++ b/libvirt-override.c
 @@ -6770,21 +6770,22 @@ libvirt_virStreamSend(PyObject *self ATTRIBUTE_UNUSED,
  {
  PyObject *py_retval;
  PyObject *pyobj_stream;
 +PyObject *pyobj_data;
  virStreamPtr stream;
  char *data;
 -int datalen;
 +Py_ssize_t datalen;
  int ret;
 -int nbytes;

 -if (!PyArg_ParseTuple(args, (char *) Oz#i:virStreamRecv,
 -  pyobj_stream, data, datalen, nbytes)) {
 +if (!PyArg_ParseTuple(args, (char *) OO:virStreamRecv,
 +  pyobj_stream, pyobj_data)) {
  DEBUG(%s failed to parse tuple\n, __FUNCTION__);
  return VIR_PY_INT_FAIL;
  }
  stream = PyvirStream_Get(pyobj_stream);
 +libvirt_charPtrSizeUnwrap(pyobj_data, data, datalen);

  LIBVIRT_BEGIN_ALLOW_THREADS;
 -ret = virStreamSend(stream, data, nbytes);
 +ret = virStreamSend(stream, data, datalen);
  LIBVIRT_END_ALLOW_THREADS;

  DEBUG(StreamSend ret=%d\n, ret);
 diff --git a/typewrappers.c b/typewrappers.c
 index 7331cbd..a8cca30 100644
 --- a/typewrappers.c
 +++ b/typewrappers.c
 @@ -385,6 +385,25 @@ libvirt_charPtrUnwrap(PyObject *obj, char **str)
  return 0;
  }

 +int libvirt_charPtrSizeUnwrap(PyObject *obj, char **str, Py_ssize_t *size)
 +{
 +int ret;
 +*str = NULL;
 +*size = 0;
 +if (!obj) {
 +PyErr_SetString(PyExc_TypeError, unexpected type);
 +return -1;
 +}
 +
 +#if PY_MAJOR_VERSION  2
 +ret = PyBytes_AsStringAndSize(obj, str, size);
 +#else
 +ret = PyString_AsStringAndSize(obj, str, size);
 +#endif
 +
 +return ret;
 +}
 +
  PyObject *
  libvirt_virDomainPtrWrap(virDomainPtr node)
  {
 diff --git a/typewrappers.h b/typewrappers.h
 index 6bb193c..ed1e4a3 100644
 --- a/typewrappers.h
 +++ b/typewrappers.h
 @@ -175,6 +175,7 @@ int libvirt_ulonglongUnwrap(PyObject *obj, unsigned long 
 long *val);
  int libvirt_doubleUnwrap(PyObject *obj, double *val);
  int libvirt_boolUnwrap(PyObject *obj, bool *val);
  int libvirt_charPtrUnwrap(PyObject *obj, char **str);
 +int libvirt_charPtrSizeUnwrap(PyObject *obj, char **str, Py_ssize_t *size);
  PyObject * libvirt_virConnectPtrWrap(virConnectPtr node);
  PyObject * libvirt_virDomainPtrWrap(virDomainPtr node);
  PyObject * libvirt_virNetworkPtrWrap(virNetworkPtr node);
 --
 1.8.3.1

 --
 libvir-list mailing list
 libvir-list@redhat.com
 https://www.redhat.com/mailman/listinfo/libvir-list

I don't really know the format specifier but it seems reasonably
correct that O is object and then you're unwrapping it which makes
sense so ACK.

-- 
Doug Goldstein

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list