From: Mike Bayer Date: Tue, 26 May 2009 17:03:03 +0000 (+0000) Subject: more fixes to bound parameter exception reporting X-Git-Tag: rel_0_5_4p2 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=853992649c64252c3959b76600434f9cab05288c;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git more fixes to bound parameter exception reporting --- diff --git a/CHANGES b/CHANGES index 314794b587..cced2b719c 100644 --- a/CHANGES +++ b/CHANGES @@ -9,7 +9,7 @@ CHANGES - sql - Repaired the printing of SQL exceptions which are not - based on parameters. + based on parameters or are not executemany() style. - postgres - Deprecated the hardcoded TIMESTAMP function, which when diff --git a/lib/sqlalchemy/exc.py b/lib/sqlalchemy/exc.py index e0310d03c1..ce130ce3c2 100644 --- a/lib/sqlalchemy/exc.py +++ b/lib/sqlalchemy/exc.py @@ -132,11 +132,11 @@ class DBAPIError(SQLAlchemyError): self.connection_invalidated = connection_invalidated def __str__(self): - if self.params and len(self.params) > 10: + if isinstance(self.params, (list, tuple)) and len(self.params) > 10 and isinstance(self.params[0], (list, dict, tuple)): return ' '.join((SQLAlchemyError.__str__(self), repr(self.statement), repr(self.params[:2]), - '... and a total of %i bound parameters' % len(self.params))) + '... and a total of %i bound parameter sets' % len(self.params))) return ' '.join((SQLAlchemyError.__str__(self), repr(self.statement), repr(self.params))) diff --git a/test/base/except.py b/test/base/except.py index c2b60f32a7..3f4d654771 100644 --- a/test/base/except.py +++ b/test/base/except.py @@ -32,6 +32,45 @@ class WrapTest(unittest.TestCase): 'this is a message', None, OperationalError()) except sa_exceptions.DBAPIError, exc: assert str(exc) == "(OperationalError) 'this is a message' None" + + def test_tostring_large_dict(self): + try: + raise sa_exceptions.DBAPIError.instance( + 'this is a message', {'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6, 'g':7, 'h':8, 'i':9, 'j':10, 'k':11}, OperationalError()) + except sa_exceptions.DBAPIError, exc: + assert str(exc).startswith("(OperationalError) 'this is a message' {") + + def test_tostring_large_list(self): + try: + raise sa_exceptions.DBAPIError.instance( + 'this is a message', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], OperationalError()) + except sa_exceptions.DBAPIError, exc: + assert str(exc).startswith("(OperationalError) 'this is a message' [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]") + + def test_tostring_large_executemany(self): + try: + raise sa_exceptions.DBAPIError.instance( + 'this is a message', [{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},], OperationalError()) + except sa_exceptions.DBAPIError, exc: + assert str(exc) == "(OperationalError) 'this is a message' [{1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}]", str(exc) + + try: + raise sa_exceptions.DBAPIError.instance( + 'this is a message', [{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},], OperationalError()) + except sa_exceptions.DBAPIError, exc: + assert str(exc) == "(OperationalError) 'this is a message' [{1: 1}, {1: 1}] ... and a total of 11 bound parameter sets" + + try: + raise sa_exceptions.DBAPIError.instance( + 'this is a message', [(1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,)], OperationalError()) + except sa_exceptions.DBAPIError, exc: + assert str(exc) == "(OperationalError) 'this is a message' [(1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,)]" + + try: + raise sa_exceptions.DBAPIError.instance( + 'this is a message', [(1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), ], OperationalError()) + except sa_exceptions.DBAPIError, exc: + assert str(exc) == "(OperationalError) 'this is a message' [(1,), (1,)] ... and a total of 11 bound parameter sets" def test_db_error_busted_dbapi(self): try: