Skip to content

Commit

Permalink
Merge pull request #15 from lschoe/main
Browse files Browse the repository at this point in the history
Use UV_E* constants, OSError built-in translation.
  • Loading branch information
Vizonex authored Jan 17, 2024
2 parents d61f416 + fdcd851 commit 4341812
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 75 deletions.
61 changes: 10 additions & 51 deletions winloop/errors.pyx
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
6 changes: 3 additions & 3 deletions winloop/handles/stream.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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().
Expand Down Expand Up @@ -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()'),
Expand Down
4 changes: 2 additions & 2 deletions winloop/handles/udp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
<uv.uv_udp_t*>self._handle,
Expand Down
4 changes: 2 additions & 2 deletions winloop/includes/_stdlib.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 20 additions & 17 deletions winloop/includes/uv.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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


Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)

0 comments on commit 4341812

Please sign in to comment.