Skip to content

Commit 7bd964d

Browse files
Wulian233eendebakptpicnixzgpsheadhugovk
authored
gh-121423: Improve import time of socket (#121424)
Improve import time of `socket` by writing `socket.errorTab` as a constant and lazy import modules. Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Gregory P. Smith <greg@krypto.org> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
1 parent 91ff700 commit 7bd964d

File tree

2 files changed

+109
-101
lines changed

2 files changed

+109
-101
lines changed

Lib/socket.py

Lines changed: 107 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
import _socket
5353
from _socket import *
5454

55-
import os, sys, io, selectors
55+
import io
56+
import os
57+
import sys
5658
from enum import IntEnum, IntFlag
5759

5860
try:
@@ -110,102 +112,103 @@ def _intenum_converter(value, enum_klass):
110112

111113
# WSA error codes
112114
if sys.platform.lower().startswith("win"):
113-
errorTab = {}
114-
errorTab[6] = "Specified event object handle is invalid."
115-
errorTab[8] = "Insufficient memory available."
116-
errorTab[87] = "One or more parameters are invalid."
117-
errorTab[995] = "Overlapped operation aborted."
118-
errorTab[996] = "Overlapped I/O event object not in signaled state."
119-
errorTab[997] = "Overlapped operation will complete later."
120-
errorTab[10004] = "The operation was interrupted."
121-
errorTab[10009] = "A bad file handle was passed."
122-
errorTab[10013] = "Permission denied."
123-
errorTab[10014] = "A fault occurred on the network??" # WSAEFAULT
124-
errorTab[10022] = "An invalid operation was attempted."
125-
errorTab[10024] = "Too many open files."
126-
errorTab[10035] = "The socket operation would block."
127-
errorTab[10036] = "A blocking operation is already in progress."
128-
errorTab[10037] = "Operation already in progress."
129-
errorTab[10038] = "Socket operation on nonsocket."
130-
errorTab[10039] = "Destination address required."
131-
errorTab[10040] = "Message too long."
132-
errorTab[10041] = "Protocol wrong type for socket."
133-
errorTab[10042] = "Bad protocol option."
134-
errorTab[10043] = "Protocol not supported."
135-
errorTab[10044] = "Socket type not supported."
136-
errorTab[10045] = "Operation not supported."
137-
errorTab[10046] = "Protocol family not supported."
138-
errorTab[10047] = "Address family not supported by protocol family."
139-
errorTab[10048] = "The network address is in use."
140-
errorTab[10049] = "Cannot assign requested address."
141-
errorTab[10050] = "Network is down."
142-
errorTab[10051] = "Network is unreachable."
143-
errorTab[10052] = "Network dropped connection on reset."
144-
errorTab[10053] = "Software caused connection abort."
145-
errorTab[10054] = "The connection has been reset."
146-
errorTab[10055] = "No buffer space available."
147-
errorTab[10056] = "Socket is already connected."
148-
errorTab[10057] = "Socket is not connected."
149-
errorTab[10058] = "The network has been shut down."
150-
errorTab[10059] = "Too many references."
151-
errorTab[10060] = "The operation timed out."
152-
errorTab[10061] = "Connection refused."
153-
errorTab[10062] = "Cannot translate name."
154-
errorTab[10063] = "The name is too long."
155-
errorTab[10064] = "The host is down."
156-
errorTab[10065] = "The host is unreachable."
157-
errorTab[10066] = "Directory not empty."
158-
errorTab[10067] = "Too many processes."
159-
errorTab[10068] = "User quota exceeded."
160-
errorTab[10069] = "Disk quota exceeded."
161-
errorTab[10070] = "Stale file handle reference."
162-
errorTab[10071] = "Item is remote."
163-
errorTab[10091] = "Network subsystem is unavailable."
164-
errorTab[10092] = "Winsock.dll version out of range."
165-
errorTab[10093] = "Successful WSAStartup not yet performed."
166-
errorTab[10101] = "Graceful shutdown in progress."
167-
errorTab[10102] = "No more results from WSALookupServiceNext."
168-
errorTab[10103] = "Call has been canceled."
169-
errorTab[10104] = "Procedure call table is invalid."
170-
errorTab[10105] = "Service provider is invalid."
171-
errorTab[10106] = "Service provider failed to initialize."
172-
errorTab[10107] = "System call failure."
173-
errorTab[10108] = "Service not found."
174-
errorTab[10109] = "Class type not found."
175-
errorTab[10110] = "No more results from WSALookupServiceNext."
176-
errorTab[10111] = "Call was canceled."
177-
errorTab[10112] = "Database query was refused."
178-
errorTab[11001] = "Host not found."
179-
errorTab[11002] = "Nonauthoritative host not found."
180-
errorTab[11003] = "This is a nonrecoverable error."
181-
errorTab[11004] = "Valid name, no data record requested type."
182-
errorTab[11005] = "QoS receivers."
183-
errorTab[11006] = "QoS senders."
184-
errorTab[11007] = "No QoS senders."
185-
errorTab[11008] = "QoS no receivers."
186-
errorTab[11009] = "QoS request confirmed."
187-
errorTab[11010] = "QoS admission error."
188-
errorTab[11011] = "QoS policy failure."
189-
errorTab[11012] = "QoS bad style."
190-
errorTab[11013] = "QoS bad object."
191-
errorTab[11014] = "QoS traffic control error."
192-
errorTab[11015] = "QoS generic error."
193-
errorTab[11016] = "QoS service type error."
194-
errorTab[11017] = "QoS flowspec error."
195-
errorTab[11018] = "Invalid QoS provider buffer."
196-
errorTab[11019] = "Invalid QoS filter style."
197-
errorTab[11020] = "Invalid QoS filter style."
198-
errorTab[11021] = "Incorrect QoS filter count."
199-
errorTab[11022] = "Invalid QoS object length."
200-
errorTab[11023] = "Incorrect QoS flow count."
201-
errorTab[11024] = "Unrecognized QoS object."
202-
errorTab[11025] = "Invalid QoS policy object."
203-
errorTab[11026] = "Invalid QoS flow descriptor."
204-
errorTab[11027] = "Invalid QoS provider-specific flowspec."
205-
errorTab[11028] = "Invalid QoS provider-specific filterspec."
206-
errorTab[11029] = "Invalid QoS shape discard mode object."
207-
errorTab[11030] = "Invalid QoS shaping rate object."
208-
errorTab[11031] = "Reserved policy QoS element type."
115+
errorTab = {
116+
6: "Specified event object handle is invalid.",
117+
8: "Insufficient memory available.",
118+
87: "One or more parameters are invalid.",
119+
995: "Overlapped operation aborted.",
120+
996: "Overlapped I/O event object not in signaled state.",
121+
997: "Overlapped operation will complete later.",
122+
10004: "The operation was interrupted.",
123+
10009: "A bad file handle was passed.",
124+
10013: "Permission denied.",
125+
10014: "A fault occurred on the network??",
126+
10022: "An invalid operation was attempted.",
127+
10024: "Too many open files.",
128+
10035: "The socket operation would block.",
129+
10036: "A blocking operation is already in progress.",
130+
10037: "Operation already in progress.",
131+
10038: "Socket operation on nonsocket.",
132+
10039: "Destination address required.",
133+
10040: "Message too long.",
134+
10041: "Protocol wrong type for socket.",
135+
10042: "Bad protocol option.",
136+
10043: "Protocol not supported.",
137+
10044: "Socket type not supported.",
138+
10045: "Operation not supported.",
139+
10046: "Protocol family not supported.",
140+
10047: "Address family not supported by protocol family.",
141+
10048: "The network address is in use.",
142+
10049: "Cannot assign requested address.",
143+
10050: "Network is down.",
144+
10051: "Network is unreachable.",
145+
10052: "Network dropped connection on reset.",
146+
10053: "Software caused connection abort.",
147+
10054: "The connection has been reset.",
148+
10055: "No buffer space available.",
149+
10056: "Socket is already connected.",
150+
10057: "Socket is not connected.",
151+
10058: "The network has been shut down.",
152+
10059: "Too many references.",
153+
10060: "The operation timed out.",
154+
10061: "Connection refused.",
155+
10062: "Cannot translate name.",
156+
10063: "The name is too long.",
157+
10064: "The host is down.",
158+
10065: "The host is unreachable.",
159+
10066: "Directory not empty.",
160+
10067: "Too many processes.",
161+
10068: "User quota exceeded.",
162+
10069: "Disk quota exceeded.",
163+
10070: "Stale file handle reference.",
164+
10071: "Item is remote.",
165+
10091: "Network subsystem is unavailable.",
166+
10092: "Winsock.dll version out of range.",
167+
10093: "Successful WSAStartup not yet performed.",
168+
10101: "Graceful shutdown in progress.",
169+
10102: "No more results from WSALookupServiceNext.",
170+
10103: "Call has been canceled.",
171+
10104: "Procedure call table is invalid.",
172+
10105: "Service provider is invalid.",
173+
10106: "Service provider failed to initialize.",
174+
10107: "System call failure.",
175+
10108: "Service not found.",
176+
10109: "Class type not found.",
177+
10110: "No more results from WSALookupServiceNext.",
178+
10111: "Call was canceled.",
179+
10112: "Database query was refused.",
180+
11001: "Host not found.",
181+
11002: "Nonauthoritative host not found.",
182+
11003: "This is a nonrecoverable error.",
183+
11004: "Valid name, no data record requested type.",
184+
11005: "QoS receivers.",
185+
11006: "QoS senders.",
186+
11007: "No QoS senders.",
187+
11008: "QoS no receivers.",
188+
11009: "QoS request confirmed.",
189+
11010: "QoS admission error.",
190+
11011: "QoS policy failure.",
191+
11012: "QoS bad style.",
192+
11013: "QoS bad object.",
193+
11014: "QoS traffic control error.",
194+
11015: "QoS generic error.",
195+
11016: "QoS service type error.",
196+
11017: "QoS flowspec error.",
197+
11018: "Invalid QoS provider buffer.",
198+
11019: "Invalid QoS filter style.",
199+
11020: "Invalid QoS filter style.",
200+
11021: "Incorrect QoS filter count.",
201+
11022: "Invalid QoS object length.",
202+
11023: "Incorrect QoS flow count.",
203+
11024: "Unrecognized QoS object.",
204+
11025: "Invalid QoS policy object.",
205+
11026: "Invalid QoS flow descriptor.",
206+
11027: "Invalid QoS provider-specific flowspec.",
207+
11028: "Invalid QoS provider-specific filterspec.",
208+
11029: "Invalid QoS shape discard mode object.",
209+
11030: "Invalid QoS shaping rate object.",
210+
11031: "Reserved policy QoS element type."
211+
}
209212
__all__.append("errorTab")
210213

211214

@@ -348,6 +351,9 @@ def makefile(self, mode="r", buffering=None, *,
348351
if hasattr(os, 'sendfile'):
349352

350353
def _sendfile_use_sendfile(self, file, offset=0, count=None):
354+
# Lazy import to improve module import time
355+
import selectors
356+
351357
self._check_sendfile_params(file, offset, count)
352358
sockno = self.fileno()
353359
try:
@@ -549,27 +555,27 @@ def fromfd(fd, family, type, proto=0):
549555
return socket(family, type, proto, nfd)
550556

551557
if hasattr(_socket.socket, "sendmsg"):
552-
import array
553-
554558
def send_fds(sock, buffers, fds, flags=0, address=None):
555559
""" send_fds(sock, buffers, fds[, flags[, address]]) -> integer
556560
557561
Send the list of file descriptors fds over an AF_UNIX socket.
558562
"""
563+
import array
564+
559565
return sock.sendmsg(buffers, [(_socket.SOL_SOCKET,
560566
_socket.SCM_RIGHTS, array.array("i", fds))])
561567
__all__.append("send_fds")
562568

563569
if hasattr(_socket.socket, "recvmsg"):
564-
import array
565-
566570
def recv_fds(sock, bufsize, maxfds, flags=0):
567571
""" recv_fds(sock, bufsize, maxfds[, flags]) -> (data, list of file
568572
descriptors, msg_flags, address)
569573
570574
Receive up to maxfds file descriptors returning the message
571575
data and a list containing the descriptors.
572576
"""
577+
import array
578+
573579
# Array of ints
574580
fds = array.array("i")
575581
msg, ancdata, flags, addr = sock.recvmsg(bufsize,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve import time of :mod:`socket` by lazy importing modules and
2+
writing :data:`!socket.errorTab` as a constant.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy