new type wrapper for iterable objects

This commit is contained in:
Wenzel Jakob 2016-04-22 09:33:19 +02:00
parent 54b6fdd43d
commit 7ca2755880

View File

@ -212,6 +212,16 @@ private:
ssize_t pos = 0; ssize_t pos = 0;
}; };
inline bool iterable_check(PyObject *obj) {
PyObject *iter = PyObject_GetIter(obj);
if (iter) {
Py_DECREF(iter);
return true;
} else {
PyErr_Clear();
return false;
}
}
NAMESPACE_END(detail) NAMESPACE_END(detail)
@ -233,7 +243,6 @@ NAMESPACE_END(detail)
class iterator : public object { class iterator : public object {
public: public:
PYBIND11_OBJECT_DEFAULT(iterator, object, PyIter_Check) PYBIND11_OBJECT_DEFAULT(iterator, object, PyIter_Check)
iterator(handle obj, bool borrowed = false) : object(obj, borrowed) { }
iterator& operator++() { iterator& operator++() {
if (ptr()) if (ptr())
value = object(PyIter_Next(m_ptr), false); value = object(PyIter_Next(m_ptr), false);
@ -250,12 +259,17 @@ private:
mutable object value; mutable object value;
}; };
class iterable : public object {
public:
PYBIND11_OBJECT_DEFAULT(iterable, object, detail::iterable_check)
};
inline detail::accessor handle::operator[](handle key) const { return detail::accessor(ptr(), key.ptr(), false); } inline detail::accessor handle::operator[](handle key) const { return detail::accessor(ptr(), key.ptr(), false); }
inline detail::accessor handle::operator[](const char *key) const { return detail::accessor(ptr(), key, false); } inline detail::accessor handle::operator[](const char *key) const { return detail::accessor(ptr(), key, false); }
inline detail::accessor handle::attr(handle key) const { return detail::accessor(ptr(), key.ptr(), true); } inline detail::accessor handle::attr(handle key) const { return detail::accessor(ptr(), key.ptr(), true); }
inline detail::accessor handle::attr(const char *key) const { return detail::accessor(ptr(), key, true); } inline detail::accessor handle::attr(const char *key) const { return detail::accessor(ptr(), key, true); }
inline iterator handle::begin() const { return iterator(PyObject_GetIter(ptr())); } inline iterator handle::begin() const { return iterator(PyObject_GetIter(ptr()), false); }
inline iterator handle::end() const { return iterator(nullptr); } inline iterator handle::end() const { return iterator(nullptr, false); }
class str : public object { class str : public object {
public: public: