When output_encoding is set, wraps the output buffer into a io.TextIOWrapper class
This means that the output_buffer has to be a io.IOBase instance
in order to work along with the TextIOWrapper
Handles wrapping when the buffer is Python2 stdtout, which has 'file' type
Adds test for this situation
+import io
import sys
from sqlalchemy import __version__ as sa_version
"""Create a base class with a metaclass."""
return meta("%sBase" % meta.__name__, (base,), {})
################################################
+
+if py2k:
+ def writable_buffer(file):
+ return io.FileIO(file.fileno(), 'w')
+else:
+ def writable_buffer(file):
+ return file
from sqlalchemy import create_engine
from sqlalchemy.engine import url as sqla_url
-from .compat import callable
+from .compat import callable, writable_buffer
from . import ddl, util
log = logging.getLogger(__name__)
self.connection = connection
self._migrations_fn = opts.get('fn')
self.as_sql = as_sql
+
self.output_buffer = opts.get("output_buffer", sys.stdout)
- if opts.get('output_encoding'):
+ if "output_encoding" in opts:
self.output_buffer = io.TextIOWrapper(
- self.output_buffer,
- opts['output_encoding']
- )
+ opts.get("output_buffer") or writable_buffer(sys.stdout),
+ opts.get('output_encoding')
+ )
self._user_compare_type = opts.get('compare_type', False)
self._user_compare_server_default = opts.get(
+import io
from unittest import TestCase
from alembic import command, util
def setUp(self):
env = staging_env()
self.cfg = _no_sql_testing_config()
+ self.cfg.output_buffer = io.StringIO()
global a, b, c
a, b, c = three_rev_fixture(self.cfg)
command.upgrade(self.cfg, a, sql=True)
command.downgrade(self.cfg, "%s:%s" % (b, a), sql=True)
command.current(self.cfg)
+ # current seems to close the buffer (?)
+ self.cfg.output_buffer = io.StringIO()
command.stamp(self.cfg, a)
def test_starting_rev_pre_context(self):
command.downgrade,
self.cfg, b, sql=True
)
+
+ def test_upgrade_with_output_encoding(self):
+ env_file_fixture("""
+url = config.get_main_option('sqlalchemy.url')
+context.configure(url=url, output_encoding='utf-8')
+assert not context.requires_connection()
+""")
+ command.upgrade(self.cfg, a, sql=True)
+ command.downgrade(self.cfg, "%s:%s" % (b, a), sql=True)