[Python-checkins] gh-105927: _ssl GET_SOCKET() uses _PyWeakref_GET_REF() (#106002)

vstinner webhook-mailer at python.org
Thu Jun 22 21:02:05 EDT 2023


https://github.com/python/cpython/commit/7b3ed5b29fd33ecfdaedc3bb0b8f6c05ded68361
commit: 7b3ed5b29fd33ecfdaedc3bb0b8f6c05ded68361
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2023-06-23T03:02:02+02:00
summary:

gh-105927: _ssl GET_SOCKET() uses _PyWeakref_GET_REF() (#106002)

files:
M Modules/_ssl.c

diff --git a/Modules/_ssl.c b/Modules/_ssl.c
index 9ce61a651c2cb..4254fde0f5190 100644
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -383,10 +383,20 @@ typedef enum {
 #define ERRSTR1(x,y,z) (x ":" y ": " z)
 #define ERRSTR(x) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x)
 
-/* Get the socket from a PySSLSocket, if it has one */
+// Get the socket from a PySSLSocket, if it has one.
+// Return a borrowed reference.
 static inline PySocketSockObject* GET_SOCKET(PySSLSocket *obj) {
     if (obj->Socket) {
-        return (PySocketSockObject *)PyWeakref_GetObject(obj->Socket);
+        PyObject *sock = _PyWeakref_GET_REF(obj->Socket);
+        if (sock != NULL) {
+            // GET_SOCKET() returns a borrowed reference
+            Py_DECREF(sock);
+        }
+        else {
+            // dead weak reference
+            sock = Py_None;
+        }
+        return (PySocketSockObject *)sock;  // borrowed reference
     }
     else {
         return NULL;



More information about the Python-checkins mailing list