トップ 差分 一覧 ソース 検索 ヘルプ RSS ログイン

BugTrack-その他のメモ/10

R備忘録 /状態空間モデリング/donlp2/その他のメモ

Python + SWIG

  • 投稿者: みゅ
  • カテゴリ: Python
  • 優先度: 普通
  • 状態: 完了
  • 日時: 2010年01月01日 17時39分17秒

内容

  • メモ.だけど以下のページのまったくの書き写しです.ごめんなさい.

Python/Swig

  • 管理人様、ありがとうございます.
  • そのうち、独自の情報も追加予定・・・
  • C のソース
/* hello.c */
int print_hello()
{
printf("hello\n");
return 0;
}
  • hello.i
/* hello.i */
%module hello
%{
/* Put header files here (optional) */
extern int print_hello();
%}
extern int print_hello();
  • コマンド その1
$ swig -python hello.i
  • hello.pyとhello_wrap.cができる
    • hello_wrap.cがラッパーなんでしょうね?
  • hello.pyはこんな感じ
$ vi hello.py
# This file was automatically generated by SWIG (http://www.swig.org).
# Version 1.3.36
#
# Don't modify this file, modify the SWIG interface instead.
# This file is compatible with both classic and new-style classes.

import _hello
import new
new_instancemethod = new.instancemethod
try:
    _swig_property = property
except NameError:
    pass # Python < 2.2 doesn't have 'property'.
def _swig_setattr_nondynamic(self,class_type,name,value,static=1):
・・・(続く)
  • コマンド その2
$ gcc -c hello.c hello_wrap.c -I /usr/include/python2.5
hello.c: In function ‘print_hello’:
hello.c:3: 警告: incompatible implicit declaration of built-in function ‘printf’
$ gcc -shared hello.o hello_wrap.o -o _hello.so
/usr/lib/gcc/i486-pc-linux-gnu/4.3.2/../../../../i486-pc-linux-gnu/bin/ld: warni
ng: creating a DT_TEXTREL in object.
  • 以下のようになる
$ ls -la
合計 184
drwxr-xr-x 2 admin admin  4096 2010-01-01 17:30 .
drwxr-xr-x 8 admin admin  4096 2010-01-01 17:29 ..
-rwxr-xr-x 1 admin admin 34916 2010-01-01 17:30 _hello.so
-rw-r--r-- 1 admin admin    56 2010-01-01 17:28 hello.c
-rw-r--r-- 1 admin admin   126 2010-01-01 17:28 hello.i
-rw-r--r-- 1 admin admin   864 2010-01-01 17:30 hello.o
-rw-r--r-- 1 admin admin  1609 2010-01-01 17:29 hello.py
-rw-r--r-- 1 admin admin 91083 2010-01-01 17:29 hello_wrap.c
-rw-r--r-- 1 admin admin 27872 2010-01-01 17:30 hello_wrap.o
  • コマンド その3
$ python
Python 2.5.4 (r254:67916, Jun  8 2009, 13:35:50)
[GCC 4.1.2 (Gentoo 4.1.2 p1.3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
>>> hello.print_hello()
hello
0
>>>

SWIGでPythonラッパを書いてみるの例

  • (管理人様ありがとうございます.)
  • example.c
#include <stdio.h>
void print_hoge(int n)
{
  int i;
  for (i = 0; i < n; i++) {
    printf("hoge\n");
  }
}

void print_hoge2(char *c){
        printf("%s\n", c);
}
  • example.i
%module example
%{
/* Write include header (Optional) */
/* #include "example.h" */
/*extern void print_hoge (int n);*/
%}

extern void print_hoge (int n);
extern void print_hoge2(char *c);
  • コマンド1
swig -python example.i
  • コマンド2
gcc -c example.c example_wrap.c -I /usr/include/python2.5
  • コマンド3
gcc -shared example.o example_wrap.o -o _example.so
  • 結果
python
>>> import example
>>> example.print_hoge(5)
hoge
hoge
hoge
hoge
hoge
>>> example.print_hoge2('test')
test

作成したモジュールを使うために

  • PYTHONPATHを設定する
PYTHONPATH='/home/admin/swig_test' python
  • <取り消し線>ということは、mod_pythonで使えるようにするには、apacheの起動スクリプトにPYTHONPATHを設定するか、PYTHONPATH変数を指定して、apacheを起動するとよいのかも.</取り消し線>

リンク

コメント