summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/engine
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-06-03 13:13:16 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2013-06-03 13:13:16 -0400
commit74c98bf182a1cac1ca1837da69e1c0550beaaab5 (patch)
tree4815d198d2aa4a6497330fb5d81e53bf4acfbb2d /lib/sqlalchemy/engine
parentff399ac75074916045410cedae72489cb60e8b50 (diff)
parentc2a158c137ee07a146f02e5ee89ec42e486c6a37 (diff)
downloadsqlalchemy-74c98bf182a1cac1ca1837da69e1c0550beaaab5.tar.gz
Merge branch 'master' into ticket_1068
Diffstat (limited to 'lib/sqlalchemy/engine')
-rw-r--r--lib/sqlalchemy/engine/base.py38
-rw-r--r--lib/sqlalchemy/engine/ddl.py4
-rw-r--r--lib/sqlalchemy/engine/default.py55
-rw-r--r--lib/sqlalchemy/engine/reflection.py21
-rw-r--r--lib/sqlalchemy/engine/result.py38
-rw-r--r--lib/sqlalchemy/engine/strategies.py20
-rw-r--r--lib/sqlalchemy/engine/url.py13
7 files changed, 87 insertions, 102 deletions
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py
index b4c9b1e1c..2d9f3af94 100644
--- a/lib/sqlalchemy/engine/base.py
+++ b/lib/sqlalchemy/engine/base.py
@@ -3,13 +3,13 @@
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
-
+from __future__ import with_statement
"""Defines :class:`.Connection` and :class:`.Engine`.
"""
-from __future__ import with_statement
+
import sys
from .. import exc, schema, util, log, interfaces
from ..sql import expression, util as sql_util
@@ -460,7 +460,7 @@ class Connection(Connectable):
try:
self.engine.dialect.do_begin(self.connection)
- except Exception, e:
+ except Exception as e:
self._handle_dbapi_exception(e, None, None, None, None)
def _rollback_impl(self):
@@ -473,7 +473,7 @@ class Connection(Connectable):
try:
self.engine.dialect.do_rollback(self.connection)
self.__transaction = None
- except Exception, e:
+ except Exception as e:
self._handle_dbapi_exception(e, None, None, None, None)
else:
self.__transaction = None
@@ -487,7 +487,7 @@ class Connection(Connectable):
try:
self.engine.dialect.do_commit(self.connection)
self.__transaction = None
- except Exception, e:
+ except Exception as e:
self._handle_dbapi_exception(e, None, None, None, None)
def _savepoint_impl(self, name=None):
@@ -688,7 +688,7 @@ class Connection(Connectable):
dialect = self.dialect
ctx = dialect.execution_ctx_cls._init_default(
dialect, self, conn)
- except Exception, e:
+ except Exception as e:
self._handle_dbapi_exception(e, None, None, None, None)
ret = ctx._exec_default(default, None)
@@ -734,6 +734,8 @@ class Connection(Connectable):
distilled_params = _distill_params(multiparams, params)
if distilled_params:
+ # note this is usually dict but we support RowProxy
+ # as well; but dict.keys() as an iterator is OK
keys = distilled_params[0].keys()
else:
keys = []
@@ -822,7 +824,7 @@ class Connection(Connectable):
conn = self._revalidate_connection()
context = constructor(dialect, self, conn, *args)
- except Exception, e:
+ except Exception as e:
self._handle_dbapi_exception(e,
str(statement), parameters,
None, None)
@@ -865,7 +867,7 @@ class Connection(Connectable):
statement,
parameters,
context)
- except Exception, e:
+ except Exception as e:
self._handle_dbapi_exception(
e,
statement,
@@ -939,7 +941,7 @@ class Connection(Connectable):
cursor,
statement,
parameters)
- except Exception, e:
+ except Exception as e:
self._handle_dbapi_exception(
e,
statement,
@@ -954,17 +956,11 @@ class Connection(Connectable):
"""
try:
cursor.close()
- except Exception, e:
- try:
- ex_text = str(e)
- except TypeError:
- ex_text = repr(e)
- if not self.closed:
- self.connection._logger.warn(
- "Error closing cursor: %s", ex_text)
-
- if isinstance(e, (SystemExit, KeyboardInterrupt)):
- raise
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except Exception:
+ self.connection._logger.error(
+ "Error closing cursor", exc_info=True)
_reentrant_error = False
_is_disconnect = False
@@ -1045,7 +1041,7 @@ class Connection(Connectable):
Compiled: _execute_compiled,
schema.SchemaItem: _execute_default,
schema.DDLElement: _execute_ddl,
- basestring: _execute_text
+ util.string_types[0]: _execute_text
}
def default_schema_name(self):
diff --git a/lib/sqlalchemy/engine/ddl.py b/lib/sqlalchemy/engine/ddl.py
index c61a9d59c..6daa9be6b 100644
--- a/lib/sqlalchemy/engine/ddl.py
+++ b/lib/sqlalchemy/engine/ddl.py
@@ -52,7 +52,7 @@ class SchemaGenerator(DDLBase):
if self.tables is not None:
tables = self.tables
else:
- tables = metadata.tables.values()
+ tables = list(metadata.tables.values())
collection = [t for t in sql_util.sort_tables(tables)
if self._can_create_table(t)]
seq_coll = [s for s in metadata._sequences.values()
@@ -120,7 +120,7 @@ class SchemaDropper(DDLBase):
if self.tables is not None:
tables = self.tables
else:
- tables = metadata.tables.values()
+ tables = list(metadata.tables.values())
collection = [
t
diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py
index dc45e12b1..91869ab75 100644
--- a/lib/sqlalchemy/engine/default.py
+++ b/lib/sqlalchemy/engine/default.py
@@ -59,17 +59,16 @@ class DefaultDialect(interfaces.Dialect):
# *not* the FLOAT type however.
supports_native_decimal = False
- # Py3K
- #supports_unicode_statements = True
- #supports_unicode_binds = True
- #returns_unicode_strings = True
- #description_encoding = None
- # Py2K
- supports_unicode_statements = False
- supports_unicode_binds = False
- returns_unicode_strings = False
- description_encoding = 'use_encoding'
- # end Py2K
+ if util.py3k:
+ supports_unicode_statements = True
+ supports_unicode_binds = True
+ returns_unicode_strings = True
+ description_encoding = None
+ else:
+ supports_unicode_statements = False
+ supports_unicode_binds = False
+ returns_unicode_strings = False
+ description_encoding = 'use_encoding'
name = 'default'
@@ -203,14 +202,10 @@ class DefaultDialect(interfaces.Dialect):
return None
def _check_unicode_returns(self, connection):
- # Py2K
- if self.supports_unicode_statements:
- cast_to = unicode
+ if util.py2k and not self.supports_unicode_statements:
+ cast_to = util.binary_type
else:
- cast_to = str
- # end Py2K
- # Py3K
- #cast_to = str
+ cast_to = util.text_type
def check_unicode(formatstr, type_):
cursor = connection.connection.cursor()
@@ -219,8 +214,8 @@ class DefaultDialect(interfaces.Dialect):
cursor.execute(
cast_to(
expression.select(
- [expression.cast(
- expression.literal_column(
+ [expression.cast(
+ expression.literal_column(
"'test %s returns'" % formatstr),
type_)
]).compile(dialect=self)
@@ -228,8 +223,8 @@ class DefaultDialect(interfaces.Dialect):
)
row = cursor.fetchone()
- return isinstance(row[0], unicode)
- except self.dbapi.Error, de:
+ return isinstance(row[0], util.text_type)
+ except self.dbapi.Error as de:
util.warn("Exception attempting to "
"detect unicode returns: %r" % de)
return False
@@ -375,10 +370,10 @@ class DefaultExecutionContext(interfaces.ExecutionContext):
self.execution_options.update(connection._execution_options)
if not dialect.supports_unicode_statements:
- self.unicode_statement = unicode(compiled)
+ self.unicode_statement = util.text_type(compiled)
self.statement = dialect._encoder(self.unicode_statement)[0]
else:
- self.statement = self.unicode_statement = unicode(compiled)
+ self.statement = self.unicode_statement = util.text_type(compiled)
self.cursor = self.create_cursor()
self.compiled_parameters = []
@@ -416,7 +411,7 @@ class DefaultExecutionContext(interfaces.ExecutionContext):
self.result_map = compiled.result_map
- self.unicode_statement = unicode(compiled)
+ self.unicode_statement = util.text_type(compiled)
if not dialect.supports_unicode_statements:
self.statement = self.unicode_statement.encode(
self.dialect.encoding)
@@ -521,7 +516,7 @@ class DefaultExecutionContext(interfaces.ExecutionContext):
self.executemany = len(parameters) > 1
if not dialect.supports_unicode_statements and \
- isinstance(statement, unicode):
+ isinstance(statement, util.text_type):
self.unicode_statement = statement
self.statement = dialect._encoder(statement)[0]
else:
@@ -575,8 +570,8 @@ class DefaultExecutionContext(interfaces.ExecutionContext):
"""
conn = self.root_connection
- if isinstance(stmt, unicode) and \
- not self.dialect.supports_unicode_statements:
+ if isinstance(stmt, util.text_type) and \
+ not self.dialect.supports_unicode_statements:
stmt = self.dialect._encoder(stmt)[0]
if self.dialect.positional:
@@ -736,7 +731,7 @@ class DefaultExecutionContext(interfaces.ExecutionContext):
inputsizes.append(dbtype)
try:
self.cursor.setinputsizes(*inputsizes)
- except Exception, e:
+ except Exception as e:
self.root_connection._handle_dbapi_exception(
e, None, None, None, self)
else:
@@ -754,7 +749,7 @@ class DefaultExecutionContext(interfaces.ExecutionContext):
inputsizes[key] = dbtype
try:
self.cursor.setinputsizes(**inputsizes)
- except Exception, e:
+ except Exception as e:
self.root_connection._handle_dbapi_exception(
e, None, None, None, self)
diff --git a/lib/sqlalchemy/engine/reflection.py b/lib/sqlalchemy/engine/reflection.py
index 90f21db09..cf2caf679 100644
--- a/lib/sqlalchemy/engine/reflection.py
+++ b/lib/sqlalchemy/engine/reflection.py
@@ -41,8 +41,12 @@ def cache(fn, self, con, *args, **kw):
return fn(self, con, *args, **kw)
key = (
fn.__name__,
- tuple(a for a in args if isinstance(a, basestring)),
- tuple((k, v) for k, v in kw.iteritems() if isinstance(v, (basestring, int, float)))
+ tuple(a for a in args if isinstance(a, util.string_types)),
+ tuple((k, v) for k, v in kw.items() if
+ isinstance(v,
+ util.string_types + util.int_types + (float, )
+ )
+ )
)
ret = info_cache.get(key)
if ret is None:
@@ -381,16 +385,15 @@ class Inspector(object):
# table.kwargs will need to be passed to each reflection method. Make
# sure keywords are strings.
tblkw = table.kwargs.copy()
- for (k, v) in tblkw.items():
+ for (k, v) in list(tblkw.items()):
del tblkw[k]
tblkw[str(k)] = v
- # Py2K
- if isinstance(schema, str):
- schema = schema.decode(dialect.encoding)
- if isinstance(table_name, str):
- table_name = table_name.decode(dialect.encoding)
- # end Py2K
+ if util.py2k:
+ if isinstance(schema, str):
+ schema = schema.decode(dialect.encoding)
+ if isinstance(table_name, str):
+ table_name = table_name.decode(dialect.encoding)
# columns
found_table = False
diff --git a/lib/sqlalchemy/engine/result.py b/lib/sqlalchemy/engine/result.py
index 88930081e..65ce3b742 100644
--- a/lib/sqlalchemy/engine/result.py
+++ b/lib/sqlalchemy/engine/result.py
@@ -8,7 +8,7 @@
and :class:`.RowProxy."""
-from itertools import izip
+
from .. import exc, types, util
from ..sql import expression
import collections
@@ -55,7 +55,7 @@ except ImportError:
return list(self)
def __iter__(self):
- for processor, value in izip(self._processors, self._row):
+ for processor, value in zip(self._processors, self._row):
if processor is None:
yield value
else:
@@ -72,7 +72,7 @@ except ImportError:
except TypeError:
if isinstance(key, slice):
l = []
- for processor, value in izip(self._processors[key],
+ for processor, value in zip(self._processors[key],
self._row[key]):
if processor is None:
l.append(value)
@@ -93,7 +93,7 @@ except ImportError:
def __getattr__(self, name):
try:
return self[name]
- except KeyError, e:
+ except KeyError as e:
raise AttributeError(e.args[0])
@@ -142,7 +142,7 @@ class RowProxy(BaseRowProxy):
def items(self):
"""Return a list of tuples, each tuple containing a key/value pair."""
# TODO: no coverage here
- return [(key, self[key]) for key in self.iterkeys()]
+ return [(key, self[key]) for key in self.keys()]
def keys(self):
"""Return the list of keys as strings represented by this RowProxy."""
@@ -274,7 +274,7 @@ class ResultMetaData(object):
def _key_fallback(self, key, raiseerr=True):
map = self._keymap
result = None
- if isinstance(key, basestring):
+ if isinstance(key, util.string_types):
result = map.get(key if self.case_sensitive else key.lower())
# fallback for targeting a ColumnElement to a textual expression
# this is a rare use case which only occurs when matching text()
@@ -328,8 +328,8 @@ class ResultMetaData(object):
return {
'_pickled_keymap': dict(
(key, index)
- for key, (processor, obj, index) in self._keymap.iteritems()
- if isinstance(key, (basestring, int))
+ for key, (processor, obj, index) in self._keymap.items()
+ if isinstance(key, util.string_types + util.int_types)
),
'keys': self.keys,
"case_sensitive": self.case_sensitive,
@@ -338,9 +338,9 @@ class ResultMetaData(object):
def __setstate__(self, state):
# the row has been processed at pickling time so we don't need any
# processor anymore
- self._processors = [None for _ in xrange(len(state['keys']))]
+ self._processors = [None for _ in range(len(state['keys']))]
self._keymap = keymap = {}
- for key, index in state['_pickled_keymap'].iteritems():
+ for key, index in state['_pickled_keymap'].items():
# not preserving "obj" here, unfortunately our
# proxy comparison fails with the unpickle
keymap[key] = (None, None, index)
@@ -440,7 +440,7 @@ class ResultProxy(object):
"""
try:
return self.context.rowcount
- except Exception, e:
+ except Exception as e:
self.connection._handle_dbapi_exception(
e, None, None, self.cursor, self.context)
@@ -462,7 +462,7 @@ class ResultProxy(object):
"""
try:
return self._saved_cursor.lastrowid
- except Exception, e:
+ except Exception as e:
self.connection._handle_dbapi_exception(
e, None, None,
self._saved_cursor, self.context)
@@ -746,7 +746,7 @@ class ResultProxy(object):
l = self.process_rows(self._fetchall_impl())
self.close()
return l
- except Exception, e:
+ except Exception as e:
self.connection._handle_dbapi_exception(
e, None, None,
self.cursor, self.context)
@@ -765,7 +765,7 @@ class ResultProxy(object):
if len(l) == 0:
self.close()
return l
- except Exception, e:
+ except Exception as e:
self.connection._handle_dbapi_exception(
e, None, None,
self.cursor, self.context)
@@ -784,7 +784,7 @@ class ResultProxy(object):
else:
self.close()
return None
- except Exception, e:
+ except Exception as e:
self.connection._handle_dbapi_exception(
e, None, None,
self.cursor, self.context)
@@ -800,7 +800,7 @@ class ResultProxy(object):
try:
row = self._fetchone_impl()
- except Exception, e:
+ except Exception as e:
self.connection._handle_dbapi_exception(
e, None, None,
self.cursor, self.context)
@@ -966,9 +966,9 @@ class BufferedColumnResultProxy(ResultProxy):
# constructed.
metadata._orig_processors = metadata._processors
# replace the all type processors by None processors.
- metadata._processors = [None for _ in xrange(len(metadata.keys))]
+ metadata._processors = [None for _ in range(len(metadata.keys))]
keymap = {}
- for k, (func, obj, index) in metadata._keymap.iteritems():
+ for k, (func, obj, index) in metadata._keymap.items():
keymap[k] = (None, obj, index)
self._metadata._keymap = keymap
@@ -989,7 +989,7 @@ class BufferedColumnResultProxy(ResultProxy):
if size is None:
return self.fetchall()
l = []
- for i in xrange(size):
+ for i in range(size):
row = self.fetchone()
if row is None:
break
diff --git a/lib/sqlalchemy/engine/strategies.py b/lib/sqlalchemy/engine/strategies.py
index 4c81df8f0..c65986ca2 100644
--- a/lib/sqlalchemy/engine/strategies.py
+++ b/lib/sqlalchemy/engine/strategies.py
@@ -78,20 +78,14 @@ class DefaultEngineStrategy(EngineStrategy):
def connect():
try:
return dialect.connect(*cargs, **cparams)
- except Exception, e:
+ except Exception as e:
invalidated = dialect.is_disconnect(e, None, None)
- # Py3K
- #raise exc.DBAPIError.instance(None, None,
- # e, dialect.dbapi.Error,
- # connection_invalidated=invalidated
- #) from e
- # Py2K
- import sys
- raise exc.DBAPIError.instance(
- None, None, e, dialect.dbapi.Error,
- connection_invalidated=invalidated
- ), None, sys.exc_info()[2]
- # end Py2K
+ util.raise_from_cause(
+ exc.DBAPIError.instance(None, None,
+ e, dialect.dbapi.Error,
+ connection_invalidated=invalidated
+ )
+ )
creator = kwargs.pop('creator', connect)
diff --git a/lib/sqlalchemy/engine/url.py b/lib/sqlalchemy/engine/url.py
index c4931b48c..ed5729eea 100644
--- a/lib/sqlalchemy/engine/url.py
+++ b/lib/sqlalchemy/engine/url.py
@@ -14,7 +14,6 @@ be used directly and is also accepted directly by ``create_engine()``.
"""
import re
-import urllib
from .. import exc, util
from . import Dialect
@@ -67,7 +66,7 @@ class URL(object):
if self.username is not None:
s += self.username
if self.password is not None:
- s += ':' + urllib.quote_plus(self.password)
+ s += ':' + util.quote_plus(self.password)
s += "@"
if self.host is not None:
s += self.host
@@ -76,7 +75,7 @@ class URL(object):
if self.database is not None:
s += '/' + self.database
if self.query:
- keys = self.query.keys()
+ keys = list(self.query)
keys.sort()
s += '?' + "&".join("%s=%s" % (k, self.query[k]) for k in keys)
return s
@@ -150,7 +149,7 @@ def make_url(name_or_url):
existing URL object is passed, just returns the object.
"""
- if isinstance(name_or_url, basestring):
+ if isinstance(name_or_url, util.string_types):
return _parse_rfc1738_args(name_or_url)
else:
return name_or_url
@@ -177,17 +176,15 @@ def _parse_rfc1738_args(name):
tokens = components['database'].split('?', 2)
components['database'] = tokens[0]
query = (len(tokens) > 1 and dict(util.parse_qsl(tokens[1]))) or None
- # Py2K
- if query is not None:
+ if util.py2k and query is not None:
query = dict((k.encode('ascii'), query[k]) for k in query)
- # end Py2K
else:
query = None
components['query'] = query
if components['password'] is not None:
components['password'] = \
- urllib.unquote_plus(components['password'])
+ util.unquote_plus(components['password'])
name = components.pop('name')
return URL(name, **components)