1 #include <torch/csrc/utils/tuple_parser.h> 4 #include <torch/csrc/DynamicTypes.h> 5 #include <torch/csrc/autograd/python_variable.h> 6 #include <torch/csrc/utils/python_strings.h> 7 #include <torch/csrc/utils/python_numbers.h> 15 TupleParser::TupleParser(PyObject* args,
int num_args) : args(args), idx(0) {
16 int size = (int) PyTuple_GET_SIZE(args);
17 if (num_args >= 0 && size != num_args) {
18 std::string msg(
"missing required arguments (expected ");
19 msg += std::to_string(num_args) +
" got " + std::to_string(size) +
")";
20 throw std::runtime_error(msg);
24 auto TupleParser::parse(
bool& x,
const std::string& param_name) ->
void {
25 PyObject* obj = next_arg();
26 if (!PyBool_Check(obj)) {
27 throw invalid_type(
"bool", param_name);
32 auto TupleParser::parse(
int& x,
const std::string& param_name) ->
void {
33 PyObject* obj = next_arg();
34 if (!THPUtils_checkLong(obj)) {
35 throw invalid_type(
"int", param_name);
37 x = THPUtils_unpackLong(obj);
40 auto TupleParser::parse(
double& x,
const std::string& param_name) ->
void {
41 PyObject* obj = next_arg();
42 if (!THPUtils_checkDouble(obj)) {
43 throw invalid_type(
"float", param_name);
45 x = THPUtils_unpackDouble(obj);
48 auto TupleParser::parse(std::vector<int>& x,
const std::string& param_name) ->
void {
49 PyObject* obj = next_arg();
50 if (!PyTuple_Check(obj)) {
51 throw invalid_type(
"tuple of int", param_name);
53 int size = PyTuple_GET_SIZE(obj);
55 for (
int i = 0; i < size; ++i) {
56 PyObject* item = PyTuple_GET_ITEM(obj, i);
57 if (!THPUtils_checkLong(item)) {
58 throw invalid_type(
"tuple of int", param_name);
60 x[i] = THPUtils_unpackLong(item);
64 auto TupleParser::parse(std::string& x,
const std::string& param_name) ->
void {
65 PyObject* obj = next_arg();
66 if (!THPUtils_checkString(obj)) {
67 throw invalid_type(
"bytes/str", param_name);
69 x = THPUtils_unpackString(obj);
72 auto TupleParser::next_arg() -> PyObject* {
73 if (idx >= PyTuple_GET_SIZE(args)) {
74 throw std::runtime_error(
"out of range");
76 return PyTuple_GET_ITEM(args, idx++);
79 auto TupleParser::invalid_type(
const std::string& expected,
const std::string& param_name) -> std::runtime_error {
80 std::string msg(
"argument ");
81 msg += std::to_string(idx - 1);
88 PyObject* obj = PyTuple_GET_ITEM(args, idx -1);
89 if (PyTuple_Check(obj)){
90 msg +=
" but got tuple of (";
91 int size = PyTuple_GET_SIZE(obj);
92 for (
int i = 0; i < size; ++i) {
93 msg += Py_TYPE(PyTuple_GET_ITEM(obj, i))->tp_name;
102 msg += Py_TYPE(PyTuple_GET_ITEM(args, idx - 1))->tp_name;
104 return std::runtime_error(msg);