-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPyIterator.cpp
More file actions
87 lines (69 loc) · 2.3 KB
/
Copy pathPyIterator.cpp
File metadata and controls
87 lines (69 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "PyIterator.hpp"
#include "runtime/PyObject.hpp"
#include "runtime/StopIteration.hpp"
#include "runtime/Value.hpp"
#include "runtime/ValueError.hpp"
#include "types/api.hpp"
#include "types/builtin.hpp"
#include <limits>
using namespace py;
PyIterator::PyIterator(PyObject *iterator)
: PyBaseObject(types::BuiltinTypes::the().iterator()), m_iterator(iterator)
{}
PyResult<PyIterator *> PyIterator::create(PyObject *iterator)
{
auto *result = VirtualMachine::the().heap().allocate<PyIterator>(iterator);
if (!result) { return Err(memory_error(sizeof(PyIterator))); }
return Ok(result);
}
PyResult<PyObject *> PyIterator::__iter__() const { return Ok(const_cast<PyIterator *>(this)); }
PyResult<PyObject *> PyIterator::__next__()
{
if (!m_iterator) { return Ok(py_none()); }
if (m_index == std::numeric_limits<size_t>::max()) {
return Err(value_error("iter index too large"));
}
auto result = m_iterator->getitem(m_index);
if (result.is_err()
&& (result.unwrap_err()->type()->issubclass(types::index_error())
|| result.unwrap_err()->type()->issubclass(types::stop_iteration()))) {
m_iterator = nullptr;
return Err(stop_iteration());
} else if (result.is_err()) {
return result;
}
m_index++;
return result;
}
PyResult<size_t> PyIterator::__len__() const
{
if (!m_iterator) { return Ok(size_t{ 0 }); }
if (m_iterator->type()->underlying_type().mapping_type_protocol.has_value()
&& m_iterator->type()->underlying_type().mapping_type_protocol->__len__.has_value()) {
return m_iterator->as_mapping()
.and_then([](auto mapping) { return mapping.len(); })
.and_then([this](size_t seqsize) -> PyResult<size_t> { return Ok(seqsize - m_index); });
}
return Ok(0);
}
void PyIterator::visit_graph(Visitor &visitor)
{
PyObject::visit_graph(visitor);
if (m_iterator) { visitor.visit(*m_iterator); }
}
PyType *PyIterator::static_type() const { return types::iterator(); }
namespace {
std::once_flag iterator_flag;
std::unique_ptr<TypePrototype> register_iterator()
{
return std::move(klass<PyIterator>("iterator").disable_new().type);
}
}// namespace
std::function<std::unique_ptr<TypePrototype>()> PyIterator::type_factory()
{
return [] {
static std::unique_ptr<TypePrototype> type = nullptr;
std::call_once(iterator_flag, []() { type = register_iterator(); });
return std::move(type);
};
}