From 64334e6c9512de1c012b06f69bd4f00fee4b6595 Mon Sep 17 00:00:00 2001 From: Berry Schoenmakers Date: Sun, 14 Jan 2024 16:19:46 +0100 Subject: [PATCH] Use UV_E* constants, OSError built-in translation. --- winloop/errors.pyx | 61 ++++++------------------------------ winloop/handles/stream.pyx | 6 ++-- winloop/handles/udp.pyx | 4 +-- winloop/includes/_stdlib.pxi | 4 +-- winloop/includes/uv.pxd | 37 ++++++++++++---------- 5 files changed, 37 insertions(+), 75 deletions(-) diff --git a/winloop/errors.pyx b/winloop/errors.pyx index 9add7e5..8142d7a 100644 --- a/winloop/errors.pyx +++ b/winloop/errors.pyx @@ -1,69 +1,28 @@ # cython:language_level = 3 -from libc.string cimport strerror +from libc.string cimport strerror # from includes._stdlib cimport * from .includes cimport uv , system -cdef str __strerr(int errno): - return strerror(errno).decode() cdef __convert_python_error(int uverr): - # XXX Won't work for Windows: + # XXX Won't work for Windows: # From libuv docs: # Implementation detail: on Unix error codes are the # negated errno (or -errno), while on Windows they # are defined by libuv to arbitrary negative numbers. # NOTE (Vizonex): I DEBUNKED IT, IT CAN! As long as we can find it's actual Diagnosis... - # by substituting the 'UV_E' macros to Just 'E' we can then find the errors we need and - # then diagnose them... + # by substituting the 'UV_E' macros to Just 'E' we can then find the errors we need and + # then diagnose them... # TODO VERIFY THAT THE ERRORS ARE CORRECTLY MAPPED!!! - cdef int oserr = -uverr - - exc = OSError - - if uverr in (uv.EACCES, uv.EPERM): - exc = PermissionError - - # TODO (Vizonex) Translate more error types... - elif uverr == uv.EOF: - exc = EOFError - - elif uverr in (uv.EAGAIN, uv.EALREADY): - exc = BlockingIOError - - elif uverr in (uv.EPIPE, uv.UV__ESHUTDOWN): - exc = BrokenPipeError - - elif uverr == uv.ECONNABORTED: - exc = ConnectionAbortedError - - elif uverr == uv.ECONNREFUSED: - exc = ConnectionRefusedError - - elif uverr == uv.ECONNRESET: - exc = ConnectionResetError - - elif uverr == uv.EEXIST: - exc = FileExistsError - - elif uverr in (uv.ENOENT, uv.UV__ENOENT): - exc = FileNotFoundError - - elif uverr == uv.EINTR: - exc = InterruptedError - - elif uverr == uv.EISDIR: - exc = IsADirectoryError - - elif uverr == uv.ESRCH: - exc = ProcessLookupError - - elif uverr == uv.ETIMEDOUT: - exc = TimeoutError - - return exc(oserr, __strerr(oserr)) + # ... + # The following approach seems to work for Windows: translation from uverr, + # which is a negative number like -4088 or -4071 defined by libuv (as mentioned above), + # to error numbers obtained via the Python module errno. + err = getattr(errno, uv.uv_err_name(uverr).decode(), uverr) + return OSError(err, uv.uv_strerror(uverr).decode()) cdef int __convert_socket_error(int uverr) noexcept: diff --git a/winloop/handles/stream.pyx b/winloop/handles/stream.pyx index 89d2044..7c86c60 100644 --- a/winloop/handles/stream.pyx +++ b/winloop/handles/stream.pyx @@ -570,7 +570,7 @@ cdef class UVStream(UVBaseTransport): self._buffer_size = 0 return - elif err != -4088: # for now, use -4088 directly, instead of uv.EAGAIN (which is equal to 11) + elif err != uv.UV_EAGAIN: ctx.close() # print("Something failed in line 519 uv.uv_try_write") exc = convert_error(err) @@ -786,7 +786,7 @@ cdef inline bint __uv_stream_on_read_common(UVStream sc, Loop loop, sc.__reading_stopped() # Just in case. return True - if nread == -4095: # for now, use -4095 directly, instead of uv.EOF (which is equal to -1) + if nread == uv.UV_EOF: # From libuv docs: # The callee is responsible for stopping closing the stream # when an error happens by calling uv_read_stop() or uv_close(). @@ -984,7 +984,7 @@ cdef void __uv_stream_buffered_on_read(uv.uv_stream_t* stream, Loop loop = sc._loop Py_buffer* pybuf = &sc._read_pybuf - if nread == uv.ENOBUFS: + if nread == uv.UV_ENOBUFS: sc._fatal_error( RuntimeError( 'unhandled error (or an empty buffer) in get_buffer()'), diff --git a/winloop/handles/udp.pyx b/winloop/handles/udp.pyx index 0d487c9..6181da1 100644 --- a/winloop/handles/udp.pyx +++ b/winloop/handles/udp.pyx @@ -229,9 +229,9 @@ cdef class UDPTransport(UVBaseTransport): saddr) PyBuffer_Release(&try_pybuf) else: - err = uv.EAGAIN + err = uv.UV_EAGAIN - if err == uv.EAGAIN: + if err == uv.UV_EAGAIN: ctx = _UDPSendContext.new(self, data) err = uv.uv_udp_send(&ctx.req, self._handle, diff --git a/winloop/includes/_stdlib.pxi b/winloop/includes/_stdlib.pxi index 881fa32..c5e1613 100644 --- a/winloop/includes/_stdlib.pxi +++ b/winloop/includes/_stdlib.pxi @@ -175,8 +175,8 @@ cdef py_inf = float('inf') # Cython doesn't clean-up imported objects properly in Py3 mode, -# so we delete refs to all modules manually (except sys) -del asyncio, concurrent, collections, errno +# so we delete refs to all modules manually (except sys and errno) +del asyncio, concurrent, collections del functools, inspect, itertools, socket, os, threading del signal, subprocess, ssl del time, traceback, warnings, weakref diff --git a/winloop/includes/uv.pxd b/winloop/includes/uv.pxd index 757c580..e5d4309 100644 --- a/winloop/includes/uv.pxd +++ b/winloop/includes/uv.pxd @@ -10,35 +10,38 @@ cdef extern from "vendor/include/uv.h" nogil: cdef int UV_TCP_IPV6ONLY # NOTE: This is from errno.h as well... + + # ... maybe possible to change these back all to UV_EACCES etcetera like in uvloop cdef int EACCES - cdef int EAGAIN cdef int UV_EAGAIN cdef int EALREADY cdef int EBUSY - cdef int ECONNABORTED - cdef int ECONNREFUSED + cdef int UV_ECONNABORTED + cdef int UV_ECONNREFUSED cdef int ECONNRESET + cdef int UV_ECONNRESET cdef int ECANCELED - cdef int EEXIST - cdef int EINTR + cdef int UV_EEXIST + cdef int UV_EINTR cdef int EINVAL - cdef int EISDIR + cdef int UV_EISDIR cdef int UV__ENOENT cdef int ENOENT cdef int EOF + cdef int UV_EOF cdef int EPERM cdef int EPIPE # cdef int ESHUTDOWN # There's only a few of these that don't cut this execption... cdef int UV__ESHUTDOWN - cdef int ESRCH + cdef int UV_ESRCH cdef int ETIMEDOUT cdef int EBADF - cdef int ENOBUFS + cdef int UV_ENOBUFS cdef int EWOULDBLOCK - # socket-erros + # socket-errors # cdef int EAI_ADDRFAMILY cdef int UV__EAI_ADDRFAMILY cdef int EAI_AGAIN @@ -57,7 +60,7 @@ cdef extern from "vendor/include/uv.h" nogil: cdef int SOL_SOCKET cdef int SO_ERROR - + # TODO Vizonex Get SO_RESUSEADDR FROM ANOTHER HEADER FILE IF NEEDED! # cdef int SO_REUSEADDR # cdef int SO_REUSEPORT We already have this avalibe inside of "_stdlib.pxi" so no need to have it here... @@ -152,7 +155,7 @@ cdef extern from "vendor/include/uv.h" nogil: size_t write_queue_size uv_loop_t* loop - # NOTE Introduced flags and type into uv_stream so that + # NOTE Introduced flags and type into uv_stream so that # we can implement a faster / more direct version of uv_try_write unsigned int flags uv_handle_type type @@ -255,7 +258,7 @@ cdef extern from "vendor/include/uv.h" nogil: UV_LEAVE_GROUP = 0, UV_JOIN_GROUP - + const char* uv_strerror(int err) const char* uv_err_name(int err) @@ -510,7 +513,7 @@ cdef extern from "vendor/include/uv.h" nogil: UV_INHERIT_STREAM = 0x04, UV_READABLE_PIPE = 0x10, UV_WRITABLE_PIPE = 0x20, - # NOTE Added in UV_NONBLOCK_PIPE for stability reasons... - Vizonex + # NOTE Added in UV_NONBLOCK_PIPE for stability reasons... - Vizonex UV_NONBLOCK_PIPE = 0x40 @@ -524,7 +527,7 @@ cdef extern from "vendor/include/uv.h" nogil: ctypedef unsigned char uv_uid_t - ctypedef unsigned char uv_gid_t + ctypedef unsigned char uv_gid_t ctypedef struct uv_process_options_t: uv_exit_cb exit_cb @@ -554,7 +557,7 @@ cdef extern from "vendor/include/uv.h" nogil: int uv_socketpair(int type, int protocol, uv_os_sock_t socket_vector[2], int flags0, int flags1) - + uv_handle_type uv_guess_handle(uv_file) cdef enum uv_fs_event: @@ -567,8 +570,8 @@ cdef extern from "winsock2.h": cdef int SO_BROADCAST -# Since we already have system imported into here and To Prevent Looped imports -# I'll just put try_tcp_write right here - Vizonex +# Since we already have system imported into here and To Prevent Looped imports +# I'll just put try_tcp_write right here - Vizonex cdef extern from "includes/tcp.h": # incase anyone is like "try_tcp_write doesn't exitst in libuv!" I'll put another note into stream.pyx about this function int try_tcp_write(uv_tcp_t* handle, system.WSABUF bufs)