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;
54 /** \@file python/except.i
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();
88 my ($class, $parent, $comment) = split /\t/, $_, 3;
89 print FD "%exceptionclass $class;\n";
94 %include "xapian/error.h"
98 SWIGEXPORT void SetPythonException() {
101 } catch (Swig::DirectorException &) {
102 /* This happens if a director raised an exception. The standard SWIG
103 * director exception handling code sets the Python error state if
104 * necessary, so we don't need to do anything. */
107 for (reverse @allclasses) {
108 my ($class, $parent, $comment) = split /\t/, $_, 3;
110 } catch (const Xapian::$class &e) {
111 SWIG_Python_Raise(SWIG_NewPointerObj((new Xapian::$class(e)),
112 SWIGTYPE_p_Xapian__$class,
115 SWIGTYPE_p_Xapian__$class);
120 } catch (const Xapian::Error &e) {
121 SWIG_Python_Raise(SWIG_NewPointerObj((new Xapian::Error(e)),
122 SWIGTYPE_p_Xapian__Error,
125 SWIGTYPE_p_Xapian__Error);
126 } catch (const std::exception& e) {
127 SWIG_Error(SWIG_RuntimeError, e.what());
129 SWIG_Error(SWIG_UnknownError, "unknown error in Xapian");
135 /* Functions and methods which are marked as "nothrow": */
139 exception_data::for_each_nothrow(sub { print FD "%exception $_[0];\n" });
147 Xapian::SetPythonException();
152 /* If a Python error is raised by a call to a director function, the following
153 * code should cause a C++ exception to be thrown.
155 %feature("director:except") {
156 if ($error != NULL) {
157 throw Swig::DirectorMethodException();
161 /* vim:syntax=cpp:set noexpandtab: */