| 519 | } |
| 520 | |
| 521 | std::vector<int> CastPyArg2VectorOfInt(PyObject* obj, size_t arg_pos) { |
| 522 | std::vector<int> result; |
| 523 | if (PyList_Check(obj)) { |
| 524 | Py_ssize_t len = PyList_Size(obj); |
| 525 | PyObject* item = nullptr; |
| 526 | for (Py_ssize_t i = 0; i < len; i++) { |
| 527 | item = PyList_GET_ITEM(obj, i); |
| 528 | if (PyObject_CheckLong(item)) { |
| 529 | result.emplace_back(PyObject_ToInt32(item)); |
| 530 | } else { |
| 531 | PADDLE_THROW(common::errors::InvalidType( |
| 532 | "argument (position %d) must be " |
| 533 | "list of int, but got %s at pos %d", |
| 534 | arg_pos + 1, |
| 535 | reinterpret_cast<PyTypeObject*>(item->ob_type)->tp_name, |
| 536 | i)); |
| 537 | } |
| 538 | } |
| 539 | } else if (PyTuple_Check(obj)) { |
| 540 | Py_ssize_t len = PyTuple_Size(obj); |
| 541 | PyObject* item = nullptr; |
| 542 | for (Py_ssize_t i = 0; i < len; i++) { |
| 543 | item = PyTuple_GET_ITEM(obj, i); |
| 544 | if (PyObject_CheckLong(item)) { |
| 545 | result.emplace_back(PyObject_ToInt32(item)); |
| 546 | } else { |
| 547 | PADDLE_THROW(common::errors::InvalidType( |
| 548 | "argument (position %d) must be " |
| 549 | "list of int, but got %s at pos %d", |
| 550 | arg_pos + 1, |
| 551 | reinterpret_cast<PyTypeObject*>(item->ob_type)->tp_name, |
| 552 | i)); |
| 553 | } |
| 554 | } |
| 555 | } else if (obj == Py_None) { |
| 556 | return {}; |
| 557 | } else if (PyObject_CheckLong(obj)) { |
| 558 | return {PyObject_ToInt32(obj)}; |
| 559 | } else { |
| 560 | PADDLE_THROW(common::errors::InvalidType( |
| 561 | "argument (position %d) must be " |
| 562 | "list or tuple, but got %s", |
| 563 | arg_pos + 1, |
| 564 | reinterpret_cast<PyTypeObject*>(obj->ob_type)->tp_name)); |
| 565 | } |
| 566 | return result; |
| 567 | } |
| 568 | |
| 569 | std::vector<int64_t> CastPyArg2VectorOfInt64(PyObject* obj, size_t arg_pos) { |
| 570 | std::vector<int64_t> result; |
no test coverage detected