[C++-sig] Converting std::out_of_range to Python IndexError

Raoul Gough RaoulGough at yahoo.co.uk
Mon Sep 8 20:32:58 CEST 2003


In working on the indexing suite, I came across a std::out_of_range
exception (thrown by std::vector<>::at) and wanted it to arrive in
Python as an IndexError - this functions similarly to the
StopIteration exception for sequences with __getitem__ rather than
__iter__. Anyway, adding it to libs/python/src/errors.cpp is trivial
(diff follows this paragraph). I also added an explicit include for
<stdexcept>, since this header is the one that defines the standard
exception hierarchy. OK to commit these changes?

Index: errors.cpp
===================================================================
RCS file: /cvsroot/boost/boost/libs/python/src/errors.cpp,v
retrieving revision 1.9
diff -w -d -u -r1.9 errors.cpp
--- errors.cpp	23 Jul 2003 03:00:47 -0000	1.9
+++ errors.cpp	8 Sep 2003 18:24:38 -0000
@@ -11,6 +11,7 @@
 #include <boost/python/errors.hpp>
 #include <boost/cast.hpp>
 #include <boost/python/detail/exception_handler.hpp>
+#include <stdexcept>
 
 namespace boost { namespace python {
 
@@ -37,6 +38,10 @@
     catch(const bad_numeric_cast& x)
     {
         PyErr_SetString(PyExc_OverflowError, x.what());
+    }
+    catch(const std::out_of_range& x)
+    {
+        PyErr_SetString(PyExc_IndexError, x.what());
     }
     catch(const std::exception& x)
     {


-- 
Raoul Gough.
(setq dabbrev-case-fold-search nil)





More information about the Cplusplus-sig mailing list