#! /usr/bin/bash

wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz

tar -xzvf Python-2.7.8.tgz
cd Python-2.7.8
./configure --with-system-ffi
make
cd ..

cat << EOF > ./test.c
#include <stdbool.h>

typedef union {
    const char *a;
    const char *b;
    const char *c;
    const char *d;
    const char *e;
    const char *f;
    bool g;
    bool h;
    bool i;
} test_t;

test_t f(void);

test_t
f(void) {
  test_t example = {.g = true };
  return example;
}
EOF
cat << EOF > ./test.py
#! /usr/bin/python2

from ctypes import *

test = cdll.LoadLibrary("./test.so")

class TestT(Union):
    _fields_ = [("a", c_char_p),
                ("b", c_char_p),
                ("c", c_char_p),
                ("d", c_char_p),
                ("e", c_char_p),
                ("f", c_char_p),
                ("g", c_bool),
                ("h", c_bool),
                ("i", c_bool)]

test.f.restype = TestT
r = test.f()
print r.g
EOF


gcc -c -fPIC -std=c99 -O0 ./test.c -o test.o
gcc -shared -o test.so test.o

./Python-2.7.8/python test.py
