Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyUnicodeDecodeError::new_utf8 raises TypeError on PyPy #2770

Closed
samuelcolvin opened this issue Nov 22, 2022 · 2 comments · Fixed by #2772
Closed

PyUnicodeDecodeError::new_utf8 raises TypeError on PyPy #2770

samuelcolvin opened this issue Nov 22, 2022 · 2 comments · Fixed by #2772
Labels

Comments

@samuelcolvin
Copy link
Contributor

Bug Description

PyUnicodeDecodeError::new_utf8(py, py_bytes.as_bytes(), err)?;

Appears to be raising/returning a TypeError, on PyPy, specifically

TypeError: expected str, got NoneType object

Here's my full code in case it helps:

pub fn py_bytes_to_str(py_bytes: &PyBytes) -> PyResult<&str> {
    match from_utf8(py_bytes.as_bytes()) {
        Ok(s) => Ok(s),
        Err(err) => {
            let py = py_bytes.py();
            let decode_err = PyUnicodeDecodeError::new_utf8(py, py_bytes.as_bytes(), err)?;
            Err(PyErr::from_value(decode_err))
        }
    }
}

Steps to Reproduce

Run the above code on PyPy, with bytes that raise an error like b'\x81'.

Backtrace

No response

Your operating system and version

Ubuntu

Your Python version (python --version)

Python 3.9.12 (05fbe3aa5b0845e6c37239768aa455451aa5faba, Mar 29 2022, 08:15:34) [PyPy 7.3.9 with GCC 10.2.1 20210130 (Red Hat 10.2.1-11)]

Your Rust version (rustc --version)

rustc 1.67.0-nightly (c1a859b25 2022-11-10)

Your PyO3 version

pyo3 = "0.17.3"

How did you install python? Did you use a virtualenv?

pyenv, virtualenv yes

Additional Info

No response

@samuelcolvin
Copy link
Contributor Author

samuelcolvin commented Nov 22, 2022

Maybe UnicodeDecodeError expects reason as a last argument? But that's not passed:

pub unsafe fn PyUnicodeDecodeError_Create(
encoding: *const c_char,
object: *const c_char,
length: Py_ssize_t,
start: Py_ssize_t,
end: Py_ssize_t,
_reason: *const c_char,
) -> *mut PyObject {
crate::PyObject_CallFunction(
PyExc_UnicodeDecodeError,
std::ffi::CStr::from_bytes_with_nul(b"sy#nns\0")
.unwrap()
.as_ptr(),
encoding,
object,
length,
start,
end,
)
}

@mejrs
Copy link
Member

mejrs commented Nov 22, 2022

I think that Py_BuildValue format string is wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants