Skip to content

Commit 3a2e615

Browse files
author
Erlend Egeberg Aasland
authored
gh-94393: Remove unneeded module state from _json (#94394)
1 parent 1bc8a38 commit 3a2e615

File tree

1 file changed

+15
-58
lines changed

1 file changed

+15
-58
lines changed

Modules/_json.c

+15-58
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,6 @@
1414
#include "structmember.h" // PyMemberDef
1515
#include "pycore_accu.h"
1616

17-
typedef struct {
18-
PyObject *PyScannerType;
19-
PyObject *PyEncoderType;
20-
} _jsonmodulestate;
21-
22-
static inline _jsonmodulestate*
23-
get_json_state(PyObject *module)
24-
{
25-
void *state = PyModule_GetState(module);
26-
assert(state != NULL);
27-
return (_jsonmodulestate *)state;
28-
}
29-
3017

3118
typedef struct _PyScannerObject {
3219
PyObject_HEAD
@@ -1815,70 +1802,40 @@ PyDoc_STRVAR(module_doc,
18151802
static int
18161803
_json_exec(PyObject *module)
18171804
{
1818-
_jsonmodulestate *state = get_json_state(module);
1819-
1820-
state->PyScannerType = PyType_FromSpec(&PyScannerType_spec);
1821-
if (state->PyScannerType == NULL) {
1805+
PyObject *PyScannerType = PyType_FromSpec(&PyScannerType_spec);
1806+
if (PyScannerType == NULL) {
18221807
return -1;
18231808
}
1824-
Py_INCREF(state->PyScannerType);
1825-
if (PyModule_AddObject(module, "make_scanner", state->PyScannerType) < 0) {
1826-
Py_DECREF(state->PyScannerType);
1809+
int rc = PyModule_AddObjectRef(module, "make_scanner", PyScannerType);
1810+
Py_DECREF(PyScannerType);
1811+
if (rc < 0) {
18271812
return -1;
18281813
}
18291814

1830-
state->PyEncoderType = PyType_FromSpec(&PyEncoderType_spec);
1831-
if (state->PyEncoderType == NULL) {
1815+
PyObject *PyEncoderType = PyType_FromSpec(&PyEncoderType_spec);
1816+
if (PyEncoderType == NULL) {
18321817
return -1;
18331818
}
1834-
Py_INCREF(state->PyEncoderType);
1835-
if (PyModule_AddObject(module, "make_encoder", state->PyEncoderType) < 0) {
1836-
Py_DECREF(state->PyEncoderType);
1819+
rc = PyModule_AddObjectRef(module, "make_encoder", PyEncoderType);
1820+
Py_DECREF(PyEncoderType);
1821+
if (rc < 0) {
18371822
return -1;
18381823
}
18391824

18401825
return 0;
18411826
}
18421827

1843-
static int
1844-
_jsonmodule_traverse(PyObject *module, visitproc visit, void *arg)
1845-
{
1846-
_jsonmodulestate *state = get_json_state(module);
1847-
Py_VISIT(state->PyScannerType);
1848-
Py_VISIT(state->PyEncoderType);
1849-
return 0;
1850-
}
1851-
1852-
static int
1853-
_jsonmodule_clear(PyObject *module)
1854-
{
1855-
_jsonmodulestate *state = get_json_state(module);
1856-
Py_CLEAR(state->PyScannerType);
1857-
Py_CLEAR(state->PyEncoderType);
1858-
return 0;
1859-
}
1860-
1861-
static void
1862-
_jsonmodule_free(void *module)
1863-
{
1864-
_jsonmodule_clear((PyObject *)module);
1865-
}
1866-
18671828
static PyModuleDef_Slot _json_slots[] = {
18681829
{Py_mod_exec, _json_exec},
18691830
{0, NULL}
18701831
};
18711832

18721833
static struct PyModuleDef jsonmodule = {
1873-
PyModuleDef_HEAD_INIT,
1874-
"_json",
1875-
module_doc,
1876-
sizeof(_jsonmodulestate),
1877-
speedups_methods,
1878-
_json_slots,
1879-
_jsonmodule_traverse,
1880-
_jsonmodule_clear,
1881-
_jsonmodule_free,
1834+
.m_base = PyModuleDef_HEAD_INIT,
1835+
.m_name = "_json",
1836+
.m_doc = module_doc,
1837+
.m_methods = speedups_methods,
1838+
.m_slots = _json_slots,
18821839
};
18831840

18841841
PyMODINIT_FUNC

0 commit comments

Comments
 (0)