From: Federico Caselli Date: Mon, 29 Mar 2021 19:58:38 +0000 (+0200) Subject: Repair exception handling in CursorResult X-Git-Tag: rel_1_4_4~1^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3df6a3f1fbb589110c3ef3f344758bb11d3dc3df;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Repair exception handling in CursorResult Fixes: #6138 Change-Id: I794a3da688fd8577fb06770ef02bf827a5c55397 --- diff --git a/doc/build/changelog/unreleased_14/6138.rst b/doc/build/changelog/unreleased_14/6138.rst new file mode 100644 index 0000000000..4b288cf07f --- /dev/null +++ b/doc/build/changelog/unreleased_14/6138.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: bug, engine + :tickets: 6138 + + Repair wrong arguments to exception handling method + in CursorResult. diff --git a/lib/sqlalchemy/engine/cursor.py b/lib/sqlalchemy/engine/cursor.py index b5b92b1714..5a3b929018 100644 --- a/lib/sqlalchemy/engine/cursor.py +++ b/lib/sqlalchemy/engine/cursor.py @@ -1624,7 +1624,7 @@ class BaseCursorResult(object): try: return self.context.rowcount except BaseException as e: - self.cursor_strategy.handle_exception(self, e) + self.cursor_strategy.handle_exception(self, self.cursor, e) @property def lastrowid(self): @@ -1645,7 +1645,7 @@ class BaseCursorResult(object): try: return self.context.get_lastrowid() except BaseException as e: - self.cursor_strategy.handle_exception(self, e) + self.cursor_strategy.handle_exception(self, self.cursor, e) @property def returns_rows(self): diff --git a/lib/sqlalchemy/testing/suite/test_update_delete.py b/lib/sqlalchemy/testing/suite/test_update_delete.py index 3fb51ead3d..f5ee2e0281 100644 --- a/lib/sqlalchemy/testing/suite/test_update_delete.py +++ b/lib/sqlalchemy/testing/suite/test_update_delete.py @@ -37,6 +37,7 @@ class SimpleUpdateDeleteTest(fixtures.TablesTest): ) assert not r.is_insert assert not r.returns_rows + assert r.rowcount == 1 eq_( connection.execute(t.select().order_by(t.c.id)).fetchall(), @@ -48,6 +49,7 @@ class SimpleUpdateDeleteTest(fixtures.TablesTest): r = connection.execute(t.delete().where(t.c.id == 2)) assert not r.is_insert assert not r.returns_rows + assert r.rowcount == 1 eq_( connection.execute(t.select().order_by(t.c.id)).fetchall(), [(1, "d1"), (3, "d3")], diff --git a/test/requirements.py b/test/requirements.py index 4858e6a243..e71cec6e19 100644 --- a/test/requirements.py +++ b/test/requirements.py @@ -928,15 +928,15 @@ class DefaultRequirements(SuiteRequirements): cursor object. """ - return skip_if( - "mssql+pymssql", "crashes on pymssql" - ) + fails_on_everything_except( - "mysql", - "mariadb", - "sqlite+pysqlite", - "sqlite+aiosqlite", - "sqlite+pysqlcipher", - "mssql", + return skip_if("mssql+pymssql", "crashes on pymssql") + only_on( + [ + "mysql", + "mariadb", + "sqlite+pysqlite", + "sqlite+aiosqlite", + "sqlite+pysqlcipher", + "mssql", + ] ) @property diff --git a/test/sql/test_resultset.py b/test/sql/test_resultset.py index 5439d63b57..67bd7785bf 100644 --- a/test/sql/test_resultset.py +++ b/test/sql/test_resultset.py @@ -43,6 +43,7 @@ from sqlalchemy.testing import assert_raises_message from sqlalchemy.testing import assertions from sqlalchemy.testing import engines from sqlalchemy.testing import eq_ +from sqlalchemy.testing import expect_raises_message from sqlalchemy.testing import fixtures from sqlalchemy.testing import in_ from sqlalchemy.testing import is_ @@ -1476,6 +1477,36 @@ class CursorResultTest(fixtures.TablesTest): finally: r.close() + @testing.requires.dbapi_lastrowid + def test_lastrowid(self, connection): + users = self.tables.users + + r = connection.execute( + users.insert(), dict(user_id=1, user_name="Test") + ) + eq_(r.lastrowid, r.context.get_lastrowid()) + + def test_raise_errors(self, connection): + users = self.tables.users + + class Wrapper: + def __init__(self, context): + self.context = context + + def __getattr__(self, name): + if name in ("rowcount", "get_lastrowid"): + raise Exception("canary") + return getattr(self.context, name) + + r = connection.execute( + users.insert(), dict(user_id=1, user_name="Test") + ) + r.context = Wrapper(r.context) + with expect_raises_message(Exception, "canary"): + r.rowcount + with expect_raises_message(Exception, "canary"): + r.lastrowid + class KeyTargetingTest(fixtures.TablesTest): run_inserts = "once"