1 # generate-python-exceptions: generate error handling code for Python bindings
2 my $copyright = <<'END';
3 Copyright (C) 2003,2004,2006,2007,2008,2009,2012 Olly Betts
4 Copyright (C) 2007 Lemur Consulting Ltd
5 Copyright (C) 2007 Richard Boulton
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 my @allclasses = (@baseclasses, @classes);
27 my @posting_source_virtual_methods = qw(
43 my @matchspy_virtual_methods = qw(
49 open FD, ">except.i" or die $!;
51 $copyright =~ s/^/ */mg;
55 * \@brief Custom Python exception handling.
57 /* Warning: This file is generated by $0
58 * - do not modify directly!
67 %exceptionclass Error;
68 %ignore Error::get_description;
70 std::string __str__() const {
71 std::string desc($self->get_msg());
72 if (!$self->get_context().empty()) {
73 desc += " (context: ";
74 desc += $self->get_context();
77 if ($self->get_error_string()) {
79 desc += $self->get_error_string();
89 print FD "%exceptionclass $class;\n";
94 %ignore Xapian::Error::Error(const Error&);
95 %include "xapian/error.h"
99 SWIGEXPORT void SetPythonException() {
102 } catch (Swig::DirectorException &) {
103 /* This happens if a director raised an exception. The standard SWIG
104 * director exception handling code sets the Python error state if
105 * necessary, so we don't need to do anything. */
108 for (reverse @allclasses) {
111 } catch (const Xapian::$class &e) {
112 SWIG_Python_Raise(SWIG_NewPointerObj((new Xapian::$class(e)),
113 SWIGTYPE_p_Xapian__$class,
116 SWIGTYPE_p_Xapian__$class);
121 } catch (const Xapian::Error &e) {
122 SWIG_Python_Raise(SWIG_NewPointerObj((new Xapian::Error(e)),
123 SWIGTYPE_p_Xapian__Error,
126 SWIGTYPE_p_Xapian__Error);
127 } catch (const std::exception& e) {
128 SWIG_Error(SWIG_RuntimeError, e.what());
130 SWIG_Error(SWIG_UnknownError, "unknown error in Xapian");
136 /* Functions and methods which are marked as "nothrow": */
140 exception_data::for_each_nothrow(sub { print FD "%exception $_[0];\n" });
148 Xapian::SetPythonException();
153 /* If a Python error is raised by a call to a director function, the following
154 * code should cause a C++ exception to be thrown.
156 %feature("director:except") {
157 if ($error != NULL) {
158 throw Swig::DirectorMethodException();
162 /* vim:syntax=cpp:set noexpandtab: */