Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #399 from gsomix/python3_interface
updates on python3 interface
  • Loading branch information
Soeren Sonnenburg committed Mar 28, 2012
2 parents c375b8f + da550bd commit 355cb51
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 20 deletions.
13 changes: 7 additions & 6 deletions src/configure
Expand Up @@ -1591,7 +1591,7 @@ set_install_dirs() {
python_major=`$PYTHON -V 2>&1 | cut -f 2 -d ' ' | cut -f 1 -d '.'`
python_minor=`$PYTHON -V 2>&1 | cut -f 2 -d ' ' | cut -f 2 -d '.'`
if test -z "$_pydir" ; then
if test "$python_major" -ge 2 && test "$python_minor" -ge 6 ; then
if (test "$python_major" -ge 2 && test "$python_minor" -ge 6) || (test "$python_major" -ge 3) ; then
_pydir="`$PYTHON -c 'import sys; print(\"python\"+sys.version[0:3])'`/dist-packages"
else
_pydir="`$PYTHON -c 'import sys; print(\"python\"+sys.version[0:3])'`/site-packages"
Expand Down Expand Up @@ -4379,11 +4379,6 @@ EOF
fi
fi

if [ `echo $PYVER | cut -c 1` -gt 2 ]
then
SWIGFLAGS_PYTHON_MODULAR="${SWIGFLAGS_PYTHON_MODULAR} -py3"
fi

if test "$_python" = yes
then
DEFINES_PYTHON="$DEFINES_PYTHON -DHAVE_PYTHON"
Expand All @@ -4400,6 +4395,12 @@ EOF
COMPFLAGS_CPP_PYTHON=""
COMPFLAGS_C_PYTHON=""
fi

if [ `echo $PYVER | cut -c 1` -gt 2 ]
then
SWIGFLAGS_PYTHON_MODULAR="${SWIGFLAGS_PYTHON_MODULAR} -py3"
DEFINES_PYTHON="${DEFINES_PYTHON} -DPYTHON3"
fi
}

test_r()
Expand Down
10 changes: 10 additions & 0 deletions src/interfaces/modular/SGBase.i
Expand Up @@ -323,7 +323,11 @@ namespace shogun
char* result=CFile::read_whole_file(fname, len);
unlink(fname);

#ifdef PYTHON3
PyObject* str=PyUnicode_FromStringAndSize(result, len);
#else
PyObject* str=PyString_FromStringAndSize(result, len);
#endif
SG_FREE(result);

PyObject* tuple=PyTuple_New(2);
Expand All @@ -340,7 +344,13 @@ namespace shogun
PyObject* py_str = PyTuple_GetItem(state,1);
char* str=NULL;
Py_ssize_t len=0;

#ifdef PYTHON3
len = PyUnicode_GetSize((PyObject*) py_str);
str = PyBytes_AsString(PyUnicode_AsASCIIString(const_cast<PyObject*>(py_str)));
#else
PyString_AsStringAndSize(py_str, &str, &len);
#endif

char* fname=tmpnam(NULL);
FILE* tmpf=fopen(fname, "w");;
Expand Down
130 changes: 116 additions & 14 deletions src/interfaces/python_modular/swig_typemaps.i
Expand Up @@ -29,11 +29,6 @@ extern "C" {
#include <numpy/arrayobject.h>
}

#if PY_VERSION_HEX >= 0x03000000
#define PyString_FromStringAndSize PyBytes_FromStringAndSize
#define PyString_AsStringAndSize PyBytes_AsStringAndSize
#endif

