|
1 | | -use crate::{err::PyErrArguments, exceptions, PyErr, Python}; |
| 1 | +use crate::{err::PyErrArguments, exceptions, types, PyErr, Python}; |
2 | 2 | use crate::{IntoPyObject, Py, PyAny}; |
3 | 3 | use std::io; |
4 | 4 |
|
@@ -118,23 +118,70 @@ macro_rules! impl_to_pyerr { |
118 | 118 | }; |
119 | 119 | } |
120 | 120 |
|
| 121 | +impl PyErrArguments for exceptions::Utf8ErrorWithBytes { |
| 122 | + fn arguments(self, py: Python<'_>) -> Py<PyAny> { |
| 123 | + let Self { err, bytes } = self; |
| 124 | + let start = err.valid_up_to(); |
| 125 | + let end = err.error_len().map_or(bytes.len(), |l| start + l); |
| 126 | + |
| 127 | + let encoding = types::PyString::new(py, "utf-8").into_any(); |
| 128 | + let bytes = types::PyBytes::new(py, &bytes).into_any(); |
| 129 | + let start = types::PyInt::new(py, start).into_any(); |
| 130 | + let end = types::PyInt::new(py, end).into_any(); |
| 131 | + let reason = types::PyString::new(py, "invalid utf-8").into_any(); |
| 132 | + |
| 133 | + // FIXME(icxolu) remove unwrap |
| 134 | + types::PyTuple::new(py, &[encoding, bytes, start, end, reason]) |
| 135 | + .unwrap() |
| 136 | + .into_any() |
| 137 | + .unbind() |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +impl std::convert::From<exceptions::Utf8ErrorWithBytes> for PyErr { |
| 142 | + fn from(err: exceptions::Utf8ErrorWithBytes) -> PyErr { |
| 143 | + exceptions::PyUnicodeDecodeError::new_err(err) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +impl PyErrArguments for std::string::FromUtf8Error { |
| 148 | + fn arguments(self, py: Python<'_>) -> Py<PyAny> { |
| 149 | + exceptions::Utf8ErrorWithBytes { |
| 150 | + err: self.utf8_error(), |
| 151 | + bytes: self.into_bytes(), |
| 152 | + } |
| 153 | + .arguments(py) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +impl std::convert::From<std::string::FromUtf8Error> for PyErr { |
| 158 | + fn from(err: std::string::FromUtf8Error) -> PyErr { |
| 159 | + exceptions::PyUnicodeDecodeError::new_err(err) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +impl PyErrArguments for std::ffi::IntoStringError { |
| 164 | + fn arguments(self, py: Python<'_>) -> Py<PyAny> { |
| 165 | + exceptions::Utf8ErrorWithBytes { |
| 166 | + err: self.utf8_error(), |
| 167 | + bytes: self.into_cstring().into_bytes(), |
| 168 | + } |
| 169 | + .arguments(py) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +impl std::convert::From<std::ffi::IntoStringError> for PyErr { |
| 174 | + fn from(err: std::ffi::IntoStringError) -> PyErr { |
| 175 | + exceptions::PyUnicodeDecodeError::new_err(err) |
| 176 | + } |
| 177 | +} |
| 178 | + |
121 | 179 | impl_to_pyerr!(std::array::TryFromSliceError, exceptions::PyValueError); |
122 | 180 | impl_to_pyerr!(std::num::ParseIntError, exceptions::PyValueError); |
123 | 181 | impl_to_pyerr!(std::num::ParseFloatError, exceptions::PyValueError); |
124 | 182 | impl_to_pyerr!(std::num::TryFromIntError, exceptions::PyValueError); |
125 | 183 | impl_to_pyerr!(std::str::ParseBoolError, exceptions::PyValueError); |
126 | | -impl_to_pyerr!(std::ffi::IntoStringError, exceptions::PyUnicodeDecodeError); |
127 | 184 | impl_to_pyerr!(std::ffi::NulError, exceptions::PyValueError); |
128 | | -impl_to_pyerr!(std::str::Utf8Error, exceptions::PyUnicodeDecodeError); |
129 | | -impl_to_pyerr!(std::string::FromUtf8Error, exceptions::PyUnicodeDecodeError); |
130 | | -impl_to_pyerr!( |
131 | | - std::string::FromUtf16Error, |
132 | | - exceptions::PyUnicodeDecodeError |
133 | | -); |
134 | | -impl_to_pyerr!( |
135 | | - std::char::DecodeUtf16Error, |
136 | | - exceptions::PyUnicodeDecodeError |
137 | | -); |
138 | 185 | impl_to_pyerr!(std::net::AddrParseError, exceptions::PyValueError); |
139 | 186 |
|
140 | 187 | #[cfg(test)] |
|
0 commit comments