]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Repair exception handling in CursorResult
authorFederico Caselli <cfederico87@gmail.com>
Mon, 29 Mar 2021 19:58:38 +0000 (21:58 +0200)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 30 Mar 2021 23:13:58 +0000 (19:13 -0400)
Fixes: #6138
Change-Id: I794a3da688fd8577fb06770ef02bf827a5c55397

doc/build/changelog/unreleased_14/6138.rst [new file with mode: 0644]
lib/sqlalchemy/engine/cursor.py
lib/sqlalchemy/testing/suite/test_update_delete.py
test/requirements.py
test/sql/test_resultset.py

diff --git a/doc/build/changelog/unreleased_14/6138.rst b/doc/build/changelog/unreleased_14/6138.rst
new file mode 100644 (file)
index 0000000..4b288cf
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: bug, engine
+    :tickets: 6138
+
+    Repair wrong arguments to exception handling method
+    in CursorResult.
index b5b92b1714492f138ccd2f13ad7b74cb79712a15..5a3b929018bc78dcd4a524357291f70944fa1b03 100644 (file)
@@ -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):
index 3fb51ead3dba8433082f9bf07882b5c01fbcccf4..f5ee2e02815595ab8f24110f09d3a1638c926cf0 100644 (file)
@@ -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")],
index 4858e6a243ee4cbf47e031c27d875c834806fa58..e71cec6e19898b4609ea42c1c5c3a0c1cfbbc9b4 100644 (file)
@@ -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
index 5439d63b574727d14f79204cca774cb5cfa1ea06..67bd7785bf8a3ff0283f179a4c7f30653e7cb1c1 100644 (file)
@@ -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"