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 @directorclasses = qw(
28 MatchDecider ExpandDecider Stopper ValueRangeProcessor KeyMaker
29 StemImplementation Compactor MatchSpy FieldProcessor
32 my @posting_source_virtual_methods = qw(
48 my @matchspy_virtual_methods = qw(
54 open FD, ">except.i" or die $!;
56 $copyright =~ s/^/ */mg;
59 /** \@file python/except.i
60 * \@brief Custom Python exception handling.
62 /* Warning: This file is generated by $0
63 * - do not modify directly!
72 %exceptionclass Error;
73 %ignore Error::get_description;
75 std::string __str__() const {
76 std::string desc($self->get_msg());
77 if (!$self->get_context().empty()) {
78 desc += " (context: ";
79 desc += $self->get_context();
82 if ($self->get_error_string()) {
84 desc += $self->get_error_string();
93 my ($class, $parent, $comment) = split /\t/, $_, 3;
94 print FD "%exceptionclass $class;\n";
99 %include "xapian/error.h"
103 SWIGEXPORT void SetPythonException() {
106 } catch (Swig::DirectorException &) {
107 /* This happens if a director raised an exception. The standard SWIG
108 * director exception handling code sets the Python error state if
109 * necessary, so we don't need to do anything. */
112 for (reverse @allclasses) {
113 my ($class, $parent, $comment) = split /\t/, $_, 3;
115 } catch (const Xapian::$class &e) {
116 SWIG_Python_Raise(SWIG_NewPointerObj((new Xapian::$class(e)),
117 SWIGTYPE_p_Xapian__$class,
120 SWIGTYPE_p_Xapian__$class);
125 } catch (const Xapian::Error &e) {
126 SWIG_Python_Raise(SWIG_NewPointerObj((new Xapian::Error(e)),
127 SWIGTYPE_p_Xapian__Error,
130 SWIGTYPE_p_Xapian__Error);
131 } catch (const std::exception& e) {
132 SWIG_Error(SWIG_RuntimeError, e.what());
134 SWIG_Error(SWIG_UnknownError, "unknown error in Xapian");
140 /* Functions and methods which are marked as "nothrow": */
144 exception_data::for_each_nothrow(sub { print FD "%exception $_[0];\n" });
152 Xapian::SetPythonException();
157 /* The following exception directives are for the methods defined by
158 * director classes, which don't drop the GIL in the first place. */
160 for my $class (@directorclasses) {
162 %exception Xapian::${class}::operator() {
166 Xapian::SetPythonException();
173 for my $method (@posting_source_virtual_methods) {
175 %exception Xapian::PostingSource::${method} {
179 Xapian::SetPythonException();
186 for my $method (@matchspy_virtual_methods) {
188 %exception Xapian::MatchSpy::${method} {
192 Xapian::SetPythonException();
200 %exception Xapian::Stopper::get_description {
204 Xapian::SetPythonException();
208 %exception Xapian::StemImplementation::get_description {
212 Xapian::SetPythonException();
216 %exception Xapian::Compactor::set_status {
220 Xapian::SetPythonException();
224 %exception Xapian::Compactor::resolve_duplicate_metadata {
228 Xapian::SetPythonException();
233 /* If a Python error is raised by a call to a director function, the following
234 * code should cause a C++ exception to be thrown.
236 %feature("director:except") {
237 if ($error != NULL) {
238 throw Swig::DirectorMethodException();
242 /* vim:syntax=cpp:set noexpandtab: */