diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2016-08-16 19:39:51 +0100 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2016-08-18 07:10:54 +0100 |
commit | f778ee19dfbcbf9cca4f202a59be6ac6175cd8c6 (patch) | |
tree | caf3ae7a705571719c976e197e1f5ca8cc665560 /Examples/test-suite/python_builtin.i | |
parent | 2740812970382202fe97e52f7c881eaa71c2e7c0 (diff) | |
download | swig-f778ee19dfbcbf9cca4f202a59be6ac6175cd8c6.tar.gz |
Python builtin hashfunc closure fix
If the wrong return type in the hash function was used, an error:
SystemError: error return without exception set
was raised
Add some tests for testing builtin slots
Diffstat (limited to 'Examples/test-suite/python_builtin.i')
-rw-r--r-- | Examples/test-suite/python_builtin.i | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/Examples/test-suite/python_builtin.i b/Examples/test-suite/python_builtin.i new file mode 100644 index 000000000..977b57ec7 --- /dev/null +++ b/Examples/test-suite/python_builtin.i @@ -0,0 +1,74 @@ +// 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 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) {} + static SimpleValue *inout(SimpleValue *sv) { + return sv; + } +}; +%} + +%{ +#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"; + } +}; +%} + |