/* Functions to extract array attributes.
*/
static bool is_array(PyObject* a) { return (a) && PyArray_Check(a); }
Expand All @@ -47,8 +42,18 @@ static bool array_is_contiguous(PyObject* a) { return PyArray_ISCONTIGUOUS(a); }
static const char* typecode_string(PyObject* py_obj) {
if (py_obj == NULL ) return "C NULL value";
if (PyCallable_Check(py_obj)) return "callable" ;

#if PY_VERSION_HEX >= 0x03000000
if (PyUnicode_Check( py_obj)) return "unicode" ;
#else
if (PyString_Check( py_obj)) return "string" ;
#endif

#if PY_VERSION_HEX >= 0x03000000
if (PyLong_Check( py_obj)) return "long" ;
#else
if (PyInt_Check( py_obj)) return "int" ;
#endif
if (PyFloat_Check( py_obj)) return "float" ;
if (PyDict_Check( py_obj)) return "dict" ;
if (PyList_Check( py_obj)) return "list" ;
Expand Down Expand Up @@ -201,9 +206,14 @@ static int is_pystring_list(PyObject* obj, int typecode)
for (int32_t i=0; i<size; i++)
{
PyObject *o = PyList_GetItem(list,i);
if (typecode == NPY_STRING)

if (typecode == NPY_STRING || typecode == NPY_UNICODE)
{
if (!PyString_Check(o))
#if PY_VERSION_HEX >= 0x03000000
if (!PyUnicode_Check(o))
#else
if (!PyString_Check(o))
#endif
{
result=0;
break;
Expand Down Expand Up @@ -357,13 +367,23 @@ static bool string_from_strpy(SGStringList<type>& sg_strings, PyObject* obj, int
for (int32_t i=0; i<size; i++)
{
PyObject *o = PyList_GetItem(list,i);
if (typecode == NPY_STRING)
if (typecode == NPY_STRING || typecode == NPY_UNICODE)
{
if (PyString_Check(o))
#if PY_VERSION_HEX >= 0x03000000
if (PyUnicode_Check(o))
#else
if (PyString_Check(o))
#endif
{
int32_t len=PyString_Size(o);
max_len=shogun::CMath::max(len,max_len);
const char* str=PyString_AsString(o);

#if PY_VERSION_HEX >= 0x03000000
int32_t len = PyUnicode_GetSize((PyObject*) o);
const char* str = PyBytes_AsString(PyUnicode_AsASCIIString(const_cast<PyObject*>(o)));
#else
int32_t len = PyString_Size(o);
const char* str = PyString_AsString(o);
#endif
max_len=shogun::CMath::max(len,max_len);

strings[i].slen=len;
strings[i].string=NULL;
Expand Down Expand Up @@ -449,11 +469,15 @@ static bool string_to_strpy(PyObject* &obj, SGStringList<type> sg_strings, int t
{
PyObject* s=NULL;

if (typecode == NPY_STRING)
if (typecode == NPY_STRING || typecode == NPY_UNICODE)
{
/* This path is only taking if str[i].string is a char*. However this cast is
required to build through for non char types. */
s=PyString_FromStringAndSize((char*) str[i].string, str[i].slen);
#if PY_VERSION_HEX >= 0x03000000
s=PyUnicode_FromStringAndSize((char*) str[i].string, str[i].slen);
#else
s=PyString_FromStringAndSize((char*) str[i].string, str[i].slen);
#endif
}
else
{
Expand Down Expand Up @@ -544,8 +568,13 @@ static bool spmatrix_from_numpy(SGSparseMatrix<type>& sg_matrix, PyObject* obj,
}

/* get array dimensions */
#if PY_VERSION_HEX >= 0x03000000
int32_t num_feat=PyLong_AsLong(PyTuple_GetItem(shape, 0));
int32_t num_vec=PyLong_AsLong(PyTuple_GetItem(shape, 1));
#else
int32_t num_feat=PyInt_AsLong(PyTuple_GetItem(shape, 0));
int32_t num_vec=PyInt_AsLong(PyTuple_GetItem(shape, 1));
#endif

/* get indptr array */
int is_new_object_indptr=0;
Expand Down Expand Up @@ -766,11 +795,19 @@ static bool spvector_to_numpy(PyObject* &obj, SGSparseVector<type> sg_vector, in

/* Define concrete examples of the TYPEMAP_IN_SGVECTOR macros */
TYPEMAP_IN_SGVECTOR(bool, NPY_BOOL)
#ifdef PYTHON3 // str -> unicode for python3
TYPEMAP_IN_SGVECTOR(char, NPY_UNICODE)
#else
TYPEMAP_IN_SGVECTOR(char, NPY_STRING)
#endif
TYPEMAP_IN_SGVECTOR(uint8_t, NPY_UINT8)
TYPEMAP_IN_SGVECTOR(int16_t, NPY_INT16)
TYPEMAP_IN_SGVECTOR(uint16_t, NPY_UINT16)
#ifdef PYTHON3 // int -> long for python3
TYPEMAP_IN_SGVECTOR(int32_t, NPY_INT)
#else
TYPEMAP_IN_SGVECTOR(int32_t, NPY_INT32)
#endif
TYPEMAP_IN_SGVECTOR(uint32_t, NPY_UINT32)
TYPEMAP_IN_SGVECTOR(int64_t, NPY_INT64)
TYPEMAP_IN_SGVECTOR(uint64_t, NPY_UINT64)
Expand All @@ -792,11 +829,19 @@ TYPEMAP_IN_SGVECTOR(PyObject, NPY_OBJECT)

/* Define concrete examples of the TYPEMAP_OUT_SGVECTOR macros */
TYPEMAP_OUT_SGVECTOR(bool, NPY_BOOL)
#ifdef PYTHON3 // str -> unicode for python3
TYPEMAP_OUT_SGVECTOR(char, NPY_UNICODE)
#else
TYPEMAP_OUT_SGVECTOR(char, NPY_STRING)
#endif
TYPEMAP_OUT_SGVECTOR(uint8_t, NPY_UINT8)
TYPEMAP_OUT_SGVECTOR(int16_t, NPY_INT16)
TYPEMAP_OUT_SGVECTOR(uint16_t, NPY_UINT16)
#ifdef PYTHON3 // int -> long for python3
TYPEMAP_OUT_SGVECTOR(int32_t, NPY_INT)
#else
TYPEMAP_OUT_SGVECTOR(int32_t, NPY_INT32)
#endif
TYPEMAP_OUT_SGVECTOR(uint32_t, NPY_UINT32)
TYPEMAP_OUT_SGVECTOR(int64_t, NPY_INT64)
TYPEMAP_OUT_SGVECTOR(uint64_t, NPY_UINT64)
Expand All @@ -823,11 +868,19 @@ TYPEMAP_OUT_SGVECTOR(PyObject, NPY_OBJECT)

/* Define concrete examples of the TYPEMAP_IN_SGMATRIX macros */
TYPEMAP_IN_SGMATRIX(bool, NPY_BOOL)
#ifdef PYTHON3 // str -> unicode for python3
TYPEMAP_IN_SGMATRIX(char, NPY_UNICODE)
#else
TYPEMAP_IN_SGMATRIX(char, NPY_STRING)
#endif
TYPEMAP_IN_SGMATRIX(uint8_t, NPY_UINT8)
TYPEMAP_IN_SGMATRIX(int16_t, NPY_INT16)
TYPEMAP_IN_SGMATRIX(uint16_t, NPY_UINT16)
#ifdef PYTHON3 // int -> long for python3
TYPEMAP_IN_SGMATRIX(int32_t, NPY_INT)
#else
TYPEMAP_IN_SGMATRIX(int32_t, NPY_INT32)
#endif
TYPEMAP_IN_SGMATRIX(uint32_t, NPY_UINT32)
TYPEMAP_IN_SGMATRIX(int64_t, NPY_INT64)
TYPEMAP_IN_SGMATRIX(uint64_t, NPY_UINT64)
Expand All @@ -849,11 +902,19 @@ TYPEMAP_IN_SGMATRIX(PyObject, NPY_OBJECT)

/* Define concrete examples of the TYPEMAP_OUT_SGMATRIX macros */
TYPEMAP_OUT_SGMATRIX(bool, NPY_BOOL)
#ifdef PYTHON3 // str -> unicode for python3
TYPEMAP_OUT_SGMATRIX(char, NPY_UNICODE)
#else
TYPEMAP_OUT_SGMATRIX(char, NPY_STRING)
#endif
TYPEMAP_OUT_SGMATRIX(uint8_t, NPY_UINT8)
TYPEMAP_OUT_SGMATRIX(int16_t, NPY_INT16)
TYPEMAP_OUT_SGMATRIX(uint16_t, NPY_UINT16)
#ifdef PYTHON3 // int -> long for python3
TYPEMAP_OUT_SGMATRIX(int32_t, NPY_INT)
#else
TYPEMAP_OUT_SGMATRIX(int32_t, NPY_INT32)
#endif
TYPEMAP_OUT_SGMATRIX(uint32_t, NPY_UINT32)
TYPEMAP_OUT_SGMATRIX(int64_t, NPY_INT64)
TYPEMAP_OUT_SGMATRIX(uint64_t, NPY_UINT64)
Expand Down Expand Up @@ -881,11 +942,19 @@ TYPEMAP_OUT_SGMATRIX(PyObject, NPY_OBJECT)

/* Define concrete examples of the TYPEMAP_INND macros */
TYPEMAP_INND(bool, NPY_BOOL)
#ifdef PYTHON3 // str -> unicode for python3
TYPEMAP_INND(char, NPY_UNICODE)
#else
TYPEMAP_INND(char, NPY_STRING)
#endif
TYPEMAP_INND(uint8_t, NPY_UINT8)
TYPEMAP_INND(int16_t, NPY_INT16)
TYPEMAP_INND(uint16_t, NPY_UINT16)
#ifdef PYTHON3 // int -> long for python3
TYPEMAP_INND(int32_t, NPY_INT)
#else
TYPEMAP_INND(int32_t, NPY_INT32)
#endif
TYPEMAP_INND(uint32_t, NPY_UINT32)
TYPEMAP_INND(int64_t, NPY_INT64)
TYPEMAP_INND(uint64_t, NPY_UINT64)
Expand All @@ -900,6 +969,7 @@ TYPEMAP_INND(PyObject, NPY_OBJECT)
%define TYPEMAP_STRINGFEATURES_IN(type,typecode)
%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) shogun::SGStringList<type>
{
printf("Now in typemap stringfeat. ");
$1 = is_pystring_list($input, typecode);
}
%typemap(in) shogun::SGStringList<type>
Expand All @@ -910,11 +980,19 @@ TYPEMAP_INND(PyObject, NPY_OBJECT)
%enddef

TYPEMAP_STRINGFEATURES_IN(bool, NPY_BOOL)
#ifdef PYTHON3 // str -> unicode for python3
TYPEMAP_STRINGFEATURES_IN(char, NPY_UNICODE)
#else
TYPEMAP_STRINGFEATURES_IN(char, NPY_STRING)
#endif
TYPEMAP_STRINGFEATURES_IN(uint8_t, NPY_UINT8)
TYPEMAP_STRINGFEATURES_IN(int16_t, NPY_INT16)
TYPEMAP_STRINGFEATURES_IN(uint16_t, NPY_UINT16)
#ifdef PYTHON3 // int -> long for python3
TYPEMAP_STRINGFEATURES_IN(int32_t, NPY_INT)
#else
TYPEMAP_STRINGFEATURES_IN(int32_t, NPY_INT32)
#endif
TYPEMAP_STRINGFEATURES_IN(uint32_t, NPY_UINT32)
TYPEMAP_STRINGFEATURES_IN(int64_t, NPY_INT64)
TYPEMAP_STRINGFEATURES_IN(uint64_t, NPY_UINT64)
Expand All @@ -935,11 +1013,19 @@ TYPEMAP_STRINGFEATURES_IN(PyObject, NPY_OBJECT)
%enddef

TYPEMAP_STRINGFEATURES_OUT(bool, NPY_BOOL)
#ifdef PYTHON3 // str -> unicode for python3
TYPEMAP_STRINGFEATURES_OUT(char, NPY_UNICODE)
#else
TYPEMAP_STRINGFEATURES_OUT(char, NPY_STRING)
#endif
TYPEMAP_STRINGFEATURES_OUT(uint8_t, NPY_UINT8)
TYPEMAP_STRINGFEATURES_OUT(int16_t, NPY_INT16)
TYPEMAP_STRINGFEATURES_OUT(uint16_t, NPY_UINT16)
#ifdef PYTHON3 // int -> long for python3
TYPEMAP_STRINGFEATURES_OUT(int32_t, NPY_INT)
#else
TYPEMAP_STRINGFEATURES_OUT(int32_t, NPY_INT32)
#endif
TYPEMAP_STRINGFEATURES_OUT(uint32_t, NPY_UINT32)
TYPEMAP_STRINGFEATURES_OUT(int64_t, NPY_INT64)
TYPEMAP_STRINGFEATURES_OUT(uint64_t, NPY_UINT64)
Expand All @@ -965,11 +1051,19 @@ TYPEMAP_STRINGFEATURES_OUT(PyObject, NPY_OBJECT)
%enddef

TYPEMAP_SPARSEFEATURES_IN(bool, NPY_BOOL)
#ifdef PYTHON3 // str -> unicode for python3
TYPEMAP_SPARSEFEATURES_IN(char, NPY_UNICODE)
#else
TYPEMAP_SPARSEFEATURES_IN(char, NPY_STRING)
#endif
TYPEMAP_SPARSEFEATURES_IN(uint8_t, NPY_UINT8)
TYPEMAP_SPARSEFEATURES_IN(int16_t, NPY_INT16)
TYPEMAP_SPARSEFEATURES_IN(uint16_t, NPY_UINT16)
#ifdef PYTHON3 // int -> long for python3
TYPEMAP_SPARSEFEATURES_IN(int32_t, NPY_INT)
#else
TYPEMAP_SPARSEFEATURES_IN(int32_t, NPY_INT32)
#endif
TYPEMAP_SPARSEFEATURES_IN(uint32_t, NPY_UINT32)
TYPEMAP_SPARSEFEATURES_IN(int64_t, NPY_INT64)
TYPEMAP_SPARSEFEATURES_IN(uint64_t, NPY_UINT64)
Expand All @@ -995,11 +1089,19 @@ TYPEMAP_SPARSEFEATURES_IN(PyObject, NPY_OBJECT)
%enddef

TYPEMAP_SPARSEFEATURES_OUT(bool, NPY_BOOL)
#ifdef PYTHON3 // str -> unicode for python3
TYPEMAP_SPARSEFEATURES_OUT(char, NPY_UNICODE)
#else
TYPEMAP_SPARSEFEATURES_OUT(char, NPY_STRING)
#endif
TYPEMAP_SPARSEFEATURES_OUT(uint8_t, NPY_UINT8)
TYPEMAP_SPARSEFEATURES_OUT(int16_t, NPY_INT16)
TYPEMAP_SPARSEFEATURES_OUT(uint16_t, NPY_UINT16)
#ifdef PYTHON3 // int -> long for python3
TYPEMAP_SPARSEFEATURES_OUT(int32_t, NPY_INT)
#else
TYPEMAP_SPARSEFEATURES_OUT(int32_t, NPY_INT32)
#endif
TYPEMAP_SPARSEFEATURES_OUT(uint32_t, NPY_UINT32)
TYPEMAP_SPARSEFEATURES_OUT(int64_t, NPY_INT64)
TYPEMAP_SPARSEFEATURES_OUT(uint64_t, NPY_UINT64)
Expand Down

0 comments on commit 355cb51

Please sign in to comment.