Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions changelog.d/19412.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bump `pyo3` from 0.26.0 to 0.27.2 and `pythonize` from 0.26.0 to 0.27.0. Contributed by @razvp @ ERCOM.
4 changes: 2 additions & 2 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ http = "1.1.0"
lazy_static = "1.4.0"
log = "0.4.17"
mime = "0.3.17"
pyo3 = { version = "0.26.0", features = [
pyo3 = { version = "0.27.2", features = [
"macros",
"anyhow",
"abi3",
"abi3-py310",
] }
pyo3-log = "0.13.1"
pythonize = "0.26.0"
pythonize = "0.27.0"
regex = "1.6.0"
sha2 = "0.10.8"
serde = { version = "1.0.144", features = ["derive"] }
Expand Down
14 changes: 7 additions & 7 deletions rust/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn read_io_body(body: &Bound<'_, PyAny>, chunk_size: usize) -> PyResult<Bytes> {
let mut buf = BytesMut::new();
loop {
let bound = &body.call_method1("read", (chunk_size,))?;
let bytes: &Bound<'_, PyBytes> = bound.downcast()?;
let bytes: &Bound<'_, PyBytes> = bound.cast()?;
if bytes.as_bytes().is_empty() {
return Ok(buf.into());
}
Expand All @@ -58,12 +58,12 @@ pub fn http_request_from_twisted(request: &Bound<'_, PyAny>) -> PyResult<Request
let mut req = Request::new(body);

let bound = &request.getattr("uri")?;
let uri: &Bound<'_, PyBytes> = bound.downcast()?;
let uri: &Bound<'_, PyBytes> = bound.cast()?;
*req.uri_mut() =
Uri::try_from(uri.as_bytes()).map_err(|_| PyValueError::new_err("invalid uri"))?;

let bound = &request.getattr("method")?;
let method: &Bound<'_, PyBytes> = bound.downcast()?;
let method: &Bound<'_, PyBytes> = bound.cast()?;
*req.method_mut() = Method::from_bytes(method.as_bytes())
.map_err(|_| PyValueError::new_err("invalid method"))?;

Expand All @@ -74,17 +74,17 @@ pub fn http_request_from_twisted(request: &Bound<'_, PyAny>) -> PyResult<Request

for header in headers_iter {
let header = header?;
let header: &Bound<'_, PyTuple> = header.downcast()?;
let header: &Bound<'_, PyTuple> = header.cast()?;
let bound = &header.get_item(0)?;
let name: &Bound<'_, PyBytes> = bound.downcast()?;
let name: &Bound<'_, PyBytes> = bound.cast()?;
let name = HeaderName::from_bytes(name.as_bytes())
.map_err(|_| PyValueError::new_err("invalid header name"))?;

let bound = &header.get_item(1)?;
let values: &Bound<'_, PySequence> = bound.downcast()?;
let values: &Bound<'_, PySequence> = bound.cast()?;
for index in 0..values.len()? {
let bound = &values.get_item(index)?;
let value: &Bound<'_, PyBytes> = bound.downcast()?;
let value: &Bound<'_, PyBytes> = bound.cast()?;
let value = HeaderValue::from_bytes(value.as_bytes())
.map_err(|_| PyValueError::new_err("invalid header value"))?;
req.headers_mut().append(name.clone(), value);
Expand Down
5 changes: 4 additions & 1 deletion rust/src/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,8 @@ fn make_deferred_yieldable<'py>(
func
});

make_deferred_yieldable.call1(py, (deferred,))?.extract(py)
make_deferred_yieldable
.call1(py, (deferred,))?
.extract(py)
.map_err(Into::into)
}
32 changes: 19 additions & 13 deletions rust/src/push/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,16 @@ pub enum SimpleJsonValue {
Null,
}

impl<'source> FromPyObject<'source> for SimpleJsonValue {
fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult<Self> {
if let Ok(s) = ob.downcast::<PyString>() {
impl<'source> FromPyObject<'_, 'source> for SimpleJsonValue {
type Error = PyErr;

fn extract(ob: Borrowed<'_, 'source, PyAny>) -> Result<Self, Self::Error> {
if let Ok(s) = ob.cast::<PyString>() {
Ok(SimpleJsonValue::Str(Cow::Owned(s.to_string())))
// A bool *is* an int, ensure we try bool first.
} else if let Ok(b) = ob.downcast::<PyBool>() {
} else if let Ok(b) = ob.cast::<PyBool>() {
Ok(SimpleJsonValue::Bool(b.extract()?))
} else if let Ok(i) = ob.downcast::<PyInt>() {
} else if let Ok(i) = ob.cast::<PyInt>() {
Ok(SimpleJsonValue::Int(i.extract()?))
} else if ob.is_none() {
Ok(SimpleJsonValue::Null)
Expand All @@ -301,20 +303,22 @@ pub enum JsonValue {
Value(SimpleJsonValue),
}

impl<'source> FromPyObject<'source> for JsonValue {
fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult<Self> {
if let Ok(l) = ob.downcast::<PyList>() {
impl<'source> FromPyObject<'_, 'source> for JsonValue {
type Error = PyErr;

fn extract(ob: Borrowed<'_, 'source, PyAny>) -> Result<Self, Self::Error> {
if let Ok(l) = ob.cast::<PyList>() {
match l
.iter()
.map(|it| SimpleJsonValue::extract_bound(&it))
.map(|it| SimpleJsonValue::extract(it.as_borrowed()))
.collect()
{
Ok(a) => Ok(JsonValue::Array(a)),
Err(e) => Err(PyTypeError::new_err(format!(
"Can't convert to JsonValue::Array: {e}"
))),
}
} else if let Ok(v) = SimpleJsonValue::extract_bound(ob) {
} else if let Ok(v) = SimpleJsonValue::extract(ob) {
Ok(JsonValue::Value(v))
} else {
Err(PyTypeError::new_err(format!(
Expand Down Expand Up @@ -385,9 +389,11 @@ impl<'source> IntoPyObject<'source> for Condition {
}
}

impl<'source> FromPyObject<'source> for Condition {
fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult<Self> {
Ok(depythonize(ob)?)
impl<'source> FromPyObject<'_, 'source> for Condition {
type Error = PyErr;

fn extract(ob: Borrowed<'_, 'source, PyAny>) -> Result<Self, Self::Error> {
Ok(depythonize(&ob)?)
}
}

Expand Down
Loading