basestring and str are gone since Python 3

This commit is contained in:
Hong Minhee
2013-04-13 04:28:48 +09:00
parent 34b3aaf7e6
commit 3e0e2a8b77
6 changed files with 31 additions and 6 deletions

View File

@@ -1,6 +1,10 @@
"""Provide the 'autogenerate' feature which can produce migration operations
automatically."""
try:
import builtins
except ImportError:
import __builtin__ as builtins
import logging
import re
@@ -584,17 +588,18 @@ def _render_column(column, autogen_context):
}
def _render_server_default(default, autogen_context):
string_type = getattr(builtins, 'basestring', str)
rendered = _user_defined_render("server_default", default, autogen_context)
if rendered is not False:
return rendered
if isinstance(default, sa_schema.DefaultClause):
if isinstance(default.arg, basestring):
if isinstance(default.arg, string_type):
default = default.arg
else:
default = str(default.arg.compile(
dialect=autogen_context['dialect']))
if isinstance(default, basestring):
if isinstance(default, string_type):
# TODO: this is just a hack to get
# tests to pass until we figure out
# WTF sqlite is doing

View File

@@ -1,3 +1,8 @@
try:
import builtins
except ImportError:
import __builtin__ as builtins
from sqlalchemy.sql.expression import _BindParamClause
from sqlalchemy.ext.compiler import compiles
from sqlalchemy import schema, text
@@ -58,12 +63,13 @@ class DefaultImpl(ImplMeta('_ImplBase', (object,), {})):
def _exec(self, construct, execution_options=None,
multiparams=(),
params=util.immutabledict()):
if isinstance(construct, basestring):
if isinstance(construct, getattr(builtins, 'basestring', str)):
construct = text(construct)
if self.as_sql:
if multiparams or params:
# TODO: coverage
raise Exception("Execution arguments not allowed with as_sql")
unicode = getattr(builtins, 'unicode', str)
self.static_output(unicode(
construct.compile(dialect=self.dialect)
).replace("\t", " ").strip() + self.command_terminator)

View File

@@ -1,3 +1,8 @@
try:
import builtins
except ImportError:
import __builtin__ as builtins
from sqlalchemy.ext.compiler import compiles
from sqlalchemy import types as sqltypes
from sqlalchemy import schema
@@ -89,7 +94,7 @@ def _mysql_alter_column(element, compiler, **kw):
)
def _render_value(compiler, expr):
if isinstance(expr, basestring):
if isinstance(expr, getattr(builtins, 'basestring', str)):
return "'%s'" % expr
else:
return compiler.sql_compiler.process(expr)

View File

@@ -1,3 +1,7 @@
try:
import builtins
except ImportError:
import __builtin__ as builtins
from contextlib import contextmanager
from sqlalchemy.types import NULLTYPE, Integer
@@ -143,7 +147,7 @@ class Operations(object):
ForeignKey.
"""
if isinstance(fk._colspec, basestring):
if isinstance(fk._colspec, getattr(builtins, 'basestring', str)):
table_key, cname = fk._colspec.rsplit('.', 1)
sname, tname = self._parse_table_key(table_key)
if table_key not in metadata.tables:

View File

@@ -97,6 +97,7 @@ def _get_dialect(name):
def assert_compiled(element, assert_string, dialect=None):
dialect = _get_dialect(dialect)
unicode = getattr(builtins, 'unicode', str)
eq_(
unicode(element.compile(dialect=dialect)).\
replace("\n", "").replace("\t", ""),
@@ -160,6 +161,8 @@ def op_fixture(dialect='default', as_sql=False):
# as tests get more involved
self.connection = None
def _exec(self, construct, *args, **kw):
basestring = getattr(builtins, 'basestring', str)
unicode = getattr(builtins, 'unicode', str)
if isinstance(construct, basestring):
construct = text(construct)
sql = unicode(construct.compile(dialect=self.dialect))
@@ -315,6 +318,8 @@ def clear_staging_env():
def write_script(scriptdir, rev_id, content):
old = scriptdir._revision_map[rev_id]
path = old.path
if not hasattr(builtins, 'unicode') and isinstance(content, bytes):
content = content.decode()
with open(path, 'w') as fp:
fp.write(textwrap.dedent(content))
pyc_path = util.pyc_file_from_path(path)

View File

@@ -667,7 +667,7 @@ class AutogenerateDiffTest(AutogenTest, TestCase):
)
eq_(
[(rec[0], rec[1].name) for rec in diffs],
[('remove_table', 'extra'), ('remove_table', u'user')]
[('remove_table', 'extra'), ('remove_table', 'user')]
)
class AutogenKeyTest(AutogenTest, TestCase):