Skip to content

gh-108590: Fix sqlite3.iterdump for invalid unicode in text columns. #108657

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

Merged
merged 7 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
23 changes: 21 additions & 2 deletions Lib/sqlite3/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ def _quote_name(name):
def _quote_value(value):
return "'{0}'".format(value.replace("'", "''"))

def _force_decode(bs, *args, **kwargs):
try:
return bs.decode(*args, **kwargs)
except UnicodeDecodeError:
return "".join([chr(c) for c in bs])

class _ctx_text_factory:
def __init__(self, connection, text_factory):
self.connection = connection
self.text_factory = text_factory
self.orig_text_factory = None

def __enter__(self):
self.orig_text_factory = self.connection.text_factory
self.connection.text_factory = self.text_factory

def __exit__(self, type, value, tb):
self.connection.text_factory = self.orig_text_factory

def _iterdump(connection):
"""
Expand Down Expand Up @@ -74,8 +92,9 @@ def _iterdump(connection):
)
)
query_res = cu.execute(q)
for row in query_res:
yield("{0};".format(row[0]))
with _ctx_text_factory(connection, bytes):
for row in query_res:
yield("{0};".format(_force_decode(row[0])))

# Now when the type is 'index', 'trigger', or 'view'
q = """
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_sqlite3/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ def test_dump_virtual_tables(self):
actual = list(self.cx.iterdump())
self.assertEqual(expected, actual)

def test_dump_unicode_invalid(self):
# gh-108590
expected = [
"BEGIN TRANSACTION;",
"CREATE TABLE foo (data TEXT);",
"INSERT INTO \"foo\" VALUES('a\x9f');",
"COMMIT;",
]
self.cu.executescript("""
CREATE TABLE foo (data TEXT);
INSERT INTO foo VALUES (CAST(X'619f' AS TEXT));
""")
actual = list(self.cx.iterdump())
self.assertEqual(expected, actual)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed an issue where :meth:`sqlite3.Connection.iterdump` would fail and leave an incomplete SQL dump if a table includes invalid Unicode sequences. Patch by Corvin McPherson