Python -builtin __contains__ fix for map and set like containers.
Fix when using -builtin and wrapping std::map, std::set, std::unordered_map or std::unordered_set to ensure __contains__ is called. This is a wrapper for the STL container's find method. Without it, Python will do its own slower sequence search.
This commit is contained in:
parent
333209595d
commit
4715a4e72c
8 changed files with 76 additions and 2 deletions
|
|
@ -635,6 +635,42 @@ SwigPyBuiltin_ssizeobjargproc_closure(SwigPyWrapperFunction wrapper, PyObject *a
|
|||
return result;
|
||||
}
|
||||
|
||||
#define SWIGPY_OBJOBJPROC_CLOSURE(wrapper) \
|
||||
SWIGINTERN int \
|
||||
wrapper##_objobjproc_closure(PyObject *a, PyObject *b) { \
|
||||
return SwigPyBuiltin_objobjproc_closure(wrapper, a, b); \
|
||||
}
|
||||
SWIGINTERN int
|
||||
SwigPyBuiltin_objobjproc_closure(SwigPyWrapperFunction wrapper, PyObject *a, PyObject *b) {
|
||||
int result;
|
||||
PyObject *pyresult;
|
||||
PyObject *tuple;
|
||||
tuple = PyTuple_New(1);
|
||||
assert(tuple);
|
||||
PyTuple_SET_ITEM(tuple, 0, b);
|
||||
Py_XINCREF(b);
|
||||
pyresult = wrapper(a, tuple);
|
||||
result = pyresult ? (PyObject_IsTrue(pyresult) ? 1 : 0) : -1;
|
||||
Py_XDECREF(pyresult);
|
||||
Py_DECREF(tuple);
|
||||
return result;
|
||||
}
|
||||
|
||||
#define SWIGPY_FUNPACK_OBJOBJPROC_CLOSURE(wrapper) \
|
||||
SWIGINTERN int \
|
||||
wrapper##_objobjproc_closure(PyObject *a, PyObject *b) { \
|
||||
return SwigPyBuiltin_funpack_objobjproc_closure(wrapper, a, b); \
|
||||
}
|
||||
SWIGINTERN int
|
||||
SwigPyBuiltin_funpack_objobjproc_closure(SwigPyWrapperFunction wrapper, PyObject *a, PyObject *b) {
|
||||
int result;
|
||||
PyObject *pyresult;
|
||||
pyresult = wrapper(a, b);
|
||||
result = pyresult ? (PyObject_IsTrue(pyresult) ? 1 : 0) : -1;
|
||||
Py_XDECREF(pyresult);
|
||||
return result;
|
||||
}
|
||||
|
||||
#define SWIGPY_OBJOBJARGPROC_CLOSURE(wrapper) \
|
||||
SWIGINTERN int \
|
||||
wrapper##_objobjargproc_closure(PyObject *a, PyObject *b, PyObject *c) { \
|
||||
|
|
|
|||
|
|
@ -156,6 +156,7 @@
|
|||
%feature("python:slot", "mp_length", functype="lenfunc") __len__;
|
||||
%feature("python:slot", "mp_subscript", functype="binaryfunc") __getitem__;
|
||||
%feature("python:slot", "tp_iter", functype="getiterfunc") key_iterator;
|
||||
%feature("python:slot", "sq_contains", functype="objobjproc") __contains__;
|
||||
|
||||
%extend {
|
||||
%newobject iterkeys(PyObject **PYTHON_SELF);
|
||||
|
|
@ -263,7 +264,6 @@
|
|||
return itemList;
|
||||
}
|
||||
|
||||
// Python 2.2 methods
|
||||
bool __contains__(const key_type& key) {
|
||||
return self->find(key) != self->end();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,10 @@
|
|||
%swig_sequence_iterator(set);
|
||||
%swig_container_methods(set);
|
||||
|
||||
#if defined(SWIGPYTHON_BUILTIN)
|
||||
%feature("python:slot", "sq_contains", functype="objobjproc") __contains__;
|
||||
#endif
|
||||
|
||||
%extend {
|
||||
void append(value_type x) {
|
||||
self->insert(x);
|
||||
|
|
|
|||
|
|
@ -131,6 +131,10 @@
|
|||
%swig_sequence_forward_iterator(Map);
|
||||
%swig_container_methods(Map)
|
||||
|
||||
#if defined(SWIGPYTHON_BUILTIN)
|
||||
%feature("python:slot", "sq_contains", functype="objobjproc") __contains__;
|
||||
#endif
|
||||
|
||||
%extend {
|
||||
mapped_type __getitem__(const key_type& key) const throw (std::out_of_range) {
|
||||
Map::const_iterator i = self->find(key);
|
||||
|
|
@ -204,7 +208,6 @@
|
|||
return itemList;
|
||||
}
|
||||
|
||||
// Python 2.2 methods
|
||||
bool __contains__(const key_type& key) {
|
||||
return self->find(key) != self->end();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,6 +43,10 @@
|
|||
%swig_sequence_forward_iterator(unordered_set);
|
||||
%swig_container_methods(unordered_set);
|
||||
|
||||
#if defined(SWIGPYTHON_BUILTIN)
|
||||
%feature("python:slot", "sq_contains", functype="objobjproc") __contains__;
|
||||
#endif
|
||||
|
||||
%extend {
|
||||
void append(value_type x) {
|
||||
self->insert(x);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue