11#include <Python.h>
22#include <pythread.h>
3- #include <stdatomic.h>
43#include <stdbool.h>
54#include <stdint.h>
6- #include <time.h>
75
6+ #include "module.h"
87#include "sonyflake.h"
98#include "machine_ids.h"
109
10+ static int sonyflake_module_traverse (PyObject * m , visitproc visit , void * arg ) {
11+ struct sonyflake_module_state * state = PyModule_GetState (m );
12+ Py_VISIT (state -> sonyflake_cls );
13+ Py_VISIT (state -> machine_id_lcg_cls );
14+ return 0 ;
15+ }
16+
17+ static int sonyflake_module_clear (PyObject * m ) {
18+ struct sonyflake_module_state * state = PyModule_GetState (m );
19+ Py_CLEAR (state -> sonyflake_cls );
20+ Py_CLEAR (state -> machine_id_lcg_cls );
21+ return 0 ;
22+ }
23+
24+ static void sonyflake_module_free (void * m ) {
25+ sonyflake_module_clear (m );
26+ }
27+
1128static int sonyflake_exec (PyObject * module );
1229
1330PyModuleDef_Slot sonyflake_slots [] = {
@@ -25,8 +42,11 @@ struct PyModuleDef sonyflake_module = {
2542 PyModuleDef_HEAD_INIT ,
2643 .m_name = MODULE_NAME ,
2744 .m_doc = "" ,
28- .m_size = 0 ,
45+ .m_size = sizeof ( struct sonyflake_module_state ) ,
2946 .m_slots = sonyflake_slots ,
47+ .m_traverse = sonyflake_module_traverse ,
48+ .m_clear = sonyflake_module_clear ,
49+ .m_free = sonyflake_module_free ,
3050};
3151
3252PyMODINIT_FUNC
@@ -36,26 +56,29 @@ PyInit__sonyflake(void)
3656}
3757
3858static int sonyflake_exec (PyObject * module ) {
39- PyObject * sonyflake_cls , * machine_id_lcg_cls ;
59+ struct sonyflake_module_state * state = PyModule_GetState (module );
60+
61+ state -> sonyflake_cls = NULL ;
62+ state -> machine_id_lcg_cls = NULL ;
4063
41- sonyflake_cls = PyType_FromSpec ( & sonyflake_type_spec );
64+ state -> sonyflake_cls = PyType_FromModuleAndSpec ( module , & sonyflake_type_spec , NULL );
4265
43- if (!sonyflake_cls ) {
66+ if (!state -> sonyflake_cls ) {
4467 goto err ;
4568 }
4669
47- if (PyModule_AddObject (module , "SonyFlake" , sonyflake_cls ) < 0 ) {
48- goto err_sf ;
70+ if (PyModule_AddObjectRef (module , "SonyFlake" , state -> sonyflake_cls ) < 0 ) {
71+ goto err ;
4972 }
5073
51- machine_id_lcg_cls = PyType_FromSpec ( & machine_id_lcg_spec );
74+ state -> machine_id_lcg_cls = PyType_FromModuleAndSpec ( module , & machine_id_lcg_spec , NULL );
5275
53- if (!machine_id_lcg_cls ) {
54- goto err_lcg ;
76+ if (!state -> machine_id_lcg_cls ) {
77+ goto err ;
5578 }
5679
57- if (PyModule_AddObject (module , "MachineIDLCG" , machine_id_lcg_cls ) < 0 ) {
58- goto err_lcg ;
80+ if (PyModule_AddObjectRef (module , "MachineIDLCG" , state -> machine_id_lcg_cls ) < 0 ) {
81+ goto err ;
5982 }
6083
6184 PyModule_AddIntMacro (module , SONYFLAKE_EPOCH );
@@ -68,10 +91,9 @@ static int sonyflake_exec(PyObject *module) {
6891
6992 return 0 ;
7093
71- err_lcg :
72- Py_DECREF (machine_id_lcg_cls );
73- err_sf :
74- Py_DECREF (sonyflake_cls );
7594err :
95+ Py_CLEAR (state -> machine_id_lcg_cls );
96+ Py_CLEAR (state -> sonyflake_cls );
97+
7698 return -1 ;
7799}
0 commit comments