Originally reported as https://bugzilla.redhat.com/show_bug.cgi?id=1959080
during early integration testing of Python 3.10 done in Fedora.

The test failed because Python 3.10 reports the traceback a bit differently:

cat postgresql-13.2/src/pl/plpython/regression.diffs
diff -U3
postgresql-13.2/src/pl/plpython/expected/python3/plpython_subtransaction.out
/builddir/build/BUILD/postgresql-13.2/src/pl/plpython/results/python3/plpython_subtransaction.out
---
postgresql-13.2/src/pl/plpython/expected/python3/plpython_subtransaction.out
2021-05-11 17:05:24.116431883 +0200
+++
postgresql-13.2/src/pl/plpython/results/python3/plpython_subtransaction.out
2021-05-11 17:05:26.689459542 +0200
@@ -224,8 +224,8 @@
 SELECT subtransaction_exit_subtransaction_in_with();
 ERROR:  ValueError: this subtransaction has already been exited
 CONTEXT:  Traceback (most recent call last):
-  PL/Python function "subtransaction_exit_subtransaction_in_with", line 3,
in <module>
-    s.__exit__(None, None, None)
+  PL/Python function "subtransaction_exit_subtransaction_in_with", line 2,
in <module>
+    with plpy.subtransaction() as s:
 PL/Python function "subtransaction_exit_subtransaction_in_with"
 -- Make sure we don't get a "current transaction is aborted" error
 SELECT 1 as test;

The attached patch makes the traceback look the same in Python 3.10 and
also in earlier versions.

Regards,
Honza
From dc0bbf61345c64d8ea261b85513611d9e7fc5606 Mon Sep 17 00:00:00 2001
From: Honza Horak <hho...@redhat.com>
Date: Tue, 11 May 2021 23:22:24 +0200
Subject: [PATCH] Fix subtransaction test for Python 3.10

Starting with Python 3.10, the stacktrace looks differently:
  -  PL/Python function "subtransaction_exit_subtransaction_in_with", line 3, in <module>
  -    s.__exit__(None, None, None)
  +  PL/Python function "subtransaction_exit_subtransaction_in_with", line 2, in <module>
  +    with plpy.subtransaction() as s:
Using try/except specifically makes the error look always the same.

RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=1959080
---
 src/pl/plpython/expected/plpython_subtransaction.out | 11 +++++++----
 src/pl/plpython/sql/plpython_subtransaction.sql      |  7 +++++--
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/src/pl/plpython/expected/plpython_subtransaction.out b/src/pl/plpython/expected/plpython_subtransaction.out
index 0d0ff2e36d..2a56541917 100644
--- a/src/pl/plpython/expected/plpython_subtransaction.out
+++ b/src/pl/plpython/expected/plpython_subtransaction.out
@@ -171,8 +171,11 @@ with plpy.subtransaction() as s:
 $$ LANGUAGE plpythonu;
 CREATE FUNCTION subtransaction_exit_subtransaction_in_with() RETURNS void
 AS $$
-with plpy.subtransaction() as s:
-    s.__exit__(None, None, None)
+try:
+    with plpy.subtransaction() as s:
+        s.__exit__(None, None, None)
+except ValueError as e:
+    raise ValueError(e)
 $$ LANGUAGE plpythonu;
 SELECT subtransaction_exit_without_enter();
 ERROR:  ValueError: this subtransaction has not been entered
@@ -224,8 +227,8 @@ PL/Python function "subtransaction_enter_subtransaction_in_with"
 SELECT subtransaction_exit_subtransaction_in_with();
 ERROR:  ValueError: this subtransaction has already been exited
 CONTEXT:  Traceback (most recent call last):
-  PL/Python function "subtransaction_exit_subtransaction_in_with", line 3, in <module>
-    s.__exit__(None, None, None)
+  PL/Python function "subtransaction_exit_subtransaction_in_with", line 6, in <module>
+    raise ValueError(e)
 PL/Python function "subtransaction_exit_subtransaction_in_with"
 -- Make sure we don't get a "current transaction is aborted" error
 SELECT 1 as test;
diff --git a/src/pl/plpython/sql/plpython_subtransaction.sql b/src/pl/plpython/sql/plpython_subtransaction.sql
index 47bb11f157..cc4b1ae102 100644
--- a/src/pl/plpython/sql/plpython_subtransaction.sql
+++ b/src/pl/plpython/sql/plpython_subtransaction.sql
@@ -121,8 +121,11 @@ $$ LANGUAGE plpythonu;
 
 CREATE FUNCTION subtransaction_exit_subtransaction_in_with() RETURNS void
 AS $$
-with plpy.subtransaction() as s:
-    s.__exit__(None, None, None)
+try:
+    with plpy.subtransaction() as s:
+        s.__exit__(None, None, None)
+except ValueError as e:
+    raise ValueError(e)
 $$ LANGUAGE plpythonu;
 
 SELECT subtransaction_exit_without_enter();
-- 
2.26.3

Reply via email to