OILS / demo / swig.sh View on Github | oilshell.org

61 lines, 21 significant
1#!/usr/bin/env bash
2#
3# Can we use SWIG to wrap pure C++ in Python extension modules?
4#
5# This might be hard for our garbage collected types.
6#
7# Usage:
8# demo/swig.sh <function name>
9
10set -o nounset
11set -o pipefail
12set -o errexit
13
14install() {
15 sudo apt-get install swig
16}
17
18# https://www.swig.org/Doc3.0/Python.html#Python
19
20example() {
21 ### Translate, compile, and run
22
23 swig -python -c++ demo/swig/example.i
24
25 # Two Output Files
26 wc -l demo/swig/{example.py,example_wrap.*}
27 ls -l demo/swig/
28
29 set -x
30 python2 demo/swig/setup.py build_ext --inplace
31
32 python2 -c '
33import _example
34print(dir(_example))
35print("fact(5) = %d" % _example.fact(5))
36print("add(3, 4) = %d" % _example.add(3, 4))
37print(_example.send)
38
39# TODO: convert Str* to Python
40# print(_example.send(42, "my payload"))
41
42# Generates TypeError for you
43# print("add(x, 4) = %d" % _example.add("3", 4))
44'
45
46}
47
48clean-temp() {
49 # Don't check in generated files
50 rm -v demo/swig/{example.py,example_wrap.cxx}
51}
52
53# Hm doesn't work?
54qsn() {
55 #swig -o _tmp/qsn -python cpp/qsn.i
56 swig -python -c++ cpp/qsn.i
57 ls -l cpp/qsn*
58 #ls -l _tmp/qsn
59}
60
61"$@"