Skip to content

de: fix bytebuf - #131

Open
Finomnis wants to merge 3 commits into
davidhewitt:mainfrom
Finomnis:fix_bytebuf
Open

Finomnis wants to merge 3 commits into
davidhewitt:mainfrom
Finomnis:fix_bytebuf

Conversation

@Finomnis

Copy link
Copy Markdown

Fixes #130

@davidhewitt davidhewitt left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - suggested a simplification that will make the extraction order "standard"

Comment thread src/de.rs Outdated
Comment on lines +226 to +231
if let Ok(b) = self.input.cast::<PyByteArray>() {
visitor.visit_byte_buf(b.to_vec())
} else {
let b = self.input.cast::<PyBytes>()?;
visitor.visit_bytes(b.as_bytes())
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should do self.input.extract::<PyBackedBytes>(), using https://docs.rs/pyo3/latest/pyo3/pybacked/struct.PyBackedBytes.html as the intermediate - this'll handle both cases in a "standard" way.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly won't get to work on this for a couple of days, feel free to commit to this branch if you have improvements

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure how that would make the code better or cleaner, would you mind explaining your thoughts? I'm probably not knowledged enough about pyo3.

@Finomnis Finomnis Nov 29, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to understand by letting claude explain what you might mean and claude says the advantage is less code, but it's still preferred to keep it as is because bytearray to pybackedbytes to visit_bytes would copy the data twice, vs the current implementation which only copies once

Quote:

PyBackedBytes can't hand its buffer over as a Vec, so for bytearray → owned targets like ByteBuf it would copy twice (once into the backed storage, once in the visitor). The explicit branch copies once and stays zero-copy for bytes.

If you are fine with the potential double copy and prefer a unified code path though I'm happy to change it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, there's no way to feed a pybackedbytes into visit_byte_buf without a copy, while the current implementation does allow that, which allows visitor implementations to be more efficient if they need the owned Vec

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, you're right, I missed that they need the owned vec.

What I was thinking the "standard extract" would achieve is to try bytes first (that's presumably more likely) and then give an error which hints at both types:

if let Ok(bytes) = obj.cast::<PyBytes>() {
    // handle byes
} else if let Ok(bytearray) = obj.cast::<PyByteArray>() {
    // handle bytearray
} else {
    Err(CastIntoError::new(
        obj,
        PyTuple::new(
            obj.py(),
            [
                PyBytes::type_object(obj.py()),
                PyByteArray::type_object(obj.py()),
            ],
        )
        .unwrap()
        .into_any(),
    ))
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fully agree. Also don't like the error right now. The only reason I tried bytearray first is so the current bytes error does not change. But your version is better.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe fixed :) (edited from phone, didn't run, hopefully no syntax error)

@Finomnis Finomnis Nov 29, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend to squash merge, my commit history is a mess :D

@Finomnis Finomnis Dec 2, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidhewitt Fixed and squashed. Sorry for the delay, very busy days.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

depythonize rejects bytearray for bytes targets, although deserialize_any routes it to deserialize_bytes

2 participants