-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPyArgParser.hpp
More file actions
152 lines (140 loc) · 4.85 KB
/
PyArgParser.hpp
File metadata and controls
152 lines (140 loc) · 4.85 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#pragma once
#include "PyDict.hpp"
#include "PyInteger.hpp"
#include "PyNone.hpp"
#include "PyTuple.hpp"
#include "PyType.hpp"
#include "TypeError.hpp"
#include "vm/VM.hpp"
#include <limits>
namespace py {
template<typename... ArgTypes> struct PyArgsParser
{
private:
template<size_t Idx,
size_t MinSize,
size_t MaxSize,
typename ResultType,
typename... DefaultArgs>
static constexpr PyResult<std::monostate> unpack_tuple_helper(const std::vector<Value> &args,
std::string_view function_name,
std::integral_constant<size_t, MinSize> min_size,
std::integral_constant<size_t, MaxSize> max_size,
ResultType &result,
DefaultArgs &&...default_args)
{
using ExpectedType = std::tuple_element_t<Idx, ResultType>;
if (args.size() > Idx) {
if constexpr (std::is_base_of_v<PyObject,
std::remove_pointer_t<std::remove_cv_t<ExpectedType>>>) {
using PyObjectType = std::remove_pointer_t<std::remove_cv_t<ExpectedType>>;
const auto &arg = PyObject::from(args[Idx]);
if (arg.is_err()) return Err(arg.unwrap_err());
if constexpr (std::is_same_v<PyObject, PyObjectType>) {
std::get<Idx>(result) = arg.unwrap();
} else {
if (!as<PyObjectType>(arg.unwrap())) return Err(type_error("Unexpected type"));
std::get<Idx>(result) = as<PyObjectType>(arg.unwrap());
}
} else if constexpr (std::is_same_v<bool,
std::remove_pointer_t<std::remove_cv_t<ExpectedType>>>) {
if (auto bool_arg = truthy(args[Idx], VirtualMachine::the().interpreter());
bool_arg.is_ok()) {
std::get<Idx>(result) = bool_arg.unwrap();
} else {
return Err(bool_arg.unwrap_err());
}
} else if constexpr (std::is_same_v<bool,
std::remove_pointer_t<std::remove_cv_t<ExpectedType>>>) {
TODO();
} else if constexpr (std::is_integral_v<ExpectedType>) {
auto int_obj = PyObject::from(args[Idx]);
if (int_obj.is_err()) { return Err(int_obj.unwrap_err()); }
if (!as<PyInteger>(int_obj.unwrap())) {
return Err(type_error("'{}' object cannot be interpreted as an integer",
int_obj.unwrap()->type()->name()));
} else {
auto value = as<PyInteger>(int_obj.unwrap())->as_i64();
static_assert(sizeof(ExpectedType) <= 8);
if (!fits_in<ExpectedType>(value)) {
return Err(type_error("{} not within range ({}, {})",
value,
std::numeric_limits<ExpectedType>::min(),
std::numeric_limits<ExpectedType>::max()));
}
std::get<Idx>(result) = static_cast<ExpectedType>(value);
}
} else {
[]<bool flag = false>() {
static_assert(flag, "unsupported Python to native conversion");
}();
}
} else {
if constexpr (Idx >= MinSize && (Idx - MinSize) < sizeof...(DefaultArgs)) {
std::get<Idx>(result) = std::get<Idx - MinSize>(
std::forward_as_tuple(std::forward<DefaultArgs>(default_args)...));
} else {
TODO();
}
}
if constexpr (Idx + 1 == std::tuple_size_v<ResultType>) {
return Ok(std::monostate{});
} else {
return unpack_tuple_helper<Idx + 1>(args,
function_name,
min_size,
max_size,
result,
std::forward<DefaultArgs>(default_args)...);
}
}
public:
template<size_t MinSize, size_t MaxSize, typename... DefaultArgs>
static constexpr PyResult<std::tuple<ArgTypes...>> unpack_tuple(PyTuple *args,
PyDict *kwargs,
std::string_view function_name,
std::integral_constant<size_t, MinSize> min_size,
std::integral_constant<size_t, MaxSize> max_size,
DefaultArgs &&...default_values)
{
if constexpr (max_size() - min_size() > sizeof...(DefaultArgs)) {
[]<bool flag = false>() { static_assert(flag, "Not enough default values"); }();
}
if constexpr (max_size() - min_size() < sizeof...(DefaultArgs)) {
[]<bool flag = false>() { static_assert(flag, "Too many default values"); }();
}
if (kwargs != nullptr && !kwargs->map().empty()) {
return Err(type_error("{} takes no keyword arguments", function_name));
}
if constexpr (max_size() - min_size() == 0) {
if (args->size() != min_size()) {
if constexpr (min_size() == 1) {
return Err(type_error(
"{} takes exactly one argument ({} given)", function_name, args->size()));
} else {
return Err(type_error("{} takes exactly {} arguments ({} given)",
function_name,
min_size(),
args->size()));
}
}
}
if (args->size() < min_size()) {
return Err(type_error(
"function takes at least {} arguments ({} given)'", min_size(), args->size()));
} else if (args->size() > max_size()) {
return Err(type_error(
"function takes at most {} argument ({} given)'", max_size(), args->size()));
}
std::tuple<ArgTypes...> unpacked_args;
auto result = unpack_tuple_helper<0>(args->elements(),
function_name,
min_size,
max_size,
unpacked_args,
std::forward<DefaultArgs>(default_values)...);
if (result.is_err()) return Err(result.unwrap_err());
return Ok(unpacked_args);
}
};
}// namespace py