Skip to content

Commit

Permalink
fixed compile error, buffer stuff now is in typemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
gsomix committed Jul 14, 2012
1 parent 3523a55 commit ccb9c80
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/interfaces/modular/Features.i
Expand Up @@ -7,7 +7,7 @@
* Written (W) 2009 Soeren Sonnenburg
* Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/

#ifdef HAVE_PYTHON
%feature("autodoc", "get_str(self) -> numpy 1dim array of str\n\nUse this instead of get_string() which is not nicely wrapped") get_str;
%feature("autodoc", "get_hist(self) -> numpy 1dim array of int") get_hist;
Expand Down
3 changes: 0 additions & 3 deletions src/interfaces/modular/modshogun.i
Expand Up @@ -28,9 +28,6 @@
static int print_sgobject(PyObject *pyobj, FILE *f, int flags);
%}

/* Include helper functions for python buffer protocol */
%include "python_buffer_protocol.i"

%feature("python:slot", "tp_str", functype="reprfunc") shogun::CSGObject::__str__;
%feature("python:slot", "tp_repr", functype="reprfunc") shogun::CSGObject::__repr__;
/*%feature("python:slot", "tp_hash", functype="hashfunc") shogun::CSGObject::myHashFunc;*/
Expand Down
78 changes: 0 additions & 78 deletions src/interfaces/python_modular/python_buffer_protocol.i

This file was deleted.

78 changes: 78 additions & 0 deletions src/interfaces/python_modular/swig_typemaps.i
Expand Up @@ -770,6 +770,84 @@ static bool spvector_to_numpy(PyObject* &obj, SGSparseVector<type> sg_vector, in

%}

/* Buffer protocol stuff for DenseFeatures */
%define BUFFER_DENSEFEATURES(class_name, type_name, type, format_str)

%wrapper
%{

static int class_name ## _getbuffer(PyObject *exporter, Py_buffer *view, int flags)
{
CDenseFeatures< type > * self = (CDenseFeatures< type > *) 0;
void *argp1 = 0 ;
int res1 = 0 ;

int num_feat = 0, num_vec = 0;
Py_ssize_t* shape;
Py_ssize_t* stride;

static char* format = (char *) format_str;

res1 = SWIG_ConvertPtr(exporter, &argp1, SWIG_TypeQuery("shogun::CDenseFeatures<type>"), 0 | 0 );
if (!SWIG_IsOK(res1))
{
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" " class_name _getbuffer" "', argument " "1"" of type '" "CDenseFeatures< type > *""'");
}

self = reinterpret_cast< CDenseFeatures < type > * >(argp1);

view->buf = self->get_feature_matrix(num_feat, num_vec);

shape = new Py_ssize_t[2];
shape[0] = num_feat;
shape[1] = num_vec;

stride = new Py_ssize_t[2];
stride[0] = sizeof( type );
stride[1] = sizeof( type ) * num_feat;

view->len = shape[0]*stride[0];
view->itemsize = stride[0];
view->readonly = 0;
view->format = format;
view->ndim = 2;
view->shape = shape;
view->strides = stride;
view->suboffsets = NULL;
view->internal = NULL;

view->obj = (PyObject*) exporter;
Py_INCREF(exporter);

return 0;

fail:
return -1;
}

static void class_name ## _releasebuffer(PyObject *exporter, Py_buffer *view)
{
if(view->shape != NULL)
delete[] view->shape;

if(view->strides != NULL)
delete[] view->strides;
}

static long class_name ## _flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE;
%}

%init
%{
/* TODO less "hacked" */
SwigPyBuiltin__shogun__CDenseFeaturesT_ ## type_name ## _t_type.ht_type.tp_flags = class_name ## _flags;
%}

%feature("python:bf_getbuffer") CDenseFeatures< type_name > #class_name "_getbuffer"
%feature("python:bf_releasebuffer") CDenseFeatures< type_name > #class_name "_releasebuffer"

%enddef

/* CFeatures to ... */
%define FEATURES_BY_TYPECODE(obj, f, type, typecode)
switch (typecode) {
Expand Down

0 comments on commit ccb9c80

Please sign in to comment.