Optimise diversification by implementing C2-GLS
[xapian.git] / xapian-bindings / python3 / util.i
blob316aec173d3beda65238270c47232c13dd680868
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 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;
75 /* Force xapian.BAD_VALUENO to be handled as a constant rather than a
76 * read-only variable (ticket#297).
78 %ignore Xapian::BAD_VALUENO;
79 %constant Xapian::valueno BAD_VALUENO = Xapian::BAD_VALUENO;
82 namespace Xapian {
83 Query *get_py_query(PyObject *obj) {
84 PyObject * mythis = PyObject_GetAttrString(obj, "this");
85 if (!mythis)
86 return 0;
88 Query * retval = 0;
89 int res = SWIG_ConvertPtr(mythis, (void **)&retval,
90 SWIGTYPE_p_Xapian__Query, 0);
91 if (!SWIG_IsOK(res)) {
92 retval = 0;
94 Py_DECREF(mythis);
95 return retval;
100 namespace Xapian {
101 %extend TermIterator {
102 bool __eq__(const TermIterator &other) {
103 return (*self)==other;
105 bool __ne__(const TermIterator &other) {
106 return (*self)!=other;
109 %rename(_TermIterator) TermIterator;
111 %extend PositionIterator {
112 bool __eq__(const PositionIterator &other) {
113 return (*self)==other;
115 bool __ne__(const PositionIterator &other) {
116 return (*self)!=other;
119 %rename(_PositionIterator) PositionIterator;
121 %extend PostingIterator {
122 bool __eq__(const PostingIterator &other) {
123 return (*self)==other;
125 bool __ne__(const PostingIterator &other) {
126 return (*self)!=other;
129 %rename(_PostingIterator) PostingIterator;
131 %extend ValueIterator {
132 bool __eq__(const ValueIterator &other) {
133 return (*self)==other;
135 bool __ne__(const ValueIterator &other) {
136 return (*self)!=other;
139 %rename(_ValueIterator) ValueIterator;
141 %extend MSetIterator {
142 bool __eq__(const MSetIterator &other) {
143 return (*self)==other;
145 bool __ne__(const MSetIterator &other) {
146 return (*self)!=other;
149 %rename(_MSetIterator) MSetIterator;
151 %extend ESetIterator {
152 bool __eq__(const ESetIterator &other) {
153 return (*self)==other;
155 bool __ne__(const ESetIterator &other) {
156 return (*self)!=other;
159 %rename(_ESetIterator) ESetIterator;
162 %fragment("XapianSWIG_anystring_as_ptr", "header", fragment="SWIG_AsPtr_std_string") {
163 /* Utility function which works like SWIG_AsPtr_std_string, but
164 * converts unicode strings to UTF-8 simple strings first. */
165 static int
166 XapianSWIG_anystring_as_ptr(PyObject * obj, std::string **val)
168 if (PyUnicode_Check(obj)) {
169 PyObject * strobj = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(obj), PyUnicode_GET_SIZE(obj), "ignore");
170 if (strobj == NULL) return SWIG_ERROR;
171 char *p;
172 Py_ssize_t len;
173 PyBytes_AsStringAndSize(strobj, &p, &len);
174 if (val) *val = new std::string(p, len);
175 Py_DECREF(strobj);
176 return SWIG_NEWOBJ;
177 } else if (PyBytes_Check(obj)) {
178 char *p;
179 Py_ssize_t len;
180 PyBytes_AsStringAndSize(obj, &p, &len);
181 if (val) *val = new std::string(p, len);
182 return SWIG_NEWOBJ;
183 } else {
184 return SWIG_AsPtr_std_string(obj, val);
189 /* These typemaps depends somewhat heavily on the internals of SWIG, so
190 * might break with future versions of SWIG.
192 %typemap(in, fragment="XapianSWIG_anystring_as_ptr") const std::string &(int res = SWIG_OLDOBJ) {
193 std::string *ptr = (std::string *)0;
194 res = XapianSWIG_anystring_as_ptr($input, &ptr);
195 if (!SWIG_IsOK(res)) {
196 %argument_fail(res, "$type", $symname, $argnum);
198 if (!ptr) {
199 %argument_nullref("$type", $symname, $argnum);
201 $1 = ptr;
203 %typemap(in, fragment="XapianSWIG_anystring_as_ptr") std::string {
204 std::string *ptr = (std::string *)0;
205 int res = XapianSWIG_anystring_as_ptr($input, &ptr);
206 if (!SWIG_IsOK(res) || !ptr) {
207 %argument_fail((ptr ? res : SWIG_TypeError), "$type", $symname, $argnum);
209 $1 = *ptr;
210 if (SWIG_IsNewObj(res)) delete ptr;
212 %typemap(freearg, noblock=1, match="in") const std::string & {
213 if (SWIG_IsNewObj(res$argnum)) %delete($1);
215 %typemap(typecheck, noblock=1, precedence=900, fragment="XapianSWIG_anystring_as_ptr") const std::string & {
216 if (PyUnicode_Check($input)) {
217 $1 = 1;
218 } else if (PyBytes_Check($input)) {
219 $1 = 1;
220 } else {
221 int res = SWIG_AsPtr_std_string($input, (std::string**)(0));
222 $1 = SWIG_CheckState(res);
226 %typemap(in, fragment="XapianSWIG_anystring_as_ptr") const std::string *(int res = SWIG_OLDOBJ) {
227 std::string *ptr = (std::string *)0;
228 if ($input != Py_None) {
229 res = XapianSWIG_anystring_as_ptr($input, &ptr);
230 if (!SWIG_IsOK(res)) {
231 %argument_fail(res, "$type", $symname, $argnum);
234 $1 = ptr;
236 %typemap(freearg, noblock=1, match="in") const std::string * {
237 if (SWIG_IsNewObj(res$argnum)) %delete($1);
239 %typemap(typecheck, noblock=1, precedence=900, fragment="XapianSWIG_anystring_as_ptr") const std::string * {
240 if ($input == Py_None) {
241 $1 = 1;
242 } else if (PyUnicode_Check($input)) {
243 $1 = 1;
244 } else if (PyBytes_Check($input)) {
245 $1 = 1;
246 } else {
247 int res = SWIG_AsPtr_std_string($input, (std::string**)(0));
248 $1 = SWIG_CheckState(res);
252 /* This typemap is only currently needed for returning a value from the
253 * get_description() method of a Stopper subclass to a C++ caller, but might be
254 * more generally useful in future.
256 %typemap(directorout, noblock=1, fragment="XapianSWIG_anystring_as_ptr") std::string {
257 std::string *swig_optr = 0;
258 int swig_ores;
260 PyObject * tmp = $input;
261 Py_INCREF(tmp);
262 swig_ores = XapianSWIG_anystring_as_ptr(tmp, &swig_optr);
263 Py_DECREF(tmp);
265 if (!SWIG_IsOK(swig_ores) || !swig_optr) {
266 %dirout_fail((swig_optr ? swig_ores : SWIG_TypeError), "$type");
268 $result = *swig_optr;
269 if (SWIG_IsNewObj(swig_ores)) %delete(swig_optr);
272 /** This pair of typemaps implements conversion of the return value of
273 * ValueRangeProcessor subclasses implemented in Python from a tuple of
274 * (valueno, begin, end) to a return value of valueno, and assigning the new
275 * values of begin and end to the parameters.
277 %typemap(directorin,noblock=1) std::string & {
278 $input = SWIG_From_std_string(static_cast< std::string >($1_name));
280 %typemap(directorout,noblock=1) Xapian::valueno {
281 if (!PyTuple_Check($input)) {
282 %dirout_fail(SWIG_TypeError, "($type, std::string, std::string)");
284 if (PyTuple_Size($input) != 3) {
285 %dirout_fail(SWIG_IndexError, "($type, std::string, std::string)");
288 // Set the return value from the first item of the tuple.
289 unsigned int swig_val;
290 int swig_res = SWIG_AsVal_unsigned_SS_int(PyTuple_GET_ITEM((PyObject *)$input, 0), &swig_val);
291 if (!SWIG_IsOK(swig_res)) {
292 %dirout_fail(swig_res, "($type, std::string, std::string)");
294 c_result = static_cast< Xapian::valueno >(swig_val);
296 // Set "begin" from the second item of the tuple.
297 std::string *ptr = (std::string *)0;
298 swig_res = SWIG_AsPtr_std_string(PyTuple_GET_ITEM((PyObject *)$input, 1), &ptr);
299 if (!SWIG_IsOK(swig_res) || !ptr) {
300 delete ptr;
301 ptr = (std::string *)0;
302 %dirout_fail((ptr ? swig_res : SWIG_TypeError), "($type, std::string, std::string)");
304 begin = *ptr;
305 delete ptr;
306 ptr = (std::string *)0;
308 // Set "end" from the third item of the tuple.
309 swig_res = SWIG_AsPtr_std_string(PyTuple_GET_ITEM((PyObject *)$input, 2), &ptr);
310 if (!SWIG_IsOK(swig_res) || !ptr) {
311 delete ptr;
312 ptr = (std::string *)0;
313 %dirout_fail((ptr ? swig_res : SWIG_TypeError), "($type, std::string, std::string)");
315 end = *ptr;
316 delete ptr;
317 ptr = (std::string *)0;
320 /* These typemaps handle ValueRangeProcessors, which take non-const references
321 * to std::string and modify the strings.
323 %typemap(in) std::string &begin (std::string temp),
324 std::string &end (std::string temp) {
325 std::string *ptr = (std::string *)0;
326 int res = XapianSWIG_anystring_as_ptr($input, &ptr);
327 if (!SWIG_IsOK(res) || !ptr) {
328 %argument_fail((ptr ? res : SWIG_TypeError), "$type", $symname, $argnum);
330 temp = *ptr;
331 $1 = &temp;
332 if (SWIG_IsNewObj(res)) delete ptr;
334 %typemap(argout) (std::string &begin, std::string &end) {
335 PyObject * str;
336 PyObject * newresult;
338 // Put the existing result into the first item of a new 3-tuple.
339 newresult = PyTuple_New(3);
340 if (newresult == 0) {
341 Py_DECREF($result);
342 $result = NULL;
343 SWIG_fail;
345 PyTuple_SET_ITEM(newresult, 0, $result);
346 $result = newresult;
348 str = PyBytes_FromStringAndSize($1->data(), $1->size());
349 if (str == 0) {
350 Py_DECREF($result);
351 $result = NULL;
352 SWIG_fail;
354 PyTuple_SET_ITEM($result, 1, str);
356 str = PyBytes_FromStringAndSize($2->data(), $2->size());
357 if (str == 0) {
358 Py_DECREF($result);
359 $result = NULL;
360 SWIG_fail;
363 PyTuple_SET_ITEM($result, 2, str);
366 %typemap(directorin) (size_t num_tags, const std::string tags[]) {
367 PyObject * result = PyList_New(num_tags);
368 if (result == 0) {
369 return NULL;
372 for (size_t i = 0; i != num_tags; ++i) {
373 PyObject * str = PyBytes_FromStringAndSize(tags[i].data(), tags[i].size());
374 if (str == 0) {
375 Py_DECREF(result);
376 return NULL;
379 PyList_SET_ITEM(result, i, str);
381 $input = result;
384 /* vim:set syntax=cpp:set noexpandtab: */