summaryrefslogtreecommitdiff
path: root/Examples/test-suite/python_builtin.i
blob: e2c453d5456e3e8b6dd0de5a1eec3f5a14108f51 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Test customizing slots when using the -builtin option

%module python_builtin

%inline %{
#ifdef SWIGPYTHON_BUILTIN
bool is_python_builtin() { return true; }
#else
bool is_python_builtin() { return false; }
#endif
%}

// Test 0 for default tp_hash
%inline %{
struct ValueStruct {
  int value;
  ValueStruct(int value) : value(value) {}
  static ValueStruct *inout(ValueStruct *v) {
    return v;
  }
};
%}

// Test 1 for tp_hash
#if defined(SWIGPYTHON_BUILTIN)
%feature("python:tp_hash") SimpleValue "SimpleValueHashFunction"
#endif

%inline %{
struct SimpleValue {
  int value;
  SimpleValue(int value) : value(value) {}
};
%}

%{
#if PY_VERSION_HEX >= 0x03020000
Py_hash_t SimpleValueHashFunction(PyObject *v)
#else
long SimpleValueHashFunction(PyObject *v)
#endif
{
  SwigPyObject *sobj = (SwigPyObject *) v;
  SimpleValue *p = (SimpleValue *)sobj->ptr;
  return p->value;
}
hashfunc test_hashfunc_cast() {
    return SimpleValueHashFunction;
}
%}

// Test 2 for tp_hash
#if defined(SWIGPYTHON_BUILTIN)
%feature("python:slot", "tp_hash", functype="hashfunc") BadHashFunctionReturnType::bad_hash_function;
#endif

%inline %{
struct BadHashFunctionReturnType {
    static const char * bad_hash_function() {
      return "bad hash function";
    }
};
%}

// Test 3 for tp_hash
#if defined(SWIGPYTHON_BUILTIN)
%feature("python:slot", "tp_hash", functype="hashfunc") ExceptionHashFunction::exception_hash_function;
#endif

%catches(const char *) exception_hash_function;

%inline %{
#if PY_VERSION_HEX < 0x03020000
  #define Py_hash_t long
#endif
struct ExceptionHashFunction {
    static Py_hash_t exception_hash_function() {
      throw "oops";
    }
};
%}

// Test 4 for tp_dealloc (which is handled differently to other slots in the SWIG source)
#if defined(SWIGPYTHON_BUILTIN)
%feature("python:tp_dealloc") Dealloc1 "Dealloc1Destroyer"
%feature("python:tp_dealloc") Dealloc2 "Dealloc2Destroyer"
%feature("python:slot", "tp_dealloc", functype="destructor") Dealloc3::Destroyer;
#endif

%inline %{
static int Dealloc1CalledCount = 0;
static int Dealloc2CalledCount = 0;
static int Dealloc3CalledCount = 0;

struct Dealloc1 {
};
struct Dealloc2 {
  ~Dealloc2() {}
};
struct Dealloc3 {
  void Destroyer() {
    Dealloc3CalledCount++;
    delete this;
  }
};
%}

%{
void Dealloc1Destroyer(PyObject *v) {
  SwigPyObject *sobj = (SwigPyObject *) v;
  Dealloc1 *p = (Dealloc1 *)sobj->ptr;
  delete p;
  Dealloc1CalledCount++;
}
void Dealloc2Destroyer(PyObject *v) {
  SwigPyObject *sobj = (SwigPyObject *) v;
  Dealloc2 *p = (Dealloc2 *)sobj->ptr;
  delete p;
  Dealloc2CalledCount++;
}
%}

// Test 5 for python:compare feature
%feature("python:compare", "Py_LT") MyClass::lessThan;

%inline %{
  class MyClass {
  public:
    MyClass(int val = 0) : val(val) {}
    bool lessThan(const MyClass& other) const {
      less_than_counts++;
      return val < other.val;
    }
    int val;
    static int less_than_counts;
  };
  int MyClass::less_than_counts = 0;
%}