Skip to content

Commit 921370b

Browse files
committed
merge both winloop & uvloop's error handling ways together
1 parent 3d1fc4d commit 921370b

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

uvloop/_testbase.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ def loop_exception_handler(self, loop, context):
8989
self.loop.default_exception_handler(context)
9090

9191
def setUp(self):
92-
self.loop = self.new_loop()
92+
# WINLOOP comment: next two lines are swapped because otherwise
93+
# setting event loop policy has no effect.
9394
asyncio.set_event_loop_policy(self.new_policy())
95+
self.loop = self.new_loop()
9496
asyncio.set_event_loop(self.loop)
9597
self._check_unclosed_resources_in_debug = True
9698

uvloop/errors.pyx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import errno # import here for window's skae...
2+
13
cdef str __strerr(int errno):
24
return strerror(errno).decode()
35

@@ -8,19 +10,20 @@ cdef __convert_python_error(int uverr):
810
# Implementation detail: on Unix error codes are the
911
# negated errno (or -errno), while on Windows they
1012
# are defined by libuv to arbitrary negative numbers.
11-
13+
1214
cdef int oserr
15+
cdef object err
16+
1317
if system.PLATFORM_IS_WINDOWS:
14-
15-
# Winloop comment: The following approach seems to work for Windows:
16-
# translation from uverr, which is a negative number like -4088 or -4071
17-
# defined by libuv (as mentioned above), to error numbers obtained via
18-
# the Python module errno.
18+
19+
# So Let's try converting them a different way if were using windows.
20+
# Winloop has a smarter technique for showing these errors.
1921
err = getattr(errno, uv.uv_err_name(uverr).decode(), uverr)
2022
return OSError(err, uv.uv_strerror(uverr).decode())
21-
23+
2224
oserr = -uverr
2325

26+
2427
exc = OSError
2528

2629
if uverr in (uv.UV_EACCES, uv.UV_EPERM):
@@ -118,7 +121,20 @@ cdef convert_error(int uverr):
118121

119122
sock_err = __convert_socket_error(uverr)
120123
if sock_err:
121-
msg = system.gai_strerror(sock_err).decode('utf-8')
122-
return socket_gaierror(sock_err, msg)
124+
# Winloop comment: Sometimes libraries will throw in some
125+
# unwanted unicode BS to unravel, to prevent the possibility of this being a threat,
126+
# surrogateescape is utilized
127+
# SEE: https://github.com/Vizonex/Winloop/issues/32
128+
msg = system.gai_strerror(sock_err).decode('utf-8', "surrogateescape")
129+
# Winloop comment: on Windows, cPython has a simpler error
130+
# message than uvlib (via winsock probably) in these two cases:
131+
# EAI_FAMILY [ErrNo 10047] "An address incompatible with the requested protocol was used. "
132+
# EAI_NONAME [ErrNo 10001] "No such host is known. "
133+
# We replace these messages with "getaddrinfo failed"
134+
if sys.platform == 'win32':
135+
if sock_err in (socket_EAI_FAMILY, socket_EAI_NONAME):
136+
msg = 'getaddrinfo failed'
137+
return socket_gaierror(sock_err, msg)
123138

124139
return __convert_python_error(uverr)
140+

0 commit comments

Comments
 (0)