Skip to content

Commit

Permalink
gh-102192: remove redundant excption fields from ssl module socket
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel committed Mar 6, 2023
1 parent 6716254 commit ca602df
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 23 deletions.
27 changes: 8 additions & 19 deletions Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,7 @@ typedef struct {
* store exception information on the socket. The handshake, read, write,
* and shutdown methods check for chained exceptions.
*/
PyObject *exc_type;
PyObject *exc_value;
PyObject *exc_tb;
PyObject *exc;
} PySSLSocket;

typedef struct {
Expand Down Expand Up @@ -560,13 +558,10 @@ fill_and_set_sslerror(_sslmodulestate *state,

static int
PySSL_ChainExceptions(PySSLSocket *sslsock) {
if (sslsock->exc_type == NULL)
if (sslsock->exc == NULL)
return 0;

_PyErr_ChainExceptions(sslsock->exc_type, sslsock->exc_value, sslsock->exc_tb);
sslsock->exc_type = NULL;
sslsock->exc_value = NULL;
sslsock->exc_tb = NULL;
_PyErr_ChainExceptions1(sslsock->exc);
return -1;
}

Expand Down Expand Up @@ -803,9 +798,7 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
self->owner = NULL;
self->server_hostname = NULL;
self->err = err;
self->exc_type = NULL;
self->exc_value = NULL;
self->exc_tb = NULL;
self->exc = NULL;

/* Make sure the SSL error state is initialized */
ERR_clear_error();
Expand Down Expand Up @@ -2175,19 +2168,15 @@ Passed as \"self\" in servername callback.");
static int
PySSL_traverse(PySSLSocket *self, visitproc visit, void *arg)
{
Py_VISIT(self->exc_type);
Py_VISIT(self->exc_value);
Py_VISIT(self->exc_tb);
Py_VISIT(self->exc);
Py_VISIT(Py_TYPE(self));
return 0;
}

static int
PySSL_clear(PySSLSocket *self)
{
Py_CLEAR(self->exc_type);
Py_CLEAR(self->exc_value);
Py_CLEAR(self->exc_tb);
Py_CLEAR(self->exc);
return 0;
}

Expand Down Expand Up @@ -2532,7 +2521,7 @@ _ssl__SSLSocket_read_impl(PySSLSocket *self, Py_ssize_t len,
PySSL_SetError(self, retval, __FILE__, __LINE__);
goto error;
}
if (self->exc_type != NULL)
if (self->exc != NULL)
goto error;

done:
Expand Down Expand Up @@ -2658,7 +2647,7 @@ _ssl__SSLSocket_shutdown_impl(PySSLSocket *self)
PySSL_SetError(self, ret, __FILE__, __LINE__);
return NULL;
}
if (self->exc_type != NULL)
if (self->exc != NULL)
goto error;
if (sock)
/* It's already INCREF'ed */
Expand Down
7 changes: 3 additions & 4 deletions Modules/_ssl/debughelpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ _PySSL_msg_callback(int write_p, int version, int content_type,
buf, len
);
if (res == NULL) {
PyErr_Fetch(&ssl_obj->exc_type, &ssl_obj->exc_value, &ssl_obj->exc_tb);
ssl_obj->exc = PyErr_GetRaisedException();
} else {
Py_DECREF(res);
}
Expand Down Expand Up @@ -138,8 +138,7 @@ _PySSL_keylog_callback(const SSL *ssl, const char *line)
lock = PyThread_allocate_lock();
if (lock == NULL) {
PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
PyErr_Fetch(&ssl_obj->exc_type, &ssl_obj->exc_value,
&ssl_obj->exc_tb);
ssl_obj->exc = PyErr_GetRaisedException();
return;
}
}
Expand All @@ -156,7 +155,7 @@ _PySSL_keylog_callback(const SSL *ssl, const char *line)
errno = e;
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
ssl_obj->ctx->keylog_filename);
PyErr_Fetch(&ssl_obj->exc_type, &ssl_obj->exc_value, &ssl_obj->exc_tb);
ssl_obj->exc = PyErr_GetRaisedException();
}
PyGILState_Release(threadstate);
}
Expand Down

0 comments on commit ca602df

Please sign in to comment.