I try to extend Python using a simple C wrapped module . This module includes embedded python code and it is product of Swig . The corresponding files follows :
Expand|Select|Wrap|Line Numbers
- //main.c
- #include "Python.h"
- #include <stdio.h>
- int main()
- {
- Py_Initialize();
- PyRun_SimpleString("execfile('simple.py') \n");
- Py_Finalize();
- return 0;
- }
Expand|Select|Wrap|Line Numbers
- //simple.py
- print " Hello, everyone"
Expand|Select|Wrap|Line Numbers
- /* example.i */
- %module example
- %{
- #include "Python.h"
- int main();
- %}
- #include "Python.h"
- int main();
Expand|Select|Wrap|Line Numbers
- //extend.py
- import _example
- _example.main()
The file main.c embeds python , which only execute the file simple.py. The example.i is the interface file , which is used from Swig to generate the desirable module _example.so . This module called from script python file, extend.py for execution.
To do those things from Linux command prompt I execute the following commands:
$ swig -python example.i
$gcc -c main.c example_wrap.c -Dmain=oldmain -I/usr/include/python2.5
$ld -shared example_wrap.o main.o -o _example.so
$python extend.py
Hello everyone
Segmentation fault (core dumped)
Why does the last message appear ? Swig supports wrapping of embedded with python , C code? Do you have any idea how to solve this problem ?I will appreciate any answers .
Thanks a lot