10000 bpo-42333: Port _ssl extension module to heap types (GH-23392) by tiran · Pull Request #23392 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-42333: Port _ssl extension module to heap types (GH-23392) #23392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 20, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move exceptions to sslmodule_init_exceptions
  • Loading branch information
tiran committed Nov 19, 2020
commit 27636b08ef1bbd2439b92bf3fc240b3853c10be8
131 changes: 82 additions & 49 deletions Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,6 @@ SSLError_str(PyOSErrorObject *self)
}

static PyType_Slot sslerror_type_slots[] = {
{Py_tp_base, NULL}, /* Filled out in module init as it's not a constant */
{Py_tp_doc, (void*)SSLError_doc},
{Py_tp_str, SSLError_str},
{0, 0},
Expand Down Expand Up @@ -5944,6 +5943,85 @@ sslmodule_init_types(PyObject *module)
return 0;
}

static int
sslmodule_init_exceptions(PyObject *module)
{
PyObject *bases = NULL;

#define add_exception(exc, name, doc, base) \
do { \
(exc) = PyErr_NewExceptionWithDoc("ssl." name, (doc), (base), NULL); \
if ((exc) == NULL) goto error; \
if (PyModule_AddObjectRef(module, name, exc) < 0) goto error; \
} while(0)

bases = PyTuple_Pack(1, PyExc_OSError);
if (bases == NULL) {
goto error;
}
PySSLErrorObject = PyType_FromSpecWithBases(&sslerror_type_spec, bases);
Py_CLEAR(bases);
if (PySSLErrorObject == NULL) {
goto error;
}
if (PyModule_AddObjectRef(module, "SSLError", PySSLErrorObject) < 0) {
goto error;
}

/* ssl.CertificateError used to be a subclass of ValueError */
bases = PyTuple_Pack(2, PySSLErrorObject, PyExc_ValueError);
if (bases == NULL) {
goto error;
}
add_exception(
PySSLCertVerificationErrorObject,
"SSLCertVerificationError",
SSLCertVerificationError_doc,
bases
);
Py_CLEAR(bases);

add_exception(
PySSLZeroReturnErrorObject,
"SSLZeroReturnError",
SSLZeroReturnError_doc,
PySSLErrorObject
);

add_exception(
PySSLWantWriteErrorObject,
"SSLWantWriteError",
SSLWantWriteError_doc,
PySSLErrorObject
);

add_exception(
PySSLWantReadErrorObject,
"SSLWantReadError",
SSLWantReadError_doc,
PySSLErrorObject
);

add_exception(
PySSLSyscallErrorObject,
"SSLSyscallError",
SSLSyscallError_doc,
PySSLErrorObject
);

add_exception(
PySSLEOFErrorObject,
"SSLEOFError",
SSLEOFError_doc,
PySSLErrorObject
);
#undef add_exception

return 0;
error:
Py_XDECREF(bases);
return -1;
}

PyDoc_STRVAR(module_doc,
"Implementation module for SSL socket operations. See the socket module\n\
Expand Down Expand Up @@ -5983,7 +6061,7 @@ parse_openssl_version(unsigned long libver,
PyMODINIT_FUNC
PyInit__ssl(void)
{
PyObject *m, *d, *r, *bases;
PyObject *m, *r;
unsigned long libver;
unsigned int major, minor, fix, patch, status;
PySocketModule_APIObject *socket_api;
Expand All @@ -5993,10 +6071,11 @@ PyInit__ssl(void)
m = PyModule_Create(&_sslmodule);
if (m == NULL)
return NULL;
d = PyModule_GetDict(m);

if (sslmodule_init_types(m) != 0)
return NULL;
if (sslmodule_init_exceptions(m) != 0)
return NULL;

/* Load _socket module and its C API */
socket_api = PySocketModule_ImportModuleAndAPI();
Expand All @@ -6022,52 +6101,6 @@ PyInit__ssl(void)
_ssl_locks_count++;
#endif

/* Add symbols to module dict */
sslerror_type_slots[0].pfunc = PyExc_OSError;
PySSLErrorObject = PyType_FromSpec(&sslerror_type_spec);
if (PySSLErrorObject == NULL)
return NULL;

/* ssl.CertificateError used to be a subclass of ValueError */
bases = Py_BuildValue("OO", PySSLErrorObject, PyExc_ValueError);
if (bases == NULL)
return NULL;
PySSLCertVerificationErrorObject = PyErr_NewExceptionWithDoc(
"ssl.SSLCertVerificationError", SSLCertVerificationError_doc,
bases, NULL);
Py_DECREF(bases);
PySSLZeroReturnErrorObject = PyErr_NewExceptionWithDoc(
"ssl.SSLZeroReturnError", SSLZeroReturnError_doc,
PySSLErrorObject, NULL);
PySSLWantReadErrorObject = PyErr_NewExceptionWithDoc(
"ssl.SSLWantReadError", SSLWantReadError_doc,
PySSLErrorObject, NULL);
PySSLWantWriteErrorObject = PyErr_NewExceptionWithDoc(
"ssl.SSLWantWriteError", SSLWantWriteError_doc,
PySSLErrorObject, NULL);
PySSLSyscallErrorObject = PyErr_NewExceptionWithDoc(
"ssl.SSLSyscallError", SSLSyscallError_doc,
PySSLErrorObject, NULL);
PySSLEOFErrorObject = PyErr_NewExceptionWithDoc(
"ssl.SSLEOFError", SSLEOFError_doc,
PySSLErrorObject, NULL);
if (PySSLCertVerificationErrorObject == NULL
|| PySSLZeroReturnErrorObject == NULL
|| PySSLWantReadErrorObject == NULL
|| PySSLWantWriteErrorObject == NULL
|| PySSLSyscallErrorObject == NULL
|| PySSLEOFErrorObject == NULL)
return NULL;
if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0
|| PyDict_SetItemString(d, "SSLCertVerificationError",
PySSLCertVerificationErrorObject) != 0
|| PyDict_SetItemString(d, "SSLZeroReturnError", PySSLZeroReturnErrorObject) != 0
|| PyDict_SetItemString(d, "SSLWantReadError", PySSLWantReadErrorObject) != 0
|| PyDict_SetItemString(d, "SSLWantWriteError", PySSLWantWriteErrorObject) != 0
|| PyDict_SetItemString(d, "SSLSyscallError", PySSLSyscallErrorObject) != 0
|| PyDict_SetItemString(d, "SSLEOFError", PySSLEOFErrorObject) != 0)
return NULL;

PyModule_AddStringConstant(m, "_DEFAULT_CIPHERS",
PY_SSL_DEFAULT_CIPHER_STRING);

Expand Down
0