Fix whitespace irregularities in code
[xapian.git] / xapian-bindings / python / generate-python-exceptions
blobf5419bfcd86e4c6c3f9de8b9f208a4b268935fdc
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
20 END
22 use strict;
23 use exception_data;
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(
33     get_termfreq_min
34     get_termfreq_est
35     get_termfreq_max
36     get_maxweight
37     get_weight
38     next
39     skip_to
40     check
41     at_end
42     get_docid
43     get_description
44     init
45     name
48 my @matchspy_virtual_methods = qw(
49     name
50     merge_results
51     get_description
54 open FD, ">except.i" or die $!;
56 $copyright =~ s/^/ */mg;
58 print FD <<"EOF";
59 /** \@file python/except.i
60  * \@brief Custom Python exception handling.
61  */
62 /* Warning: This file is generated by $0
63  * - do not modify directly!
64  *
65 $copyright */
67 EOF
69 print FD <<'EOF';
70 namespace Xapian {
72 %exceptionclass Error;
73 %ignore Error::get_description;
74 %extend Error {
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();
80             desc += ')';
81         }
82         if ($self->get_error_string()) {
83             desc += " (";
84             desc += $self->get_error_string();
85             desc += ')';
86         }
87         return desc;
88     }
90 EOF
92 for (@allclasses) {
93     my ($class, $parent, $comment) = split /\t/, $_, 3;
94     print FD "%exceptionclass $class;\n";
97 print FD <<'EOF';
99 %include "xapian/error.h"
102 namespace Xapian {
103 SWIGEXPORT void SetPythonException() {
104     try {
105         throw;
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;
114     print FD <<"EOF";
115     } catch (const Xapian::$class &e) {
116         SWIG_Python_Raise(SWIG_NewPointerObj((new Xapian::$class(e)),
117                                              SWIGTYPE_p_Xapian__$class,
118                                              SWIG_POINTER_OWN),
119                           "Xapian::$class",
120                           SWIGTYPE_p_Xapian__$class);
124 print FD <<'EOF';
125     } catch (const Xapian::Error &e) {
126         SWIG_Python_Raise(SWIG_NewPointerObj((new Xapian::Error(e)),
127                                              SWIGTYPE_p_Xapian__Error,
128                                              SWIG_POINTER_OWN),
129                           "Xapian::Error",
130                           SWIGTYPE_p_Xapian__Error);
131     } catch (const std::exception& e) {
132         SWIG_Error(SWIG_RuntimeError, e.what());
133     } catch (...) {
134         SWIG_Error(SWIG_UnknownError, "unknown error in Xapian");
135     }
140 /* Functions and methods which are marked as "nothrow": */
143 chdir($INC[0]);
144 exception_data::for_each_nothrow(sub { print FD "%exception $_[0];\n" });
146 print FD <<'EOF';
148 %exception {
149     try {
150         $action
151     } catch (...) {
152         Xapian::SetPythonException();
153         SWIG_fail;
154     }
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) {
161     print FD <<"EOF";
162 %exception Xapian::${class}::operator() {
163     try {
164         \$action
165     } catch (...) {
166         Xapian::SetPythonException();
167         SWIG_fail;
168     }
173 for my $method (@posting_source_virtual_methods) {
174     print FD <<"EOF";
175 %exception Xapian::PostingSource::${method} {
176     try {
177         \$action
178     } catch (...) {
179         Xapian::SetPythonException();
180         SWIG_fail;
181     }
186 for my $method (@matchspy_virtual_methods) {
187     print FD <<"EOF";
188 %exception Xapian::MatchSpy::${method} {
189     try {
190         \$action
191     } catch (...) {
192         Xapian::SetPythonException();
193         SWIG_fail;
194     }
199 print FD <<'EOF';
200 %exception Xapian::Stopper::get_description {
201     try {
202         $action
203     } catch (...) {
204         Xapian::SetPythonException();
205         SWIG_fail;
206     }
208 %exception Xapian::StemImplementation::get_description {
209     try {
210         $action
211     } catch (...) {
212         Xapian::SetPythonException();
213         SWIG_fail;
214     }
216 %exception Xapian::Compactor::set_status {
217     try {
218         $action
219     } catch (...) {
220         Xapian::SetPythonException();
221         SWIG_fail;
222     }
224 %exception Xapian::Compactor::resolve_duplicate_metadata {
225     try {
226         $action
227     } catch (...) {
228         Xapian::SetPythonException();
229         SWIG_fail;
230     }
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.
235  */
236 %feature("director:except") {
237     if ($error != NULL) {
238         throw Swig::DirectorMethodException();
239     }
242 /* vim:syntax=cpp:set noexpandtab: */
245 close FD or die $!;