From adf66c8c6200c9222f81b3dbfa7928b6bc5308a7 Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Fri, 28 May 2004 02:10:02 +0000 Subject: [PATCH] fix anon enums git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5935 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Examples/test-suite/cpp_enum.i | 2 ++ .../test-suite/python/cpp_enum_runme.py | 5 +++++ SWIG/Lib/python/pyenum.swg | 21 +++++++++++++++++++ SWIG/Lib/python/python.swg | 9 +------- 4 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 SWIG/Lib/python/pyenum.swg diff --git a/SWIG/Examples/test-suite/cpp_enum.i b/SWIG/Examples/test-suite/cpp_enum.i index 72ee6e6a2..205c25c44 100644 --- a/SWIG/Examples/test-suite/cpp_enum.i +++ b/SWIG/Examples/test-suite/cpp_enum.i @@ -35,4 +35,6 @@ struct StructWithEnums { } }; + enum {Hi, Hello } hi; + %} diff --git a/SWIG/Examples/test-suite/python/cpp_enum_runme.py b/SWIG/Examples/test-suite/python/cpp_enum_runme.py index 347d9237e..0054ae281 100644 --- a/SWIG/Examples/test-suite/python/cpp_enum_runme.py +++ b/SWIG/Examples/test-suite/python/cpp_enum_runme.py @@ -16,3 +16,8 @@ f.hola = f.Hello if f.hola != f.Hello: print f.hola raise RuntimeError + +cpp_enum.cvar.hi = cpp_enum.Hello +if cpp_enum.cvar.hi != cpp_enum.Hello: + print cpp_enum.cvar.hi + raise RuntimeError diff --git a/SWIG/Lib/python/pyenum.swg b/SWIG/Lib/python/pyenum.swg new file mode 100644 index 000000000..f90366e23 --- /dev/null +++ b/SWIG/Lib/python/pyenum.swg @@ -0,0 +1,21 @@ +/* ------------------------------------------------------------ + * Enums + * ------------------------------------------------------------ */ +%apply int { enum SWIGTYPE }; + +%typemap(varin,fragment=SWIG_AsVal_frag(int)) enum SWIGTYPE +{ + if (sizeof(int) != sizeof($1)) { + PyErr_SetString(PyExc_TypeError, "enum variable '$name' can not be set"); + return 1; + } + if (!SWIG_AsVal(int)($input, (int*)(void*)&($1))) { + return 1; + } +} + + +/* this doesn't work now, you need to redefined it for each enum. */ +%apply const int& { const enum SWIGTYPE& }; + + diff --git a/SWIG/Lib/python/python.swg b/SWIG/Lib/python/python.swg index 93e1ea7d5..0ab994252 100644 --- a/SWIG/Lib/python/python.swg +++ b/SWIG/Lib/python/python.swg @@ -41,14 +41,7 @@ %include "pyptrtypes.swg" %include "pyprimtypes.swg" %include "pymisctypes.swg" - -/* ------------------------------------------------------------ - * Enums - * ------------------------------------------------------------ */ -%apply int { enum SWIGTYPE }; -/* this doesn't work now, you need to redefined it for each enum. */ -%apply const int& { const enum SWIGTYPE& }; - +%include "pyenum.swg" /* ------------------------------------------------------------ * Overloaded operator support