Changeset 388 for python/vendor/current/Modules/socketmodule.h
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Modules/socketmodule.h
r2 r388 18 18 /* VC6 is shipped with old platform headers, and does not have MSTcpIP.h 19 19 * Separate SDKs have all the functions we want, but older ones don't have 20 * any version information. 20 * any version information. 21 21 * I use SIO_GET_MULTICAST_FILTER to detect a decent SDK. 22 22 */ … … 77 77 78 78 /* Python module and C API name */ 79 #define PySocket_MODULE_NAME "_socket" 80 #define PySocket_CAPI_NAME "CAPI" 79 #define PySocket_MODULE_NAME "_socket" 80 #define PySocket_CAPI_NAME "CAPI" 81 #define PySocket_CAPSULE_NAME (PySocket_MODULE_NAME "." PySocket_CAPI_NAME) 81 82 82 83 /* Abstract the socket file descriptor type */ 83 84 #ifdef MS_WINDOWS 84 85 typedef SOCKET SOCKET_T; 85 # 86 # 87 # 88 # 89 # 86 # ifdef MS_WIN64 87 # define SIZEOF_SOCKET_T 8 88 # else 89 # define SIZEOF_SOCKET_T 4 90 # endif 90 91 #else 91 92 typedef int SOCKET_T; 92 # 93 # define SIZEOF_SOCKET_T SIZEOF_INT 93 94 #endif 94 95 95 96 /* Socket address */ 96 97 typedef union sock_addr { 97 98 struct sockaddr_in in; 98 99 #ifdef AF_UNIX 99 100 struct sockaddr_un un; 100 101 #endif 101 102 #ifdef AF_NETLINK 102 103 struct sockaddr_nl nl; 103 104 #endif 104 105 #ifdef ENABLE_IPV6 105 106 106 struct sockaddr_in6 in6; 107 struct sockaddr_storage storage; 107 108 #endif 108 109 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H 109 110 111 112 110 struct sockaddr_l2 bt_l2; 111 struct sockaddr_rc bt_rc; 112 struct sockaddr_sco bt_sco; 113 struct sockaddr_hci bt_hci; 113 114 #endif 114 115 #ifdef HAVE_NETPACKET_PACKET_H 115 116 struct sockaddr_ll ll; 116 117 #endif 117 118 } sock_addr_t; … … 122 123 123 124 typedef struct { 124 125 SOCKET_T sock_fd;/* Socket file descriptor */126 int sock_family;/* Address family, e.g., AF_INET */127 int sock_type;/* Socket type, e.g., SOCK_STREAM */128 int sock_proto;/* Protocol type, usually 0 */129 130 131 132 double sock_timeout;/* Operation timeout in seconds;133 125 PyObject_HEAD 126 SOCKET_T sock_fd; /* Socket file descriptor */ 127 int sock_family; /* Address family, e.g., AF_INET */ 128 int sock_type; /* Socket type, e.g., SOCK_STREAM */ 129 int sock_proto; /* Protocol type, usually 0 */ 130 PyObject *(*errorhandler)(void); /* Error handler; checks 131 errno, returns NULL and 132 sets a Python exception */ 133 double sock_timeout; /* Operation timeout in seconds; 134 0.0 means non-blocking */ 134 135 } PySocketSockObject; 135 136 … … 139 140 and how it works: 140 141 141 The _ssl module needs access to the type object defined in 142 The _ssl module needs access to the type object defined in 142 143 the _socket module. Since cross-DLL linking introduces a lot of 143 144 problems on many platforms, the "trick" is to wrap the 144 145 C API of a module in a struct which then gets exported to 145 other modules via a PyC Object.146 other modules via a PyCapsule. 146 147 147 148 The code in socketmodule.c defines this struct (which currently 148 149 only contains the type object reference, but could very 149 150 well also include other C APIs needed by other modules) 150 and exports it as PyC Objectvia the module dictionary151 and exports it as PyCapsule via the module dictionary 151 152 under the name "CAPI". 152 153 … … 161 162 Load _socket module and its C API; this sets up the global 162 163 PySocketModule: 163 164 165 164 165 if (PySocketModule_ImportModuleAndAPI()) 166 return; 166 167 167 168 … … 169 170 module: 170 171 171 172 173 174 175 176 177 172 if (!PyArg_ParseTuple(args, "O!|zz:ssl", 173 174 PySocketModule.Sock_Type, 175 176 (PyObject*)&Sock, 177 &key_file, &cert_file)) 178 return NULL; 178 179 179 180 Support could easily be extended to export more C APIs/symbols 180 this way. Currently, only the type object is exported, 181 this way. Currently, only the type object is exported, 181 182 other candidates would be socket constructors and socket 182 183 access functions. … … 186 187 /* C API for usage by other Python modules */ 187 188 typedef struct { 188 189 189 PyTypeObject *Sock_Type; 190 PyObject *error; 190 191 } PySocketModule_APIObject; 191 192 192 193 /* XXX The net effect of the following appears to be to define a function 193 194 XXX named PySocketModule_APIObject in _ssl.c. It's unclear why it isn't 194 XXX defined there directly. 195 XXX defined there directly. 195 196 196 197 >>> It's defined here because other modules might also want to use … … 208 209 if (!PyArg_ParseTuple(args, "O!|zz:ssl", 209 210 &PySocketModule.Sock_Type, (PyObject*)&Sock, 210 211 211 &key_file, &cert_file)) 212 return NULL; 212 213 ... 213 214 */ … … 227 228 int PySocketModule_ImportModuleAndAPI(void) 228 229 { 229 PyObject *mod = 0, *v = 0; 230 char *apimodule = PySocket_MODULE_NAME; 231 char *apiname = PySocket_CAPI_NAME; 232 void *api; 233 234 DPRINTF("Importing the %s C API...\n", apimodule); 235 mod = PyImport_ImportModuleNoBlock(apimodule); 236 if (mod == NULL) 237 goto onError; 238 DPRINTF(" %s package found\n", apimodule); 239 v = PyObject_GetAttrString(mod, apiname); 240 if (v == NULL) 241 goto onError; 242 Py_DECREF(mod); 243 DPRINTF(" API object %s found\n", apiname); 244 api = PyCObject_AsVoidPtr(v); 245 if (api == NULL) 246 goto onError; 247 Py_DECREF(v); 248 memcpy(&PySocketModule, api, sizeof(PySocketModule)); 249 DPRINTF(" API object loaded and initialized.\n"); 250 return 0; 230 void *api; 231 232 DPRINTF(" Loading capsule %s\n", PySocket_CAPSULE_NAME); 233 api = PyCapsule_Import(PySocket_CAPSULE_NAME, 1); 234 if (api == NULL) 235 goto onError; 236 memcpy(&PySocketModule, api, sizeof(PySocketModule)); 237 DPRINTF(" API object loaded and initialized.\n"); 238 return 0; 251 239 252 240 onError: 253 DPRINTF(" not found.\n"); 254 Py_XDECREF(mod); 255 Py_XDECREF(v); 256 return -1; 241 DPRINTF(" not found.\n"); 242 return -1; 257 243 } 258 244
Note:
See TracChangeset
for help on using the changeset viewer.