|
14 | 14 | #include "structmember.h" // PyMemberDef
|
15 | 15 | #include "pycore_accu.h"
|
16 | 16 |
|
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 |
| - |
30 | 17 |
|
31 | 18 | typedef struct _PyScannerObject {
|
32 | 19 | PyObject_HEAD
|
@@ -1815,70 +1802,40 @@ PyDoc_STRVAR(module_doc,
|
1815 | 1802 | static int
|
1816 | 1803 | _json_exec(PyObject *module)
|
1817 | 1804 | {
|
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) { |
1822 | 1807 | return -1;
|
1823 | 1808 | }
|
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) { |
1827 | 1812 | return -1;
|
1828 | 1813 | }
|
1829 | 1814 |
|
1830 |
| - state->PyEncoderType = PyType_FromSpec(&PyEncoderType_spec); |
1831 |
| - if (state->PyEncoderType == NULL) { |
| 1815 | + PyObject *PyEncoderType = PyType_FromSpec(&PyEncoderType_spec); |
| 1816 | + if (PyEncoderType == NULL) { |
1832 | 1817 | return -1;
|
1833 | 1818 | }
|
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) { |
1837 | 1822 | return -1;
|
1838 | 1823 | }
|
1839 | 1824 |
|
1840 | 1825 | return 0;
|
1841 | 1826 | }
|
1842 | 1827 |
|
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 |
| - |
1867 | 1828 | static PyModuleDef_Slot _json_slots[] = {
|
1868 | 1829 | {Py_mod_exec, _json_exec},
|
1869 | 1830 | {0, NULL}
|
1870 | 1831 | };
|
1871 | 1832 |
|
1872 | 1833 | 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, |
1882 | 1839 | };
|
1883 | 1840 |
|
1884 | 1841 | PyMODINIT_FUNC
|
|
0 commit comments