Debian Codegraph @

README.md

raw symbols SCIP
1 # Rust compatible wrappers for file-like objects in Python
2
3 This crate provides a wrapper for file-like objects in Python that implement the `io`
4 protocol, and allows them to be used as `Read`, `Write`, and `Seek` traits in Rust.
5
6 Objects need to implement the `io` protocol. For the `Read` trait, the object must have a
7 `read` method that takes a single argument, the number of bytes to read, and returns a `bytes`
8 object. For the `Write` trait, the object must have a `write` method that takes a single
9 argument, a `bytes` object. For the `Seek` trait, the object must have a `seek` method that
10 takes two arguments, the offset and whence, and returns the new position.
11
12 The `mode` attribute is checked to ensure that the file is opened in binary mode.
13 If the `mode` attribute is not present, the file is assumed to be opened in binary mode.
14
15 The `AsFd` trait is implemented for Unix-like systems, allowing the file to be used with
16 functions that take a file descriptor. The `fileno` method is called to get the file
17 descriptor.
18
19 # Example
20
21 ```rust
22 use pyo3::prelude::*;
23 use std::io::{Read, Write};
24
25 pyo3::Python::attach(|py| -> PyResult<()> {
26 let io = py.import("io")?;
27 let file = io.call_method1("BytesIO", (&b"hello"[..], ))?;
28 let mut file = pyo3_filelike::PyBinaryFile::from(file);
29 let mut buf = [0u8; 5];
30 file.read_exact(&mut buf)?;
31 assert_eq!(&buf, b"hello");
32 Ok(())
33 }).unwrap();