@@ -108,6 +108,7 @@ Local naming conventions:
108
108
#define PY_SSIZE_T_CLEAN
109
109
#include "Python.h"
110
110
#include "pycore_fileutils.h" // _Py_set_inheritable()
111
+ #include "pycore_moduleobject.h" // _PyModule_GetState
111
112
#include "structmember.h" // PyMemberDef
112
113
113
114
#ifdef _Py_MEMORY_SANITIZER
@@ -569,11 +570,25 @@ typedef struct _socket_state {
569
570
#endif
570
571
} socket_state ;
571
572
572
- static socket_state global_state ;
573
+ static inline socket_state *
574
+ get_module_state (PyObject * mod )
575
+ {
576
+ void * state = _PyModule_GetState (mod );
577
+ assert (state != NULL );
578
+ return (socket_state * )state ;
579
+ }
580
+
581
+ static struct PyModuleDef socketmodule ;
573
582
574
- #define GLOBAL_STATE () (&global_state)
583
+ static inline socket_state *
584
+ find_module_state_by_def (PyTypeObject * type )
585
+ {
586
+ PyObject * mod = PyType_GetModuleByDef (type , & socketmodule );
587
+ assert (mod != NULL );
588
+ return get_module_state (mod );
589
+ }
575
590
576
- #define clinic_state () GLOBAL_STATE( )
591
+ #define clinic_state () (find_module_state_by_def(type) )
577
592
#include "clinic/socketmodule.c.h"
578
593
#undef clinic_state
579
594
@@ -5334,7 +5349,7 @@ sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto,
5334
5349
{
5335
5350
5336
5351
SOCKET_T fd = INVALID_SOCKET ;
5337
- socket_state * state = GLOBAL_STATE ( );
5352
+ socket_state * state = find_module_state_by_def ( Py_TYPE ( self ) );
5338
5353
5339
5354
#ifndef MS_WINDOWS
5340
5355
#ifdef SOCK_CLOEXEC
@@ -5683,7 +5698,7 @@ socket_gethostbyname(PyObject *self, PyObject *args)
5683
5698
if (PySys_Audit ("socket.gethostbyname" , "O" , args ) < 0 ) {
5684
5699
goto finally ;
5685
5700
}
5686
- socket_state * state = GLOBAL_STATE ( );
5701
+ socket_state * state = get_module_state ( self );
5687
5702
int rc = setipaddr (state , name , (struct sockaddr * )& addrbuf ,
5688
5703
sizeof (addrbuf ), AF_INET );
5689
5704
if (rc < 0 ) {
@@ -5878,7 +5893,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
5878
5893
if (PySys_Audit ("socket.gethostbyname" , "O" , args ) < 0 ) {
5879
5894
goto finally ;
5880
5895
}
5881
- socket_state * state = GLOBAL_STATE ( );
5896
+ socket_state * state = get_module_state ( self );
5882
5897
if (setipaddr (state , name , SAS2SA (& addr ), sizeof (addr ), AF_INET ) < 0 ) {
5883
5898
goto finally ;
5884
5899
}
@@ -5963,7 +5978,7 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
5963
5978
goto finally ;
5964
5979
}
5965
5980
af = AF_UNSPEC ;
5966
- socket_state * state = GLOBAL_STATE ( );
5981
+ socket_state * state = get_module_state ( self );
5967
5982
if (setipaddr (state , ip_num , sa , sizeof (addr ), af ) < 0 ) {
5968
5983
goto finally ;
5969
5984
}
@@ -6226,7 +6241,7 @@ socket_socketpair(PyObject *self, PyObject *args)
6226
6241
SOCKET_T sv [2 ];
6227
6242
int family , type = SOCK_STREAM , proto = 0 ;
6228
6243
PyObject * res = NULL ;
6229
- socket_state * state = GLOBAL_STATE ( );
6244
+ socket_state * state = get_module_state ( self );
6230
6245
#ifdef SOCK_CLOEXEC
6231
6246
int * atomic_flag_works = & (state -> sock_cloexec_works );
6232
6247
#else
@@ -6732,7 +6747,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
6732
6747
Py_END_ALLOW_THREADS
6733
6748
if (error ) {
6734
6749
res0 = NULL ; // gh-100795
6735
- socket_state * state = GLOBAL_STATE ( );
6750
+ socket_state * state = get_module_state ( self );
6736
6751
set_gaierror (state , error );
6737
6752
goto err ;
6738
6753
}
@@ -6832,7 +6847,7 @@ socket_getnameinfo(PyObject *self, PyObject *args)
6832
6847
Py_END_ALLOW_THREADS
6833
6848
if (error ) {
6834
6849
res = NULL ; // gh-100795
6835
- socket_state * state = GLOBAL_STATE ( );
6850
+ socket_state * state = get_module_state ( self );
6836
6851
set_gaierror (state , error );
6837
6852
goto fail ;
6838
6853
}
@@ -6865,7 +6880,7 @@ socket_getnameinfo(PyObject *self, PyObject *args)
6865
6880
error = getnameinfo (res -> ai_addr , (socklen_t ) res -> ai_addrlen ,
6866
6881
hbuf , sizeof (hbuf ), pbuf , sizeof (pbuf ), flags );
6867
6882
if (error ) {
6868
- socket_state * state = GLOBAL_STATE ( );
6883
+ socket_state * state = get_module_state ( self );
6869
6884
set_gaierror (state , error );
6870
6885
goto fail ;
6871
6886
}
@@ -6892,7 +6907,7 @@ Get host and port for a sockaddr.");
6892
6907
static PyObject *
6893
6908
socket_getdefaulttimeout (PyObject * self , PyObject * Py_UNUSED (ignored ))
6894
6909
{
6895
- socket_state * state = GLOBAL_STATE ( );
6910
+ socket_state * state = get_module_state ( self );
6896
6911
if (state -> defaulttimeout < 0 ) {
6897
6912
Py_RETURN_NONE ;
6898
6913
}
@@ -6917,7 +6932,7 @@ socket_setdefaulttimeout(PyObject *self, PyObject *arg)
6917
6932
if (socket_parse_timeout (& timeout , arg ) < 0 )
6918
6933
return NULL ;
6919
6934
6920
- socket_state * state = GLOBAL_STATE ( );
6935
+ socket_state * state = get_module_state ( self );
6921
6936
state -> defaulttimeout = timeout ;
6922
6937
6923
6938
Py_RETURN_NONE ;
@@ -7334,27 +7349,16 @@ PyDoc_STRVAR(socket_doc,
7334
7349
\n\
7335
7350
See the socket module for documentation." );
7336
7351
7337
- static struct PyModuleDef socketmodule = {
7338
- .m_base = PyModuleDef_HEAD_INIT ,
7339
- .m_name = PySocket_MODULE_NAME ,
7340
- .m_doc = socket_doc ,
7341
- .m_size = sizeof (socket_state ),
7342
- .m_methods = socket_methods ,
7343
- };
7344
-
7345
- PyMODINIT_FUNC
7346
- PyInit__socket (void )
7352
+ static int
7353
+ socket_exec (PyObject * m )
7347
7354
{
7348
- PyObject * m , * has_ipv6 ;
7349
-
7350
- if (!os_init ())
7351
- return NULL ;
7355
+ PyObject * has_ipv6 ;
7352
7356
7353
- m = PyModule_Create ( & socketmodule );
7354
- if ( m == NULL )
7355
- return NULL ;
7357
+ if (! os_init ()) {
7358
+ return -1 ;
7359
+ }
7356
7360
7357
- socket_state * state = GLOBAL_STATE ( );
7361
+ socket_state * state = get_module_state ( m );
7358
7362
state -> defaulttimeout = _PYTIME_FROMSECONDS (-1 );
7359
7363
7360
7364
#if defined(HAVE_ACCEPT ) || defined(HAVE_ACCEPT4 )
@@ -7371,36 +7375,36 @@ PyInit__socket(void)
7371
7375
state -> socket_herror = PyErr_NewException ("socket.herror" ,
7372
7376
PyExc_OSError , NULL );
7373
7377
if (state -> socket_herror == NULL ) {
7374
- return NULL ;
7378
+ return -1 ;
7375
7379
}
7376
7380
if (PyModule_AddObjectRef (m , "error" , PyExc_OSError ) < 0 ) {
7377
- return NULL ;
7381
+ return -1 ;
7378
7382
}
7379
7383
if (PyModule_AddObjectRef (m , "herror" , state -> socket_herror ) < 0 ) {
7380
- return NULL ;
7384
+ return -1 ;
7381
7385
}
7382
7386
state -> socket_gaierror = PyErr_NewException ("socket.gaierror" ,
7383
7387
PyExc_OSError , NULL );
7384
7388
if (state -> socket_gaierror == NULL ) {
7385
- return NULL ;
7389
+ return -1 ;
7386
7390
}
7387
7391
if (PyModule_AddObjectRef (m , "gaierror" , state -> socket_gaierror ) < 0 ) {
7388
- return NULL ;
7392
+ return -1 ;
7389
7393
}
7390
7394
if (PyModule_AddObjectRef (m , "timeout" , PyExc_TimeoutError ) < 0 ) {
7391
- return NULL ;
7395
+ return -1 ;
7392
7396
}
7393
7397
7394
7398
PyObject * sock_type = PyType_FromMetaclass (NULL , m , & sock_spec , NULL );
7395
7399
if (sock_type == NULL ) {
7396
- return NULL ;
7400
+ return -1 ;
7397
7401
}
7398
7402
state -> sock_type = (PyTypeObject * )sock_type ;
7399
7403
if (PyModule_AddObjectRef (m , "SocketType" , sock_type ) < 0 ) {
7400
- return NULL ;
7404
+ return -1 ;
7401
7405
}
7402
7406
if (PyModule_AddObjectRef (m , "socket" , sock_type ) < 0 ) {
7403
- return NULL ;
7407
+ return -1 ;
7404
7408
}
7405
7409
7406
7410
#ifdef ENABLE_IPV6
@@ -7413,21 +7417,18 @@ PyInit__socket(void)
7413
7417
/* Export C API */
7414
7418
PySocketModule_APIObject * capi = sock_get_api (state );
7415
7419
if (capi == NULL ) {
7416
- Py_DECREF (m );
7417
- return NULL ;
7420
+ return -1 ;
7418
7421
}
7419
7422
PyObject * capsule = PyCapsule_New (capi ,
7420
7423
PySocket_CAPSULE_NAME ,
7421
7424
sock_destroy_api );
7422
7425
if (capsule == NULL ) {
7423
7426
sock_free_api (capi );
7424
- Py_DECREF (m );
7425
- return NULL ;
7427
+ return -1 ;
7426
7428
}
7427
7429
if (PyModule_AddObject (m , PySocket_CAPI_NAME , capsule ) < 0 ) {
7428
7430
Py_DECREF (capsule );
7429
- Py_DECREF (m );
7430
- return NULL ;
7431
+ return -1 ;
7431
7432
}
7432
7433
7433
7434
/* Address families (we only support AF_INET and AF_UNIX) */
@@ -8782,7 +8783,7 @@ PyInit__socket(void)
8782
8783
PyObject * tmp ;
8783
8784
tmp = PyLong_FromUnsignedLong (codes [i ]);
8784
8785
if (tmp == NULL )
8785
- return NULL ;
8786
+ return -1 ;
8786
8787
PyModule_AddObject (m , names [i ], tmp );
8787
8788
}
8788
8789
}
@@ -8805,10 +8806,58 @@ PyInit__socket(void)
8805
8806
#ifdef MS_WINDOWS
8806
8807
/* remove some flags on older version Windows during run-time */
8807
8808
if (remove_unusable_flags (m ) < 0 ) {
8808
- Py_DECREF (m );
8809
- return NULL ;
8809
+ return -1 ;
8810
8810
}
8811
8811
#endif
8812
8812
8813
- return m ;
8813
+ return 0 ;
8814
+ }
8815
+
8816
+ static struct PyModuleDef_Slot socket_slots [] = {
8817
+ {Py_mod_exec , socket_exec },
8818
+ {0 , NULL },
8819
+ };
8820
+
8821
+ static int
8822
+ socket_traverse (PyObject * mod , visitproc visit , void * arg )
8823
+ {
8824
+ socket_state * state = get_module_state (mod );
8825
+ Py_VISIT (state -> sock_type );
8826
+ Py_VISIT (state -> socket_herror );
8827
+ Py_VISIT (state -> socket_gaierror );
8828
+ return 0 ;
8829
+ }
8830
+
8831
+ static int
8832
+ socket_clear (PyObject * mod )
8833
+ {
8834
+ socket_state * state = get_module_state (mod );
8835
+ Py_CLEAR (state -> sock_type );
8836
+ Py_CLEAR (state -> socket_herror );
8837
+ Py_CLEAR (state -> socket_gaierror );
8838
+ return 0 ;
8839
+ }
8840
+
8841
+ static void
8842
+ socket_free (void * mod )
8843
+ {
8844
+ (void )socket_clear ((PyObject * )mod );
8845
+ }
8846
+
8847
+ static struct PyModuleDef socketmodule = {
8848
+ .m_base = PyModuleDef_HEAD_INIT ,
8849
+ .m_name = PySocket_MODULE_NAME ,
8850
+ .m_doc = socket_doc ,
8851
+ .m_size = sizeof (socket_state ),
8852
+ .m_methods = socket_methods ,
8853
+ .m_slots = socket_slots ,
8854
+ .m_traverse = socket_traverse ,
8855
+ .m_clear = socket_clear ,
8856
+ .m_free = socket_free ,
8857
+ };
8858
+
8859
+ PyMODINIT_FUNC
8860
+ PyInit__socket (void )
8861
+ {
8862
+ return PyModuleDef_Init (& socketmodule );
8814
8863
}
0 commit comments