Skip to content

Commit 38fa7c5

Browse files
committed
bpo-46541: Remove usage of _Py_IDENTIFIER from dbms modules
1 parent 3be1a44 commit 38fa7c5

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

Modules/_dbmmodule.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11

22
/* DBM module using dictionary interface */
33

4-
54
#define PY_SSIZE_T_CLEAN
6-
#define NEEDS_PY_IDENTIFIER
75
#include "Python.h"
86

97
#include <sys/types.h>
@@ -38,6 +36,7 @@
3836
typedef struct {
3937
PyTypeObject *dbm_type;
4038
PyObject *dbm_error;
39+
PyObject *str_close;
4140
} _dbm_state;
4241

4342
static inline _dbm_state*
@@ -396,8 +395,9 @@ dbm__enter__(PyObject *self, PyObject *args)
396395
static PyObject *
397396
dbm__exit__(PyObject *self, PyObject *args)
398397
{
399-
_Py_IDENTIFIER(close);
400-
return _PyObject_CallMethodIdNoArgs(self, &PyId_close);
398+
_dbm_state *state = PyType_GetModuleState(Py_TYPE(self));
399+
assert(state != NULL);
400+
return PyObject_CallMethodNoArgs(self, state->str_close);
401401
}
402402

403403
static PyMethodDef dbm_methods[] = {
@@ -526,6 +526,12 @@ _dbm_exec(PyObject *module)
526526
if (PyModule_AddType(module, (PyTypeObject *)state->dbm_error) < 0) {
527527
return -1;
528528
}
529+
530+
PyObject *str_close = PyUnicode_InternFromString("close");
531+
if (str_close == NULL) {
532+
return -1;
533+
}
534+
state->str_close = str_close;
529535
return 0;
530536
}
531537

@@ -535,6 +541,7 @@ _dbm_module_traverse(PyObject *module, visitproc visit, void *arg)
535541
_dbm_state *state = get_dbm_state(module);
536542
Py_VISIT(state->dbm_error);
537543
Py_VISIT(state->dbm_type);
544+
Py_VISIT(state->str_close);
538545
return 0;
539546
}
540547

@@ -544,6 +551,7 @@ _dbm_module_clear(PyObject *module)
544551
_dbm_state *state = get_dbm_state(module);
545552
Py_CLEAR(state->dbm_error);
546553
Py_CLEAR(state->dbm_type);
554+
Py_CLEAR(state->str_close);
547555
return 0;
548556
}
549557

Modules/_gdbmmodule.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
/* Doc strings: Mitch Chapman */
55

66
#define PY_SSIZE_T_CLEAN
7-
#define NEEDS_PY_IDENTIFIER
87
#include "Python.h"
98
#include "gdbm.h"
109

@@ -21,6 +20,7 @@ extern const char * gdbm_strerror(gdbm_error);
2120
typedef struct {
2221
PyTypeObject *gdbm_type;
2322
PyObject *gdbm_error;
23+
PyObject *str_close;
2424
} _gdbm_state;
2525

2626
static inline _gdbm_state*
@@ -545,8 +545,9 @@ gdbm__enter__(PyObject *self, PyObject *args)
545545
static PyObject *
546546
gdbm__exit__(PyObject *self, PyObject *args)
547547
{
548-
_Py_IDENTIFIER(close);
549-
return _PyObject_CallMethodIdNoArgs(self, &PyId_close);
548+
_gdbm_state *state = PyType_GetModuleState(Py_TYPE(self));
549+
assert(state != NULL);
550+
return PyObject_CallMethodNoArgs(self, state->str_close);
550551
}
551552

552553
static PyMethodDef gdbm_methods[] = {
@@ -740,6 +741,11 @@ _gdbm_exec(PyObject *module)
740741
return -1;
741742
}
742743
#endif
744+
PyObject *str_close = PyUnicode_InternFromString("close");
745+
if (str_close == NULL) {
746+
return -1;
747+
}
748+
state->str_close = str_close;
743749
return 0;
744750
}
745751

@@ -749,6 +755,7 @@ _gdbm_module_traverse(PyObject *module, visitproc visit, void *arg)
749755
_gdbm_state *state = get_gdbm_state(module);
750756
Py_VISIT(state->gdbm_error);
751757
Py_VISIT(state->gdbm_type);
758+
Py_VISIT(state->str_close);
752759
return 0;
753760
}
754761

@@ -758,6 +765,7 @@ _gdbm_module_clear(PyObject *module)
758765
_gdbm_state *state = get_gdbm_state(module);
759766
Py_CLEAR(state->gdbm_error);
760767
Py_CLEAR(state->gdbm_type);
768+
Py_CLEAR(state->str_close);
761769
return 0;
762770
}
763771

0 commit comments

Comments
 (0)