| 26 | using functional::PyObjectPtr; |
| 27 | |
| 28 | std::string PyUnpack_String(PyObject* obj) { |
| 29 | CHECK_OR_THROW(PyUnicode_Check(obj)) << "PyUnpack_String(): expect a PyUnicode object"; |
| 30 | Py_ssize_t size = -1; |
| 31 | const char* data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 32 | CHECK_NOTNULL_OR_THROW(data) << "error unpacking string as utf-8"; |
| 33 | return std::string(data, (size_t)size); |
| 34 | } |
| 35 | |
| 36 | // For signature like Tensor.reshape(*shape), this function can handle these cases: |
| 37 | // 1. parse positional arguments only case, like Tensor.reshape(1, 2) |