File: swig2_example.py

package info (click to toggle)
python-scipy 0.18.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 75,464 kB
  • ctags: 79,406
  • sloc: python: 143,495; cpp: 89,357; fortran: 81,650; ansic: 79,778; makefile: 364; sh: 265
file content (55 lines) | stat: -rw-r--r-- 1,596 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""Simple example to show how to use weave.inline on SWIG2 wrapped
objects.  SWIG2 refers to SWIG versions >= 1.3.

To run this example you must build the trivial SWIG2 extension called
swig2_ext.  To do this you need to do something like this::

 $ swig -c++ -python -I. -o swig2_ext_wrap.cxx swig2_ext.i

 $ g++ -Wall -O2 -I/usr/include/python2.3 -fPIC -I. -c \
   -o swig2_ext_wrap.os swig2_ext_wrap.cxx

 $ g++ -shared -o _swig2_ext.so swig2_ext_wrap.os \
   -L/usr/lib/python2.3/config

The files swig2_ext.i and swig2_ext.h are included in the same
directory that contains this file.

Note that weave's SWIG2 support works fine whether SWIG_COBJECT_TYPES
are used or not.

Author: Prabhu Ramachandran
Copyright (c) 2004, Prabhu Ramachandran
License: BSD Style.

"""
from __future__ import absolute_import, print_function

# Import our SWIG2 wrapped library
import swig2_ext

import scipy.weave as weave
from scipy.weave import swig2_spec, converters

# SWIG2 support is not enabled by default.  We do this by adding the
# swig2 converter to the default list of converters.
converters.default.insert(0, swig2_spec.swig2_converter())


def test():
    """Instantiate the SWIG wrapped object and then call its method
    from C++ using weave.inline

    """
    a = swig2_ext.A()
    b = swig2_ext.foo()  # This will be an APtr instance.
    b.thisown = 1  # Prevent memory leaks.
    code = """a->f();
              b->f();
              """
    weave.inline(code, ['a', 'b'], include_dirs=['.'],
                 headers=['"swig2_ext.h"'], verbose=1)


if __name__ == "__main__":
    test()