embedding & extending [SWIG,newbie]

Tino Roller rollert at t-online.de
Sat Feb 19 18:14:16 EST 2000


I have actually started to do some trials with extending/embedding
python and would like to use python as script language in a project
written in C.
Can somebody help me out

1) small simmple C programme "testit.c"

    #include "Python.h"

    #include <stdio.h>

    int main () {
        printf ("Initializing python...\n");
        Py_Initialize ();

        PyRun_SimpleString ("print \"Hello\"");
        PyRun_SimpleString ("import sys");
        PyRun_SimpleString ("sys.path.append
(\"/home/rollert/test/python\")");

        printf ("python: importing module ptest...\n");

        PyRun_SimpleString ("import ptest");
        PyRun_SimpleString ("print dir(ptest)");

        printf ("Finalizing python\n");
        Py_Finalize ();

        return 0;
    };

    make:
        gcc testit.c -o testit -I/usr/include/python1.5
-L/usr/lib/python1.5/config/lib -lpython1.5 -ldl -lpthread



2)  extension module using SWIG:
    a) SWIG interface file   ptest.i

             %module ptest
            %{
            #include "ptest.h"
            %}

            int func1 (int);
            double foo;

    b)  ptest.h
            #ifndef __TEST_H__
            #define __TEST_H__

            int func1 (int n);

            double foo;

            #endif

    c) ptest.c
            #include "ptest.h"

            int func1 (int n)
            {
              return n*n;
            }

            double foo = 3;

    make:
         swig -python  ptest.i
         gcc -shared -I/usr/include/python1.5  ptest.c  ptest_wrap.c
-o ptestmodule.so


Importing module ptest in Python works, calling testit gives error
message
    Traceback (innermost last):
      File "<string>", line 1, in ?
    ImportError: /home/rollert/c/kreport/kreport/python/ptestmodule.so:
undefined symbol: PyString_FromString

Linking the module with  libpython1.5.a results in:
    Initializing python...
    Hello
    python: importing module ptest...
    Fatal Python error: PyThreadState_Get: no current thread
    <cancelled>

Thanks,
Tino











More information about the Python-list mailing list