Python Interface mit SIP



  • Hi,
    ich wollte gerne eine python Interface für eine Library erstellen.
    Allerdings kenne ich mich mit python nicht sonderlich gut aus und die Dokumentation von SIP ist entweder unvollständig
    oder falsch oder zeigt auf tote Seiten..

    %Module ANN
    
    namespace ANN {
    	class SOMNet {
    
    	%TypeHeaderCode
    	#include <ANSOMNet.h>
    	%End
    
    	public:
    		SOMNet(const std::vector<unsigned int> &vDimI, const std::vector<unsigned int> &vDimO);
    
    //..
    	};
    };
    

    Mein erstes Problem tritt schon beim Konstruktor auf:

    dgrat@linux-6pdl:~/annetgpgpu/ANNet/include> sip -c . ANSOMNet.sip
    sip: ANN::SOMNet unsupported ctor argument type - provide %MethodCode and a C++ signature
    

    Ich vermutete, dass es an der fehleneden Definition von std::vector liegt.
    Also habe ich nach einer *.sip zum Mappen von std::vector gesucht:

    /* -*- mode:c++ -*- */
    
    /** @file
    This is the SIP interface definition for std::vector
    
    Copyright (C)  2002-2004  The Board of Trustees of  
    The Leland Stanford Junior University.  All Rights Reserved.
    
    $Id: vector.sip,v 1.10 2005/06/17 18:08:00 pfkeb Exp $
    
    @author Paul_Kunz@slac.stanford.edu
    */
    
    %MappedType std::vector<double> 
    {
    %TypeHeaderCode
    #include <vector>
    %End
    
    %ConvertFromTypeCode
    	// Handle no list.
    
    	if (!sipCpp)
    		return PyList_New(0);
    
    	// Convert to a Python list of doubles
    
    	PyObject *l;
    
    	// Create the list.
            int size = static_cast < int > ( sipCpp -> size () );
    	if ( ( l = PyList_New ( size ) ) == NULL)
    		return NULL;
    
    	// Get it.
    
    	for ( unsigned int i = 0; i < sipCpp -> size(); ++i)
              if (PyList_SetItem(l,i,PyFloat_FromDouble((double)(*sipCpp)[i])) < 0)
    		{
    			Py_DECREF(l);
    
    			return NULL;
    		}
    
    	return l;
    %End
    
    %ConvertToTypeCode
    	// Convert a Python list of double to a vector<double> on the heap.
    
    	if (sipIsErr == NULL)
    		return PyList_Check(sipPy);
    
    	if (sipPy == Py_None)
    	{
    		*sipCppPtr = NULL;
    
    		return 0;
    	}
            int size = PyList_GET_SIZE(sipPy);	
            vector<double> *qvl = new vector<double> ( size );
    	PyErr_Clear();
    
    	for (int i = 0; i < size; ++i)
    	{
    	  qvl -> operator [] (i) 
    	       = ((double)PyFloat_AsDouble(PyList_GET_ITEM(sipPy,i)));
    
    		if (PyErr_Occurred() != NULL)
    		{
    			delete qvl;
    			*sipIsErr = 1;
    
    			return 0;
    		}
    	}
    
    	*sipCppPtr = qvl;
    
    	return 1;
    %End
    };
    //..
    

    Allerdings kann ich das mittels:

    dgrat@linux-6pdl:~/annetgpgpu/ANNet/include> sip -c . vector.sip
    sip: vector.sip:14: Scoped names are not allowed in a C module
    

    nichtmal übersetzen und die Suche nach dem Fehler führt nur zur SIP-Parserimplementation. Ich finde SIP äußerst frustrierend,
    weil man nichts findet 😡 Hat hier einer etwas mehr Erfahrung, wie man ein Python-Interface mit std::vector-Argumenten erstellen kann?
    Idealerweise, sollte später unter Python eine normale Liste übergeben werden können.



  • Ich denke ich steige auf SWIG um, scheint solider zu sein. Sowohl was DOKU als auch den Parser und STL support angeht. Kann SIP gar nicht empfehlen.


Anmelden zum Antworten