Skip to content
Merged
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
Prev Previous commit
Next Next commit
Use contextmanager
  • Loading branch information
CorvinM committed Aug 30, 2023
commit cc7fba8aa17e7a7cece54f87111d3f56967a43f0
24 changes: 11 additions & 13 deletions Lib/sqlite3/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# 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 @@ -20,18 +22,14 @@ 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
@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):
"""
Expand Down Expand Up @@ -92,7 +90,7 @@ def _iterdump(connection):
)
)
query_res = cu.execute(q)
with _ctx_text_factory(connection, bytes):
with _text_factory(connection, bytes):
for row in query_res:
yield("{0};".format(_force_decode(row[0])))
Comment thread
CorvinM marked this conversation as resolved.

Expand Down