Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b3ed5b

Browse files
authored
gh-105927: _ssl GET_SOCKET() uses _PyWeakref_GET_REF() (#106002)
1 parent ee52158 commit 7b3ed5b

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Modules/_ssl.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,20 @@ typedef enum {
383383
#define ERRSTR1(x,y,z) (x ":" y ": " z)
384384
#define ERRSTR(x) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x)
385385

386-
/* Get the socket from a PySSLSocket, if it has one */
386+
// Get the socket from a PySSLSocket, if it has one.
387+
// Return a borrowed reference.
387388
static inline PySocketSockObject* GET_SOCKET(PySSLSocket *obj) {
388389
if (obj->Socket) {
389-
return (PySocketSockObject *)PyWeakref_GetObject(obj->Socket);
390+
PyObject *sock = _PyWeakref_GET_REF(obj->Socket);
391+
if (sock != NULL) {
392+
// GET_SOCKET() returns a borrowed reference
393+
Py_DECREF(sock);
394+
}
395+
else {
396+
// dead weak reference
397+
sock = Py_None;
398+
}
399+
return (PySocketSockObject *)sock; // borrowed reference
390400
}
391401
else {
392402
return NULL;

0 commit comments

Comments
 (0)