| 1545 | } |
| 1546 | |
| 1547 | static std::vector<tensorflow::int64> MakeIntList(PyObject* list) { |
| 1548 | if (list == Py_None) { |
| 1549 | return {}; |
| 1550 | } |
| 1551 | PyObject* seq = PySequence_Fast(list, "expected a sequence"); |
| 1552 | if (seq == nullptr) { |
| 1553 | return {}; |
| 1554 | } |
| 1555 | int len = PySequence_Size(list); |
| 1556 | std::vector<tensorflow::int64> tensor_ids; |
| 1557 | tensor_ids.reserve(len); |
| 1558 | for (int i = 0; i < len; ++i) { |
| 1559 | PyObject* item = PySequence_Fast_GET_ITEM(seq, i); |
| 1560 | #if PY_MAJOR_VERSION >= 3 |
| 1561 | if (PyLong_Check(item)) { |
| 1562 | #else |
| 1563 | if (PyLong_Check(item) || PyInt_Check(item)) { |
| 1564 | #endif |
| 1565 | tensorflow::int64 id = MakeInt(item); |
| 1566 | tensor_ids.push_back(id); |
| 1567 | } else { |
| 1568 | tensor_ids.push_back(-1); |
| 1569 | } |
| 1570 | } |
| 1571 | Py_DECREF(seq); |
| 1572 | return tensor_ids; |
| 1573 | } |
| 1574 | |
| 1575 | // Fill `tensor_ids` and `dtypes` from `tensors`, none of which may be |
| 1576 | // null. Returns true on success and false on a Python exception. |
| 1577 | bool TensorShapesAndDtypes(PyObject* tensors, |
| 1578 | std::vector<tensorflow::int64>* tensor_ids, |
| 1579 | std::vector<tensorflow::DataType>* dtypes) { |
| 1580 | tensorflow::Safe_PyObjectPtr seq( |
| 1581 | PySequence_Fast(tensors, "expected a sequence")); |
| 1582 | if (seq == nullptr) { |
| 1583 | return false; |
| 1584 | } |
| 1585 | int len = PySequence_Fast_GET_SIZE(seq.get()); |
| 1586 | tensor_ids->reserve(len); |
| 1587 | dtypes->reserve(len); |
| 1588 | for (int i = 0; i < len; ++i) { |
| 1589 | PyObject* item = PySequence_Fast_GET_ITEM(seq.get(), i); |
| 1590 | tensor_ids->push_back(FastTensorId(item)); |
| 1591 | dtypes->push_back(FastTensorDtype(item)); |
| 1592 | } |
| 1593 | return true; |
| 1594 | } |
| 1595 | |
| 1596 | bool TapeCouldPossiblyRecord(PyObject* tensors) { |
| 1597 | if (tensors == Py_None) { |
| 1598 | return false; |
| 1599 | } |
| 1600 | if (*ThreadTapeIsStopped()) { |
| 1601 | return false; |
| 1602 | } |
| 1603 | if (!HasTape()) { |
| 1604 | return false; |
no test coverage detected