Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
28 changes: 3 additions & 25 deletions Lib/sqlite3/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
# future enhancements, you should normally quote any identifier that
# is an English language word, even if you do not have to."


from contextlib import contextmanager


def _quote_name(name):
return '"{0}"'.format(name.replace('"', '""'))

Expand All @@ -19,24 +15,6 @@ def _quote_value(value):
return "'{0}'".format(value.replace("'", "''"))


def _force_decode(bs, *args, **kwargs):
# gh-108590: Don't fail if the database contains invalid Unicode data.
try:
return bs.decode(*args, **kwargs)
except UnicodeDecodeError:
return "".join([chr(c) for c in bs])


@contextmanager
def _text_factory(con, factory):
saved_factory = con.text_factory
con.text_factory = factory
try:
yield
finally:
con.text_factory = saved_factory


def _iterdump(connection):
"""
Returns an iterator to the dump of the database in an SQL text format.
Expand All @@ -47,6 +25,7 @@ def _iterdump(connection):
"""

writeable_schema = False
connection.text_factory = lambda x: str(x, errors='surrogateescape')
cu = connection.cursor()
yield('BEGIN TRANSACTION;')

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

# Now when the type is 'index', 'trigger', or 'view'
q = """
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_sqlite3/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_dump_unicode_invalid(self):
expected = [
"BEGIN TRANSACTION;",
"CREATE TABLE foo (data TEXT);",
"INSERT INTO \"foo\" VALUES('a\x9f');",
"INSERT INTO \"foo\" VALUES('a\udc9f');",
"COMMIT;",
]
self.cu.executescript("""
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
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
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 and Serhiy Storchaka.