|
9 | 9 |
|
10 | 10 | namespace facebook::torchcodec { |
11 | 11 |
|
12 | | -AVIOFileLikeContext::AVIOFileLikeContext( |
13 | | - py::object fileLike, |
14 | | - std::string_view neededMethod) |
| 12 | +AVIOFileLikeContext::AVIOFileLikeContext(py::object fileLike, bool isForWriting) |
15 | 13 | : fileLike_{UniquePyObject(new py::object(fileLike))} { |
16 | 14 | { |
17 | 15 | // TODO: Is it necessary to acquire the GIL here? Is it maybe even |
18 | 16 | // harmful? At the moment, this is only called from within a pybind |
19 | 17 | // function, and pybind guarantees we have the GIL. |
20 | 18 | py::gil_scoped_acquire gil; |
21 | | - TORCH_CHECK( |
22 | | - py::hasattr(fileLike, neededMethod.data()), |
23 | | - "File like object must implement a ", |
24 | | - neededMethod, |
25 | | - " method."); |
| 19 | + |
| 20 | + if (isForWriting) { |
| 21 | + TORCH_CHECK( |
| 22 | + py::hasattr(fileLike, "write"), |
| 23 | + "File like object must implement a write method for writing."); |
| 24 | + } else { |
| 25 | + TORCH_CHECK( |
| 26 | + py::hasattr(fileLike, "read"), |
| 27 | + "File like object must implement a read method for reading."); |
| 28 | + } |
| 29 | + |
26 | 30 | TORCH_CHECK( |
27 | 31 | py::hasattr(fileLike, "seek"), |
28 | 32 | "File like object must implement a seek method."); |
29 | 33 | } |
30 | | - createAVIOContext(&read, &write, &seek, &fileLike_); |
| 34 | + createAVIOContext(&read, &write, &seek, &fileLike_, isForWriting); |
31 | 35 | } |
32 | 36 |
|
33 | 37 | int AVIOFileLikeContext::read(void* opaque, uint8_t* buf, int buf_size) { |
|
0 commit comments