Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 15 additions & 6 deletions Lib/sqlite3/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ def _force_decode(bs, *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):
"""
Returns an iterator to the dump of the database in an SQL text format.
Expand Down Expand Up @@ -78,14 +91,10 @@ def _iterdump(connection):
"||quote({0})||".format(_quote_name(col)) for col in column_names
)
)
orig_text_factory = connection.text_factory
try:
connection.text_factory = bytes
query_res = cu.execute(q)
query_res = cu.execute(q)
with _ctx_text_factory(connection, bytes):
for row in query_res:
yield("{0};".format(_force_decode(row[0])))
finally:
connection.text_factory = orig_text_factory

# Now when the type is 'index', 'trigger', or 'view'
q = """
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_sqlite3/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def test_dump_virtual_tables(self):
self.assertEqual(expected, actual)

def test_dump_unicode_invalid(self):
# gh-108590
expected = [
"BEGIN TRANSACTION;",
"CREATE TABLE foo (data TEXT);",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Fixed an issue where :meth:`sqlite3.Connection.iterdump` would fail and leave an incomplete SQL dump if a table includes invalid Unicode sequences.
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