From c5b168f73b0add8cc66ada72f2e4be25b2fb33ed Mon Sep 17 00:00:00 2001 From: Michael Schroeder Date: Wed, 20 Nov 2013 12:58:09 +0100 Subject: tweak python version detection, add Datamatch.binary() Also no longer misuse swig's cdata typemap. --- bindings/python/CMakeLists.txt | 10 ++++-- bindings/solv.i | 72 +++++++++++++++++++++++++++--------------- 2 files changed, 54 insertions(+), 28 deletions(-) (limited to 'bindings') diff --git a/bindings/python/CMakeLists.txt b/bindings/python/CMakeLists.txt index 488843b..6c1b514 100644 --- a/bindings/python/CMakeLists.txt +++ b/bindings/python/CMakeLists.txt @@ -1,16 +1,20 @@ #SET (PythonLibs_FIND_VERSION 3) -#SET (PythonInterp_FIND_VERSION 3) -#SET (${SWIG_FLAGS} ${SWIG_FLAGS} -DPYTHON3=1) + FIND_PACKAGE (PythonLibs) +SET (PythonInterp_FIND_VERSION ${PYTHONLIBS_VERSION_STRING}) FIND_PACKAGE (PythonInterp REQUIRED) EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "from sys import stdout; from distutils import sysconfig; stdout.write(sysconfig.get_python_lib(True))" OUTPUT_VARIABLE PYTHON_INSTALL_DIR) +IF (${PYTHON_VERSION_MAJOR} GREATER 2) + SET (SWIG_PY_FLAGS -DPYTHON3=1) +ENDIF (${PYTHON_VERSION_MAJOR} GREATER 2) + MESSAGE (STATUS "Python executable: ${PYTHON_EXECUTABLE}") MESSAGE (STATUS "Python installation dir: ${PYTHON_INSTALL_DIR}") ADD_CUSTOM_COMMAND ( OUTPUT solv_python.c - COMMAND ${SWIG_EXECUTABLE} ${SWIG_FLAGS} -python -I${CMAKE_SOURCE_DIR}/src -o solv_python.c ${CMAKE_SOURCE_DIR}/bindings/solv.i + COMMAND ${SWIG_EXECUTABLE} ${SWIG_FLAGS} -python ${SWIG_PY_FLAGS} -I${CMAKE_SOURCE_DIR}/src -o solv_python.c ${CMAKE_SOURCE_DIR}/bindings/solv.i WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} DEPENDS ${CMAKE_SOURCE_DIR}/bindings/solv.i ) diff --git a/bindings/solv.i b/bindings/solv.i index 1a6c13c..67212a2 100644 --- a/bindings/solv.i +++ b/bindings/solv.i @@ -10,7 +10,18 @@ %markfunc Pool "mark_Pool"; #endif -%typemap(in,noblock=1,fragment="SWIG_AsCharPtrAndSize") (const unsigned char *str, int len) (int res, char *buf = 0, size_t size = 0, int alloc = 0) { +# +# binaryblob handling +# + +%{ +typedef struct { + const void *data; + size_t len; +} BinaryBlob; +%} + +%typemap(in,noblock=1,fragment="SWIG_AsCharPtrAndSize") (const unsigned char *str, size_t len) (int res, char *buf = 0, size_t size = 0, int alloc = 0) { res = SWIG_AsCharPtrAndSize($input, &buf, &size, &alloc); if (!SWIG_IsOK(res)) { #if defined(SWIGPYTHON) @@ -18,7 +29,7 @@ Py_ssize_t pysize = 0; res = PyObject_AsReadBuffer($input, &pybuf, &pysize); if (res < 0) { - SWIG_exception_fail(SWIG_ArgError(res), "not a string or buffer"); + %argument_fail(res, "BinaryBlob", $symname, $argnum); } else { buf = (void *)pybuf; size = pysize; @@ -35,6 +46,14 @@ if (alloc$argnum == SWIG_NEWOBJ) %delete_array(buf$argnum); } +%typemap(out,noblock=1,fragment="SWIG_FromCharPtrAndSize") BinaryBlob { +#if defined(SWIGPYTHON) && defined(PYTHON3) + $result = $1.data ? Py_BuildValue("y#", $1.data, $1.len) : SWIG_Py_Void(); +#else + $result = SWIG_FromCharPtrAndSize($1.data, $1.len); +#endif +} + #if defined(SWIGPYTHON) %typemap(in) Queue { /* Check if is a list */ @@ -338,10 +357,6 @@ typedef VALUE AppObjectPtr; #endif -#if !defined(SWIGPYTHON) -%include "cdata.i" -#endif - #ifdef SWIGPYTHON %include "file.i" #else @@ -970,8 +985,8 @@ typedef struct { return solv_chksum_get_type(chk); } %} - void add(const unsigned char *str, int len) { - solv_chksum_add($self, str, len); + void add(const unsigned char *str, size_t len) { + solv_chksum_add($self, str, (int)len); } void add_fp(FILE *fp) { char buf[4096]; @@ -1005,21 +1020,15 @@ typedef struct { solv_chksum_add($self, &stb.st_size, sizeof(stb.st_size)); solv_chksum_add($self, &stb.st_mtime, sizeof(stb.st_mtime)); } -#if defined(SWIGPYTHON) - PyObject *raw() { + BinaryBlob raw() { + BinaryBlob bl; int l; const unsigned char *b; b = solv_chksum_get($self, &l); - return Py_BuildValue(PY_MAJOR_VERSION < 3 ? "s#" : "y#", b, l); + bl.data = b; + bl.len = l; + return bl; } -#else - SWIGCDATA raw() { - int l; - const unsigned char *b; - b = solv_chksum_get($self, &l); - return cdata_void((void *)b, l); - } -#endif %newobject hex; char *hex() { int l; @@ -1891,18 +1900,31 @@ rb_eval_string( return pool_id2str($self->pool, $self->key->type); } Id id() { - return $self->kv.id; + return $self->kv.id; } const char *idstr() { - return pool_id2str($self->pool, $self->kv.id); + return pool_id2str($self->pool, $self->kv.id); } const char *str() { - return $self->kv.str; + return $self->kv.str; + } + BinaryBlob binary() { + BinaryBlob bl; + bl.data = 0; + bl.len = 0; + if ($self->key->type == REPOKEY_TYPE_BINARY) + { + bl.data = $self->kv.str; + bl.len = $self->kv.num; + } + else if ((bl.len = solv_chksum_len($self->key->type)) != 0) + bl.data = $self->kv.str; + return bl; } unsigned long long num() { - if ($self->key->type == REPOKEY_TYPE_NUM) - return SOLV_KV_NUM64(&$self->kv); - return $self->kv.num; + if ($self->key->type == REPOKEY_TYPE_NUM) + return SOLV_KV_NUM64(&$self->kv); + return $self->kv.num; } int num2() { return $self->kv.num2; -- cgit v1.2.3