From e91b0a7139d4a4cbd2351ccb5cd021a100cf42d2 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Tue, 1 Mar 2022 21:08:21 +0530 Subject: [PATCH] bpo-46541: remove usage of _Py_IDENTIFIER from _ssl module (GH-31599) --- Modules/_ssl.c | 42 ++++++++++++++++++++++++++++++++---------- Modules/_ssl.h | 5 +++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Modules/_ssl.c b/Modules/_ssl.c index 312b2eabe3d..e67ab42050b 100644 --- a/Modules/_ssl.c +++ b/Modules/_ssl.c @@ -22,7 +22,6 @@ #define OPENSSL_NO_DEPRECATED 1 #define PY_SSIZE_T_CLEAN -#define NEEDS_PY_IDENTIFIER #include "Python.h" @@ -447,10 +446,6 @@ fill_and_set_sslerror(_sslmodulestate *state, PyObject *err_value = NULL, *reason_obj = NULL, *lib_obj = NULL; PyObject *verify_obj = NULL, *verify_code_obj = NULL; PyObject *init_value, *msg, *key; - _Py_IDENTIFIER(reason); - _Py_IDENTIFIER(library); - _Py_IDENTIFIER(verify_message); - _Py_IDENTIFIER(verify_code); if (errcode != 0) { int lib, reason; @@ -544,20 +539,20 @@ fill_and_set_sslerror(_sslmodulestate *state, if (reason_obj == NULL) reason_obj = Py_None; - if (_PyObject_SetAttrId(err_value, &PyId_reason, reason_obj)) + if (PyObject_SetAttr(err_value, state->str_reason, reason_obj)) goto fail; if (lib_obj == NULL) lib_obj = Py_None; - if (_PyObject_SetAttrId(err_value, &PyId_library, lib_obj)) + if (PyObject_SetAttr(err_value, state->str_library, lib_obj)) goto fail; if ((sslsock != NULL) && (type == state->PySSLCertVerificationErrorObject)) { /* Only set verify code / message for SSLCertVerificationError */ - if (_PyObject_SetAttrId(err_value, &PyId_verify_code, + if (PyObject_SetAttr(err_value, state->str_verify_code, verify_code_obj)) goto fail; - if (_PyObject_SetAttrId(err_value, &PyId_verify_message, verify_obj)) + if (PyObject_SetAttr(err_value, state->str_verify_message, verify_obj)) goto fail; } @@ -6158,6 +6153,29 @@ sslmodule_init_types(PyObject *module) return 0; } +static int +sslmodule_init_strings(PyObject *module) +{ + _sslmodulestate *state = get_ssl_state(module); + state->str_library = PyUnicode_InternFromString("library"); + if (state->str_library == NULL) { + return -1; + } + state->str_reason = PyUnicode_InternFromString("reason"); + if (state->str_reason == NULL) { + return -1; + } + state->str_verify_message = PyUnicode_InternFromString("verify_message"); + if (state->str_verify_message == NULL) { + return -1; + } + state->str_verify_code = PyUnicode_InternFromString("verify_code"); + if (state->str_verify_code == NULL) { + return -1; + } + return 0; +} + static PyModuleDef_Slot sslmodule_slots[] = { {Py_mod_exec, sslmodule_init_types}, {Py_mod_exec, sslmodule_init_exceptions}, @@ -6165,6 +6183,7 @@ static PyModuleDef_Slot sslmodule_slots[] = { {Py_mod_exec, sslmodule_init_errorcodes}, {Py_mod_exec, sslmodule_init_constants}, {Py_mod_exec, sslmodule_init_versioninfo}, + {Py_mod_exec, sslmodule_init_strings}, {0, NULL} }; @@ -6214,7 +6233,10 @@ sslmodule_clear(PyObject *m) Py_CLEAR(state->err_names_to_codes); Py_CLEAR(state->lib_codes_to_names); Py_CLEAR(state->Sock_Type); - + Py_CLEAR(state->str_library); + Py_CLEAR(state->str_reason); + Py_CLEAR(state->str_verify_code); + Py_CLEAR(state->str_verify_message); return 0; } diff --git a/Modules/_ssl.h b/Modules/_ssl.h index 5593a455de4..d68ccdec5e8 100644 --- a/Modules/_ssl.h +++ b/Modules/_ssl.h @@ -29,6 +29,11 @@ typedef struct { PyObject *lib_codes_to_names; /* socket type from module CAPI */ PyTypeObject *Sock_Type; + /* Interned strings */ + PyObject *str_library; + PyObject *str_reason; + PyObject *str_verify_code; + PyObject *str_verify_message; } _sslmodulestate; static struct PyModuleDef _sslmodule_def;