[ci] Fix clang-santisers job for GHA change
[xapian.git] / xapian-bindings / python3 / util.i
blobf6fb7d5afd00c8e4fcb97e3db8cb3f5f6d880d67
1 %{
2 /* python/util.i: custom Python typemaps for xapian-bindings
4 * Copyright (C) 2002,2003 James Aylett
5 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2013,2016,2017,2019 Olly Betts
6 * Copyright (C) 2007 Lemur Consulting Ltd
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 * USA
25 %include typemaps.i
26 %include stl.i
28 /* Wrap get_description() methods as str(). */
29 %rename(__str__) get_description;
31 /* So iterator objects match the Python3 iterator API. */
32 %rename(__next__) next;
34 /* Hide "unsafe" C++ iterator methods. */
35 %rename(_allterms_begin) Xapian::Database::allterms_begin;
36 %rename(_allterms_end) Xapian::Database::allterms_end;
37 %rename(_metadata_keys_begin) Xapian::Database::metadata_keys_begin;
38 %rename(_metadata_keys_end) Xapian::Database::metadata_keys_end;
39 %rename(_synonym_keys_begin) Xapian::Database::synonym_keys_begin;
40 %rename(_synonym_keys_end) Xapian::Database::synonym_keys_end;
41 %rename(_synonyms_begin) Xapian::Database::synonyms_begin;
42 %rename(_synonyms_end) Xapian::Database::synonyms_end;
43 %rename(_spellings_begin) Xapian::Database::spellings_begin;
44 %rename(_spellings_end) Xapian::Database::spellings_end;
45 %rename(_positionlist_begin) Xapian::Database::positionlist_begin;
46 %rename(_positionlist_end) Xapian::Database::positionlist_end;
47 %rename(_postlist_begin) Xapian::Database::postlist_begin;
48 %rename(_postlist_end) Xapian::Database::postlist_end;
49 %rename(_termlist_begin) Xapian::Database::termlist_begin;
50 %rename(_termlist_end) Xapian::Database::termlist_end;
51 %rename(_termlist_begin) Xapian::Document::termlist_begin;
52 %rename(_termlist_end) Xapian::Document::termlist_end;
53 %rename(_values_begin) Xapian::Document::values_begin;
54 %rename(_values_end) Xapian::Document::values_end;
55 %rename(_get_matching_terms_begin) Xapian::Enquire::get_matching_terms_begin;
56 %rename(_get_matching_terms_end) Xapian::Enquire::get_matching_terms_end;
57 %rename(_begin) Xapian::ESet::begin;
58 %rename(_end) Xapian::ESet::end;
59 %rename(_begin) Xapian::MSet::begin;
60 %rename(_end) Xapian::MSet::end;
61 %rename(_positionlist_begin) Xapian::PostingIterator::positionlist_begin;
62 %rename(_positionlist_end) Xapian::PostingIterator::positionlist_end;
63 %rename(_get_terms_begin) Xapian::Query::get_terms_begin;
64 %rename(_get_terms_end) Xapian::Query::get_terms_end;
65 %rename(_stoplist_begin) Xapian::QueryParser::stoplist_begin;
66 %rename(_stoplist_end) Xapian::QueryParser::stoplist_end;
67 %rename(_unstem_begin) Xapian::QueryParser::unstem_begin;
68 %rename(_unstem_end) Xapian::QueryParser::unstem_end;
69 %rename(_positionlist_begin) Xapian::TermIterator::positionlist_begin;
70 %rename(_positionlist_end) Xapian::TermIterator::positionlist_end;
72 /* We replace the get_hit() method with one which returns an MSetitem. */
73 %rename(_get_hit_internal) Xapian::MSet::get_hit;
76 namespace Xapian {
77 Query *get_py_query(PyObject *obj) {
78 PyObject * mythis = PyObject_GetAttrString(obj, "this");
79 if (!mythis)
80 return 0;
82 Query * retval = 0;
83 int res = SWIG_ConvertPtr(mythis, (void **)&retval,
84 SWIGTYPE_p_Xapian__Query, 0);
85 if (!SWIG_IsOK(res)) {
86 retval = 0;
88 Py_DECREF(mythis);
89 return retval;
94 namespace Xapian {
95 %extend TermIterator {
96 bool __eq__(const TermIterator &other) {
97 return (*self)==other;
99 bool __ne__(const TermIterator &other) {
100 return (*self)!=other;
103 %rename(_TermIterator) TermIterator;
105 %extend PositionIterator {
106 bool __eq__(const PositionIterator &other) {
107 return (*self)==other;
109 bool __ne__(const PositionIterator &other) {
110 return (*self)!=other;
113 %rename(_PositionIterator) PositionIterator;
115 %extend PostingIterator {
116 bool __eq__(const PostingIterator &other) {
117 return (*self)==other;
119 bool __ne__(const PostingIterator &other) {
120 return (*self)!=other;
123 %rename(_PostingIterator) PostingIterator;
125 %extend ValueIterator {
126 bool __eq__(const ValueIterator &other) {
127 return (*self)==other;
129 bool __ne__(const ValueIterator &other) {
130 return (*self)!=other;
133 %rename(_ValueIterator) ValueIterator;
135 %extend MSetIterator {
136 bool __eq__(const MSetIterator &other) {
137 return (*self)==other;
139 bool __ne__(const MSetIterator &other) {
140 return (*self)!=other;
143 %rename(_MSetIterator) MSetIterator;
145 %extend ESetIterator {
146 bool __eq__(const ESetIterator &other) {
147 return (*self)==other;
149 bool __ne__(const ESetIterator &other) {
150 return (*self)!=other;
153 %rename(_ESetIterator) ESetIterator;
156 %fragment("XapianSWIG_anystring_as_ptr", "header", fragment="SWIG_AsPtr_std_string") {
157 /* Utility function which works like SWIG_AsPtr_std_string, but
158 * converts unicode strings to UTF-8 simple strings first. */
159 static int
160 XapianSWIG_anystring_as_ptr(PyObject * obj, std::string **val)
162 if (PyUnicode_Check(obj)) {
163 PyObject* strobj = PyUnicode_AsUTF8String(obj);
164 if (strobj == NULL) return INT_MIN;
165 char *p;
166 Py_ssize_t len;
167 PyBytes_AsStringAndSize(strobj, &p, &len);
168 if (val) *val = new std::string(p, len);
169 Py_DECREF(strobj);
170 return SWIG_NEWOBJ;
171 } else if (PyBytes_Check(obj)) {
172 char *p;
173 Py_ssize_t len;
174 PyBytes_AsStringAndSize(obj, &p, &len);
175 if (val) *val = new std::string(p, len);
176 return SWIG_NEWOBJ;
177 } else {
178 return SWIG_AsPtr_std_string(obj, val);
183 /* These typemaps depends somewhat heavily on the internals of SWIG, so
184 * might break with future versions of SWIG.
186 %typemap(in, fragment="XapianSWIG_anystring_as_ptr") const std::string &(int res = SWIG_OLDOBJ) {
187 std::string *ptr = (std::string *)0;
188 res = XapianSWIG_anystring_as_ptr($input, &ptr);
189 if (!SWIG_IsOK(res)) {
190 if (res == INT_MIN) SWIG_fail;
191 %argument_fail(res, "$type", $symname, $argnum);
193 if (!ptr) {
194 %argument_nullref("$type", $symname, $argnum);
196 $1 = ptr;
198 %typemap(in, fragment="XapianSWIG_anystring_as_ptr") std::string {
199 std::string *ptr = (std::string *)0;
200 int res = XapianSWIG_anystring_as_ptr($input, &ptr);
201 if (!SWIG_IsOK(res) || !ptr) {
202 if (res == INT_MIN) SWIG_fail;
203 %argument_fail((ptr ? res : SWIG_TypeError), "$type", $symname, $argnum);
205 $1 = *ptr;
206 if (SWIG_IsNewObj(res)) delete ptr;
208 %typemap(freearg, noblock=1, match="in") const std::string & {
209 if (SWIG_IsNewObj(res$argnum)) %delete($1);
211 %typemap(typecheck, noblock=1, precedence=900) const std::string & {
212 if (PyUnicode_Check($input)) {
213 $1 = 1;
214 } else if (PyBytes_Check($input)) {
215 $1 = 1;
216 } else {
217 int res = SWIG_AsPtr_std_string($input, (std::string**)(0));
218 $1 = SWIG_CheckState(res);
222 %typemap(in, fragment="XapianSWIG_anystring_as_ptr") const std::string *(int res = SWIG_OLDOBJ) {
223 std::string *ptr = (std::string *)0;
224 if ($input != Py_None) {
225 res = XapianSWIG_anystring_as_ptr($input, &ptr);
226 if (!SWIG_IsOK(res)) {
227 if (res == INT_MIN) SWIG_fail;
228 %argument_fail(res, "$type", $symname, $argnum);
231 $1 = ptr;
233 %typemap(freearg, noblock=1, match="in") const std::string * {
234 if (SWIG_IsNewObj(res$argnum)) %delete($1);
236 %typemap(typecheck, noblock=1, precedence=900) const std::string * {
237 if ($input == Py_None) {
238 $1 = 1;
239 } else if (PyUnicode_Check($input)) {
240 $1 = 1;
241 } else if (PyBytes_Check($input)) {
242 $1 = 1;
243 } else {
244 int res = SWIG_AsPtr_std_string($input, (std::string**)(0));
245 $1 = SWIG_CheckState(res);
249 /* This typemap is only currently needed for returning a value from the
250 * get_description() method of a Stopper subclass to a C++ caller, but might be
251 * more generally useful in future.
253 %typemap(directorout, noblock=1, fragment="XapianSWIG_anystring_as_ptr") std::string {
254 std::string *swig_optr = 0;
255 int swig_ores;
257 PyObject * tmp = $input;
258 Py_INCREF(tmp);
259 swig_ores = XapianSWIG_anystring_as_ptr(tmp, &swig_optr);
260 Py_DECREF(tmp);
262 if (!SWIG_IsOK(swig_ores) || !swig_optr) {
263 %dirout_fail((swig_optr ? swig_ores : SWIG_TypeError), "$type");
265 $result = *swig_optr;
266 if (SWIG_IsNewObj(swig_ores)) %delete(swig_optr);
269 %typemap(directorin) (size_t num_tags, const std::string tags[]) {
270 PyObject * result = PyList_New(num_tags);
271 if (result == 0) {
272 return NULL;
275 for (size_t i = 0; i != num_tags; ++i) {
276 PyObject * str = PyBytes_FromStringAndSize(tags[i].data(), tags[i].size());
277 if (str == 0) {
278 Py_DECREF(result);
279 return NULL;
282 PyList_SET_ITEM(result, i, str);
284 $input = result;
287 /* vim:set syntax=cpp:set noexpandtab: */