@@ -161,26 +161,6 @@ _get_current_module(void)
161161 return mod ;
162162}
163163
164- static PyObject *
165- get_module_from_owned_type (PyTypeObject * cls )
166- {
167- assert (cls != NULL );
168- return _get_current_module ();
169- // XXX Use the more efficient API now that we use heap types:
170- //return PyType_GetModule(cls);
171- }
172-
173- static struct PyModuleDef moduledef ;
174-
175- static PyObject *
176- get_module_from_type (PyTypeObject * cls )
177- {
178- assert (cls != NULL );
179- return _get_current_module ();
180- // XXX Use the more efficient API now that we use heap types:
181- //return PyType_GetModuleByDef(cls, &moduledef);
182- }
183-
184164static PyObject *
185165add_new_exception (PyObject * mod , const char * name , PyObject * base )
186166{
@@ -265,6 +245,23 @@ get_module_state(PyObject *mod)
265245 return state ;
266246}
267247
248+ static struct PyModuleDef moduledef ;
249+
250+ static inline PyObject *
251+ get_module_from_type (PyTypeObject * tp )
252+ {
253+ assert (tp != NULL );
254+ PyObject * mod = PyType_GetModuleByDef (tp , & moduledef );
255+ assert (mod != NULL );
256+ return mod ;
257+ }
258+
259+ static inline module_state *
260+ get_module_state_from_type (PyTypeObject * tp )
261+ {
262+ return get_module_state (get_module_from_type (tp ));
263+ }
264+
268265static module_state *
269266_get_current_module_state (void )
270267{
@@ -363,9 +360,7 @@ static int
363360exceptions_init (PyObject * mod )
364361{
365362 module_state * state = get_module_state (mod );
366- if (state == NULL ) {
367- return -1 ;
368- }
363+ assert (state != NULL );
369364
370365#define ADD (NAME , BASE ) \
371366 do { \
@@ -2245,9 +2240,7 @@ static PyObject *
22452240new_channel_info (PyObject * mod , struct channel_info * info )
22462241{
22472242 module_state * state = get_module_state (mod );
2248- if (state == NULL ) {
2249- return NULL ;
2250- }
2243+ assert (state != NULL );
22512244
22522245 assert (state -> ChannelInfoType != NULL );
22532246 PyObject * self = PyStructSequence_New (state -> ChannelInfoType );
@@ -2505,14 +2498,8 @@ channelid_richcompare(PyObject *self, PyObject *other, int op)
25052498 Py_RETURN_NOTIMPLEMENTED ;
25062499 }
25072500
2508- PyObject * mod = get_module_from_type (Py_TYPE (self ));
2509- if (mod == NULL ) {
2510- return NULL ;
2511- }
2512- module_state * state = get_module_state (mod );
2513- if (state == NULL ) {
2514- goto done ;
2515- }
2501+ module_state * state = get_module_state_from_type (Py_TYPE (self ));
2502+ assert (state != NULL );
25162503
25172504 if (!PyObject_TypeCheck (self , state -> ChannelIDType )) {
25182505 res = Py_NewRef (Py_NotImplemented );
@@ -2556,7 +2543,6 @@ channelid_richcompare(PyObject *self, PyObject *other, int op)
25562543 }
25572544
25582545done :
2559- Py_DECREF (mod );
25602546 return res ;
25612547}
25622548
@@ -2596,9 +2582,7 @@ _channelid_from_xid(_PyXIData_t *data)
25962582 }
25972583 assert (mod != Py_None );
25982584 module_state * state = get_module_state (mod );
2599- if (state == NULL ) {
2600- goto done ;
2601- }
2585+ assert (state != NULL );
26022586
26032587 // Note that we do not preserve the "resolve" flag.
26042588 int err = newchannelid (state -> ChannelIDType , xid -> cid , xid -> end ,
@@ -2823,9 +2807,7 @@ static int
28232807set_channelend_types (PyObject * mod , PyTypeObject * send , PyTypeObject * recv )
28242808{
28252809 module_state * state = get_module_state (mod );
2826- if (state == NULL ) {
2827- return -1 ;
2828- }
2810+ assert (state != NULL );
28292811
28302812 // Clear the old values if the .py module was reloaded.
28312813 if (state -> send_channel_type != NULL ) {
@@ -2951,9 +2933,7 @@ channelsmod_create(PyObject *self, PyObject *args, PyObject *kwds)
29512933 return NULL ;
29522934 }
29532935 module_state * state = get_module_state (self );
2954- if (state == NULL ) {
2955- return NULL ;
2956- }
2936+ assert (state != NULL );
29572937 channelid * cidobj = NULL ;
29582938 int err = newchannelid (state -> ChannelIDType , cid , 0 ,
29592939 & _globals .channels , 0 , 0 ,
@@ -3018,11 +2998,7 @@ channelsmod_list_all(PyObject *self, PyObject *Py_UNUSED(ignored))
30182998 goto finally ;
30192999 }
30203000 module_state * state = get_module_state (self );
3021- if (state == NULL ) {
3022- Py_DECREF (ids );
3023- ids = NULL ;
3024- goto finally ;
3025- }
3001+ assert (state != NULL );
30263002 struct channel_id_and_info * cur = cids ;
30273003 for (int64_t i = 0 ; i < count ; cur ++ , i ++ ) {
30283004 PyObject * cidobj = NULL ;
@@ -3485,16 +3461,8 @@ static PyObject *
34853461channelsmod__channel_id (PyObject * self , PyObject * args , PyObject * kwds )
34863462{
34873463 module_state * state = get_module_state (self );
3488- if (state == NULL ) {
3489- return NULL ;
3490- }
3491- PyTypeObject * cls = state -> ChannelIDType ;
3492-
3493- PyObject * mod = get_module_from_owned_type (cls );
3494- assert (mod == self );
3495- Py_DECREF (mod );
3496-
3497- return _channelid_new (self , cls , args , kwds );
3464+ assert (state != NULL );
3465+ return _channelid_new (self , state -> ChannelIDType , args , kwds );
34983466}
34993467
35003468static PyObject *
0 commit comments