Skip to content

Commit bbe98dd

Browse files
authored
Construct detailed message on text decode failure (RustPython#6014)
1 parent 5239549 commit bbe98dd

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

Lib/test/test_sqlite3/test_regression.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ def __conform__(self, protocol):
150150
with self.assertRaises(IndexError):
151151
con.execute("insert into foo(bar, baz) values (?, ?)", parameters)
152152

153-
# TODO: RUSTPYTHON
154-
@unittest.expectedFailure
155153
def test_error_msg_decode_error(self):
156154
# When porting the module to Python 3.0, the error message about
157155
# decoding errors disappeared. This verifies they're back again.

stdlib/src/sqlite.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,8 +1832,15 @@ mod _sqlite {
18321832
let text_factory = zelf.connection.text_factory.to_owned();
18331833

18341834
if text_factory.is(PyStr::class(&vm.ctx)) {
1835-
let text = String::from_utf8(text).map_err(|_| {
1836-
new_operational_error(vm, "not valid UTF-8".to_owned())
1835+
let text = String::from_utf8(text).map_err(|err| {
1836+
let col_name = st.column_name(i);
1837+
let col_name_str = ptr_to_str(col_name, vm).unwrap_or("?");
1838+
let valid_up_to = err.utf8_error().valid_up_to();
1839+
let text_prefix = String::from_utf8_lossy(&err.as_bytes()[..valid_up_to]);
1840+
let msg = format!(
1841+
"Could not decode to UTF-8 column '{col_name_str}' with text '{text_prefix}'"
1842+
);
1843+
new_operational_error(vm, msg)
18371844
})?;
18381845
vm.ctx.new_str(text).into()
18391846
} else if text_factory.is(PyBytes::class(&vm.ctx)) {

0 commit comments

Comments
 (0)