| 480 | } |
| 481 | |
| 482 | py::object _astensor1d_cpp( |
| 483 | py::handle value, py::handle dtype, py::handle device, py::handle ref) { |
| 484 | py::object ret; |
| 485 | py::object device_obj = py::none(); |
| 486 | py::object ndim_obj = py::none(); |
| 487 | if (device.ptr() != Py_None) { |
| 488 | device_obj = device2obj(device); |
| 489 | } |
| 490 | |
| 491 | if (PyObject_TypeCheck(value.ptr(), py_varnode_type)) { |
| 492 | try { |
| 493 | getattr(value, "ndim"); |
| 494 | } catch (py::error_already_set& err) { |
| 495 | if (dtype.ptr() != Py_None) { |
| 496 | ret = _astype_cpp(value, dtype); |
| 497 | } else { |
| 498 | ret = py::reinterpret_borrow<py::object>(value); |
| 499 | } |
| 500 | if (device.ptr() != Py_None) { |
| 501 | std::shared_ptr<OpDef> op = Copy::make(device_obj.cast<CompNode>()); |
| 502 | py::object Op = py::cast(op); |
| 503 | PyObject* p[2] = {Op.ptr(), ret.ptr()}; |
| 504 | py::tuple copy_ret = |
| 505 | py::reinterpret_steal<py::object>(py_apply(NULL, p, 2)); |
| 506 | return copy_ret[0]; |
| 507 | } |
| 508 | return ret; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | size_t ndim = 999; |
| 513 | if (hasattr(value, "ndim")) { |
| 514 | ndim = getattr(value, "ndim").cast<size_t>(); |
| 515 | if (ndim != 0 && ndim != 1) { |
| 516 | throw py::value_error("ndim != 1 or 0, get : " + std::to_string(ndim)); |
| 517 | } |
| 518 | if (!is_tensor(value)) { |
| 519 | return get_res_by_refhdl(value, dtype, device, ref); |
| 520 | } else { |
| 521 | return py::reinterpret_borrow<py::object>(value); |
| 522 | } |
| 523 | } |
| 524 | if (!is_py_sequence(value)) { |
| 525 | throw py::type_error(); |
| 526 | } |
| 527 | py::list lis = py::reinterpret_steal<py::list>(PySequence_List(value.ptr())); |
| 528 | bool need_concat = false; |
| 529 | for (size_t i = 0; i < lis.size(); ++i) { |
| 530 | if (is_tensor(lis[i])) { |
| 531 | need_concat = true; |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | if (!need_concat) { |
| 536 | return get_res_by_refhdl(value, dtype, device, ref); |
| 537 | } |
| 538 | if (lis.size() > 1) { |
| 539 | py::list flat_list; |
no test coverage detected