Markus Rechberger / Python
 
StartSeite | MarkusRechberger/ | Neues | TestSeite | ForumSeite | Teilnehmer | Kategorien | Index | Hilfe | Einstellungen | Ändern

Writing C extensions using direct python api

Alle variablen die intern in einer funktion benutzt werden müssen sofern sie nicht ins python zurückgeliefert werden wieder gelöscht werden. dies geschieht mittels PyObject? -> {Py DECREF}?(variable);

main.c
#include <Python.h>


static PyObject * main_system(PyObject *self, PyObject *args)
{
        char *command;
        if(!PyArg_ParseTuple(args,"s",&command))
                return NULL;
        fprintf(stderr,"%s\n",command);
        return Py_BuildValue("s","executed!");
}
static PyMethodDef MainMethods[]={
        {"system",main_system,METH_VARARGS,"Execute method."},
        {NULL,NULL,0,NULL}
};

PyMODINIT_FUNC initmain(void)
{
        PyObject *m;
        m=Py_InitModule("main",MainMethods);
}

setup.py:
from distutils.core import setup,Extension

module1 = Extension('main', sources=['main.c'])

#from distutils import sysconfig
#save_init_posix = sysconfig._init_posix
#def my_init_posix():
#    print 'my_init_posix: changing gcc to g++'
#    save_init_posix()
#    g = sysconfig._config_vars
#    g['CC'] = 'g++'
#   g['LDSHARED'] = 'g++ -shared'
#sysconfig._init_posix = my_init_posix

setup (name = 'main',
       version = '1.0',
       description = 'Demopackage',
       ext_modules = [module1])

Makefile:
all:
#        env CXX=g++ python setup.py build
        python setup.py build
clean:
        python setup.py clean
install:
        python setup.py install

building

make

Running

# python
Python 2.3.3 (#2, Jan 13 2004, 00:47:05) 
[GCC 3.3.3 20040110 (prerelease) (Debian)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import main
>>> print main.system("foo")
foo
executed!
>>> d=main.system("foo")
foo
>>> print d
executed!
>>> 

Writing python modules using swig C++

sample.h
#include <string>

std::string returnstring(){
   return std::string("string");
}

sample.i
%{
#include "sample.h"
%}
#include "sample.h"

Makefile
TOP        = ../..
SWIG      = swig-1.3
CXXSRCS    =
TARGET     = sample
INTERFACE  = sample.i
LIBS       = -lm
SWIG      = swig-1.3

all::
        $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)'          SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp

static::
        $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)'          SWIGOPT='$(SWIGOPT)' TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static

clean::
        $(MAKE) -f $(TOP)/Makefile python_clean
        rm -f $(TARGET).py

check: all

see swig1.3 sample directory in debian but sample.h is almost all you need

Writing python modules using swig and C
For using swig with C, you always need a Interface file (with the same name, only with i as extension)

the c File
sample.c
#include <string.h>
#include "userLibrary.h"

void somefunction(){
   ... do something here ...
}

the interface file for swig
sample.i
%module sample
%{
/* Put header files here (optional) */
#include "userLibrary.h"
%}
%include "userLibrary.h"

extern void somefunction();

make and compile and create the python library
> swig -python sample.i

> gcc -c sample.c sample_wrap.c -I/usr/include/python2.3

> ld -shared sample.o sample_wrap.o -o _sample.so -L(librarys)

at last in python
python

>>> import sample

>>> sample.somefunction()

python memory management

http://evanjones.ca/python-memory.html


StartSeite | MarkusRechberger/ | Neues | TestSeite | ForumSeite | Teilnehmer | Kategorien | Index | Hilfe | Einstellungen | Ändern
Text dieser Seite ändern (zuletzt geändert: 18. Februar 2005 14:29 (diff))
Suchbegriff: gesucht wird
im Titel
im Text