| 769 | }; |
| 770 | |
| 771 | static int EagerTensor_getbuffer(EagerTensor* self, Py_buffer* view, |
| 772 | int flags) { |
| 773 | if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) { |
| 774 | PyErr_SetString(PyExc_BufferError, "EagerTensor is not writable."); |
| 775 | return -1; |
| 776 | } |
| 777 | |
| 778 | // TensorHandleToNumpy is zero-copy for everything but DT_RESOURCE and |
| 779 | // DT_STRING so the following is only slightly slower than a NumPy-free |
| 780 | // implementation. |
| 781 | auto py_array = tensorflow::make_safe( |
| 782 | TFE_TensorHandleToNumpy(self->handle, self->status)); |
| 783 | if (MaybeRaiseExceptionFromTFStatus(self->status, PyExc_BufferError)) { |
| 784 | // Cleanup self->status before returning. |
| 785 | TF_SetStatus(self->status, TF_OK, ""); |
| 786 | return -1; |
| 787 | } |
| 788 | if (PyObject_GetBuffer(py_array.get(), view, flags) < 0) { |
| 789 | return -1; |
| 790 | } |
| 791 | view->readonly = 1; |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | static PyBufferProcs EagerTensor_as_buffer = { |
| 796 | #if PY_MAJOR_VERSION < 3 |
nothing calls this directly
no test coverage detected