Skip to content

Commit e91b0a7

Browse files
bpo-46541: remove usage of _Py_IDENTIFIER from _ssl module (GH-31599)
1 parent 9204bb7 commit e91b0a7

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

Modules/_ssl.c

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#define OPENSSL_NO_DEPRECATED 1
2323

2424
#define PY_SSIZE_T_CLEAN
25-
#define NEEDS_PY_IDENTIFIER
2625

2726
#include "Python.h"
2827

@@ -447,10 +446,6 @@ fill_and_set_sslerror(_sslmodulestate *state,
447446
PyObject *err_value = NULL, *reason_obj = NULL, *lib_obj = NULL;
448447
PyObject *verify_obj = NULL, *verify_code_obj = NULL;
449448
PyObject *init_value, *msg, *key;
450-
_Py_IDENTIFIER(reason);
451-
_Py_IDENTIFIER(library);
452-
_Py_IDENTIFIER(verify_message);
453-
_Py_IDENTIFIER(verify_code);
454449

455450
if (errcode != 0) {
456451
int lib, reason;
@@ -544,20 +539,20 @@ fill_and_set_sslerror(_sslmodulestate *state,
544539

545540
if (reason_obj == NULL)
546541
reason_obj = Py_None;
547-
if (_PyObject_SetAttrId(err_value, &PyId_reason, reason_obj))
542+
if (PyObject_SetAttr(err_value, state->str_reason, reason_obj))
548543
goto fail;
549544

550545
if (lib_obj == NULL)
551546
lib_obj = Py_None;
552-
if (_PyObject_SetAttrId(err_value, &PyId_library, lib_obj))
547+
if (PyObject_SetAttr(err_value, state->str_library, lib_obj))
553548
goto fail;
554549

555550
if ((sslsock != NULL) && (type == state->PySSLCertVerificationErrorObject)) {
556551
/* Only set verify code / message for SSLCertVerificationError */
557-
if (_PyObject_SetAttrId(err_value, &PyId_verify_code,
552+
if (PyObject_SetAttr(err_value, state->str_verify_code,
558553
verify_code_obj))
559554
goto fail;
560-
if (_PyObject_SetAttrId(err_value, &PyId_verify_message, verify_obj))
555+
if (PyObject_SetAttr(err_value, state->str_verify_message, verify_obj))
561556
goto fail;
562557
}
563558

@@ -6158,13 +6153,37 @@ sslmodule_init_types(PyObject *module)
61586153
return 0;
61596154
}
61606155

6156+
static int
6157+
sslmodule_init_strings(PyObject *module)
6158+
{
6159+
_sslmodulestate *state = get_ssl_state(module);
6160+
state->str_library = PyUnicode_InternFromString("library");
6161+
if (state->str_library == NULL) {
6162+
return -1;
6163+
}
6164+
state->str_reason = PyUnicode_InternFromString("reason");
6165+
if (state->str_reason == NULL) {
6166+
return -1;
6167+
}
6168+
state->str_verify_message = PyUnicode_InternFromString("verify_message");
6169+
if (state->str_verify_message == NULL) {
6170+
return -1;
6171+
}
6172+
state->str_verify_code = PyUnicode_InternFromString("verify_code");
6173+
if (state->str_verify_code == NULL) {
6174+
return -1;
6175+
}
6176+
return 0;
6177+
}
6178+
61616179
static PyModuleDef_Slot sslmodule_slots[] = {
61626180
{Py_mod_exec, sslmodule_init_types},
61636181
{Py_mod_exec, sslmodule_init_exceptions},
61646182
{Py_mod_exec, sslmodule_init_socketapi},
61656183
{Py_mod_exec, sslmodule_init_errorcodes},
61666184
{Py_mod_exec, sslmodule_init_constants},
61676185
{Py_mod_exec, sslmodule_init_versioninfo},
6186+
{Py_mod_exec, sslmodule_init_strings},
61686187
{0, NULL}
61696188
};
61706189

@@ -6214,7 +6233,10 @@ sslmodule_clear(PyObject *m)
62146233
Py_CLEAR(state->err_names_to_codes);
62156234
Py_CLEAR(state->lib_codes_to_names);
62166235
Py_CLEAR(state->Sock_Type);
6217-
6236+
Py_CLEAR(state->str_library);
6237+
Py_CLEAR(state->str_reason);
6238+
Py_CLEAR(state->str_verify_code);
6239+
Py_CLEAR(state->str_verify_message);
62186240
return 0;
62196241
}
62206242

Modules/_ssl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ typedef struct {
2929
PyObject *lib_codes_to_names;
3030
/* socket type from module CAPI */
3131
PyTypeObject *Sock_Type;
32+
/* Interned strings */
33+
PyObject *str_library;
34+
PyObject *str_reason;
35+
PyObject *str_verify_code;
36+
PyObject *str_verify_message;
3237
} _sslmodulestate;
3338

3439
static struct PyModuleDef _sslmodule_def;

0 commit comments

Comments
 (0)