diff --git a/SWIG/Examples/test-suite/python/lib_std_pair_runme.py b/SWIG/Examples/test-suite/python/lib_std_pair_runme.py index fac19df11..da156fb90 100644 --- a/SWIG/Examples/test-suite/python/lib_std_pair_runme.py +++ b/SWIG/Examples/test-suite/python/lib_std_pair_runme.py @@ -38,3 +38,4 @@ if pab.first.val != 2: pci = lib_std_pair.CIntPair(1,0) +print len(psii) diff --git a/SWIG/Lib/python/std_pair.i b/SWIG/Lib/python/std_pair.i index 41344a22c..bc2b14183 100644 --- a/SWIG/Lib/python/std_pair.i +++ b/SWIG/Lib/python/std_pair.i @@ -4,6 +4,28 @@ #include %} + +%define %pypair_methods(pair) +%extend { +%pythoncode { +def __len__(self): + return 2 +def __getitem__(self, index): + if not (index % 2): + return self.first + else: + return self.second +def __setitem__(self, index, val): + if not (index % 2): + self.first = val + else: + self.second = val +def __repr__(self): + return str((self.first, self.second)) +} +} +%enddef + %fragment("StdPairTraits","header", fragment="StdTraits",fragment="PyObject_var") { namespace swigpy { @@ -85,13 +107,7 @@ namespace std { T first; U second; - %extend - { - %pythoncode { - def __repr__(self): - return "(%s, %s)" %(str(self.first),str(self.second)) - } - } + %pypair_methods(pair) }; // *** @@ -125,6 +141,8 @@ namespace std { T first; U* second; + + %pypair_methods(pair) }; template struct pair { @@ -154,6 +172,8 @@ namespace std { T* first; U second; + + %pypair_methods(pair) }; template struct pair { @@ -180,6 +200,8 @@ namespace std { T* first; U* second; + + %pypair_methods(pair) }; }