public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Tom de Vries <vries@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] [gdb/python] Handle deprecation of PyErr_{Fetch,Restore} in 3.12
Date: Sat,  9 Mar 2024 15:12:44 +0000 (GMT)	[thread overview]
Message-ID: <20240309151244.F07DB3858419@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=2cf3c79c807917097a2eea5a2b3eb326f17581d1

commit 2cf3c79c807917097a2eea5a2b3eb326f17581d1
Author: Tom de Vries <tdevries@suse.de>
Date:   Sat Mar 9 16:13:10 2024 +0100

    [gdb/python] Handle deprecation of PyErr_{Fetch,Restore} in 3.12
    
    Starting python version 3.12, PyErr_Fetch and PyErr_Restore are deprecated.
    
    Use PyErr_GetRaisedException and PyErr_SetRaisedException instead, for
    python >= 3.12.
    
    Tested on aarch64-linux.
    
    Approved-By: Tom Tromey <tom@tromey.com>

Diff:
---
 gdb/python/python-internal.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 30802ae2480..d603b3a1b85 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -640,12 +640,18 @@ public:
 
   gdbpy_err_fetch ()
   {
+#if PY_VERSION_HEX < 0x030c0000
     PyObject *error_type, *error_value, *error_traceback;
 
     PyErr_Fetch (&error_type, &error_value, &error_traceback);
     m_error_type.reset (error_type);
     m_error_value.reset (error_value);
     m_error_traceback.reset (error_traceback);
+#else
+    /* PyErr_Fetch is deprecated in python 3.12, use PyErr_GetRaisedException
+       instead.  */
+    m_exc.reset (PyErr_GetRaisedException ());
+#endif
   }
 
   /* Call PyErr_Restore using the values stashed in this object.
@@ -654,9 +660,15 @@ public:
 
   void restore ()
   {
+#if PY_VERSION_HEX < 0x030c0000
     PyErr_Restore (m_error_type.release (),
 		   m_error_value.release (),
 		   m_error_traceback.release ());
+#else
+    /* PyErr_Restore is deprecated in python 3.12, use PyErr_SetRaisedException
+       instead.  */
+    PyErr_SetRaisedException (m_exc.release ());
+#endif
   }
 
   /* Return the string representation of the exception represented by
@@ -683,6 +695,7 @@ public:
 
   gdbpy_ref<> value () const
   {
+#if PY_VERSION_HEX < 0x030c0000
     if (!m_normalized)
       {
 	PyObject *error_type, *error_value, *error_traceback;
@@ -696,19 +709,32 @@ public:
 	m_normalized = true;
       }
     return m_error_value;
+#else
+    return m_exc;
+#endif
   }
 
   /* Return a new reference to the exception type object.  */
 
   gdbpy_ref<> type () const
   {
+#if PY_VERSION_HEX < 0x030c0000
     return m_error_type;
+#else
+    if (m_exc.get() == nullptr)
+      return nullptr;
+    return gdbpy_ref<>::new_reference ((PyObject *)Py_TYPE (m_exc.get ()));
+#endif
   }
 
 private:
 
+#if PY_VERSION_HEX < 0x030c0000
   mutable gdbpy_ref<> m_error_type, m_error_value, m_error_traceback;
   mutable bool m_normalized = false;
+#else
+  gdbpy_ref<> m_exc;
+#endif
 };
 
 /* Called before entering the Python interpreter to install the

                 reply	other threads:[~2024-03-09 15:12 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240309151244.F07DB3858419@sourceware.org \
    --to=vries@sourceware.org \
    --cc=gdb-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).