bpo-46541: remove usage of _Py_IDENTIFIER from _ssl module (GH-31599)

This commit is contained in:
Kumar Aditya 2022-03-01 21:08:21 +05:30 committed by GitHub
parent 9204bb72a2
commit e91b0a7139
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 10 deletions

View File

@ -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;
}

View File

@ -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;