Skip to content

Commit

Permalink
update pyo3 and add changelog entry
Browse files Browse the repository at this point in the history
  • Loading branch information
omerbenamram committed Apr 4, 2024
1 parent b6638e8 commit 3540ecb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.8.0] - 2024-04-04
- Update to PyO3 0.21

## [0.5.0] - 2022-04-16

- Update to PyO3 0.16
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ repository = "https://github.com/omerbenamram/pyo3-file"
license = "MIT/Apache-2.0"
readme = "README.md"

version = "0.7.0"
version = "0.8.0"
authors = ["Omer Ben-Amram <omerbenamram@gmail.com>"]
edition = "2018"

[dependencies]
pyo3 = { version = ">=0.18.1,<0.21" }
pyo3 = { version = ">=0.21,<0.22" }

[dev-dependencies]
skeptic = "0.13.7"
Expand Down
40 changes: 21 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pyo3::{exceptions::PyTypeError, prelude::*};

use pyo3::types::{PyBytes, PyString, PyType};
use pyo3::types::{PyBytes, PyString};

use std::io;
use std::io::{Read, Seek, SeekFrom, Write};
Expand All @@ -20,11 +20,10 @@ impl PyFileLikeObject {
/// instantiate it with `PyFileLikeObject::require`
pub fn new(object: PyObject) -> PyResult<Self> {
Python::with_gil(|py| {
let io = PyModule::import(py, "io")?;
let io = PyModule::import_bound(py, "io")?;
let text_io = io.getattr("TextIOBase")?;

let text_io_type = text_io.extract::<&PyType>()?;
let is_text_io = object.as_ref(py).is_instance(text_io_type)?;
let is_text_io = object.bind(py).is_instance(&text_io)?;

Ok(PyFileLikeObject {
inner: object,
Expand Down Expand Up @@ -86,7 +85,7 @@ fn pyerr_to_io_err(e: PyErr) -> io::Error {
Python::with_gil(|py| {
let e_as_object: PyObject = e.into_py(py);

match e_as_object.call_method(py, "__str__", (), None) {
match e_as_object.call_method_bound(py, "__str__", (), None) {
Ok(repr) => match repr.extract::<String>(py) {
Ok(s) => io::Error::new(io::ErrorKind::Other, s),
Err(_e) => io::Error::new(io::ErrorKind::Other, "An unknown error has occurred"),
Expand All @@ -108,23 +107,26 @@ impl Read for PyFileLikeObject {
}
let res = self
.inner
.call_method(py, "read", (buf.len() / 4,), None)
.call_method_bound(py, "read", (buf.len() / 4,), None)
.map_err(pyerr_to_io_err)?;
let pystring: &PyString = res
.downcast(py)
let pystring = res
.downcast_bound::<PyString>(py)
.expect("Expecting to be able to downcast into str from read result.");
let bytes = pystring.to_str().unwrap().as_bytes();

let rust_string = pystring.extract::<String>().unwrap();
let bytes = rust_string.as_bytes();
buf.write_all(bytes)?;
Ok(bytes.len())

} else {
let res = self
.inner
.call_method(py, "read", (buf.len(),), None)
.call_method_bound(py, "read", (buf.len(),), None)
.map_err(pyerr_to_io_err)?;
let pybytes: &PyBytes = res
.downcast(py)
let pybytes = res
.downcast_bound(py)
.expect("Expecting to be able to downcast into bytes from read result.");
let bytes = pybytes.as_bytes();
let bytes = pybytes.extract().unwrap();
buf.write_all(bytes)?;
Ok(bytes.len())
}
Expand All @@ -138,14 +140,14 @@ impl Write for PyFileLikeObject {
let arg = if self.is_text_io {
let s = std::str::from_utf8(buf)
.expect("Tried to write non-utf8 data to a TextIO object.");
PyString::new(py, s).to_object(py)
PyString::new_bound(py, s).to_object(py)
} else {
PyBytes::new(py, buf).to_object(py)
PyBytes::new_bound(py, buf).to_object(py)
};

let number_bytes_written = self
.inner
.call_method(py, "write", (arg,), None)
.call_method_bound(py, "write", (arg,), None)
.map_err(pyerr_to_io_err)?;

if number_bytes_written.is_none(py) {
Expand All @@ -162,7 +164,7 @@ impl Write for PyFileLikeObject {
fn flush(&mut self) -> Result<(), io::Error> {
Python::with_gil(|py| {
self.inner
.call_method(py, "flush", (), None)
.call_method_bound(py, "flush", (), None)
.map_err(pyerr_to_io_err)?;

Ok(())
Expand All @@ -181,7 +183,7 @@ impl Seek for PyFileLikeObject {

let new_position = self
.inner
.call_method(py, "seek", (offset, whence), None)
.call_method_bound(py, "seek", (offset, whence), None)
.map_err(pyerr_to_io_err)?;

new_position.extract(py).map_err(pyerr_to_io_err)
Expand All @@ -199,7 +201,7 @@ impl AsRawFd for PyFileLikeObject {
.expect("Object does not have a fileno() method.");

let fd = fileno
.call(py, (), None)
.call_bound(py, (), None)
.expect("fileno() method did not return a file descriptor.");

fd.extract(py).expect("File descriptor is not an integer.")
Expand Down

0 comments on commit 3540ecb

Please sign in to comment.