Stop forward declaring global QueryOptimiser in API header
[xapian.git] / xapian-bindings / python / generate-python-exceptions
blob16828592ace66e709122f069db97d21c172514fc
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 @posting_source_virtual_methods = qw(
28     get_termfreq_min
29     get_termfreq_est
30     get_termfreq_max
31     get_maxweight
32     get_weight
33     next
34     skip_to
35     check
36     at_end
37     get_docid
38     get_description
39     init
40     name
43 my @matchspy_virtual_methods = qw(
44     name
45     merge_results
46     get_description
49 open FD, ">except.i" or die $!;
51 $copyright =~ s/^/ */mg;
53 print FD <<"EOF";
54 /** \@file python/except.i
55  * \@brief Custom Python exception handling.
56  */
57 /* Warning: This file is generated by $0
58  * - do not modify directly!
59  *
60 $copyright */
62 EOF
64 print FD <<'EOF';
65 namespace Xapian {
67 %exceptionclass Error;
68 %ignore Error::get_description;
69 %extend Error {
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();
75             desc += ')';
76         }
77         if ($self->get_error_string()) {
78             desc += " (";
79             desc += $self->get_error_string();
80             desc += ')';
81         }
82         return desc;
83     }
85 EOF
87 for (@allclasses) {
88     my ($class, $parent, $comment) = split /\t/, $_, 3;
89     print FD "%exceptionclass $class;\n";
92 print FD <<'EOF';
94 %include "xapian/error.h"
97 namespace Xapian {
98 SWIGEXPORT void SetPythonException() {
99     try {
100         throw;
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;
109     print FD <<"EOF";
110     } catch (const Xapian::$class &e) {
111         SWIG_Python_Raise(SWIG_NewPointerObj((new Xapian::$class(e)),
112                                              SWIGTYPE_p_Xapian__$class,
113                                              SWIG_POINTER_OWN),
114                           "Xapian::$class",
115                           SWIGTYPE_p_Xapian__$class);
119 print FD <<'EOF';
120     } catch (const Xapian::Error &e) {
121         SWIG_Python_Raise(SWIG_NewPointerObj((new Xapian::Error(e)),
122                                              SWIGTYPE_p_Xapian__Error,
123                                              SWIG_POINTER_OWN),
124                           "Xapian::Error",
125                           SWIGTYPE_p_Xapian__Error);
126     } catch (const std::exception& e) {
127         SWIG_Error(SWIG_RuntimeError, e.what());
128     } catch (...) {
129         SWIG_Error(SWIG_UnknownError, "unknown error in Xapian");
130     }
135 /* Functions and methods which are marked as "nothrow": */
138 chdir($INC[0]);
139 exception_data::for_each_nothrow(sub { print FD "%exception $_[0];\n" });
141 print FD <<'EOF';
143 %exception {
144     try {
145         $action
146     } catch (...) {
147         Xapian::SetPythonException();
148         SWIG_fail;
149     }
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.
154  */
155 %feature("director:except") {
156     if ($error != NULL) {
157         throw Swig::DirectorMethodException();
158     }
161 /* vim:syntax=cpp:set noexpandtab: */
164 close FD or die $!;