Skip to content

Commit

Permalink
replace callback context with excepthook
Browse files Browse the repository at this point in the history
  • Loading branch information
koehlma committed Dec 29, 2015
1 parent a5f6ddd commit 4daedfe
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 54 deletions.
17 changes: 12 additions & 5 deletions uv/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,15 @@ def unpack_sockaddr(c_sockaddr):
@ffi.callback('uv_getaddrinfo_cb')
def uv_getaddrinfo_cb(uv_getaddrinfo, status, _):
addrinfo_request = library.detach(uv_getaddrinfo)
addrinfo_request.destroy()
if status == 0: addrinfo_request.populate()
with addrinfo_request.loop.callback_context:
""" :type: uv.dns.GetAddrInfo """
if status == error.StatusCode.SUCCESS:
addrinfo_request.populate()
try:
addrinfo_request.callback(addrinfo_request, error.get_status_code(status),
addrinfo_request.addrinfo)
except:
addrinfo_request.loop.handle_exception()
addrinfo_request.destroy()


@request.RequestType.GETADDRINFO
Expand Down Expand Up @@ -246,10 +250,13 @@ def getaddrinfo(host, port, family=0, socktype=0, protocol=0,
@ffi.callback('uv_getnameinfo_cb')
def uv_getnameinfo_cb(uv_getnameinfo, status, c_hostname, c_service):
nameinfo_request = library.detach(uv_getnameinfo)
nameinfo_request.destroy()
with nameinfo_request.loop.callback_context:
""" :type: uv.dns.GetNameInfo """
try:
nameinfo_request.callback(nameinfo_request, status, library.str_c2py(c_hostname),
library.str_c2py(c_service))
except:
nameinfo_request.loop.handle_exception()
nameinfo_request.destroy()


@request.RequestType.GETNAMEINFO
Expand Down
11 changes: 7 additions & 4 deletions uv/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,14 @@ def post_stat(request):

@ffi.callback('uv_fs_cb')
def fs_callback(uv_request):
request = library.detach(uv_request)
request.destroy()
fs_request = library.detach(uv_request)
""" :type: uv.FSRequest """
try:
fs_request.callback(fs_request, *fs_request.fs_type.postprocessor(fs_request))
except:
fs_request.loop.handle_exception()
lib.uv_fs_req_cleanup(uv_request)
with request.loop.callback_context:
request.callback(request, *request.fs_type.postprocessor(request))
fs_request.destroy()


def close(fd, callback=None, loop=None):
Expand Down
6 changes: 5 additions & 1 deletion uv/handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ def __call__(self, cls):
@ffi.callback('uv_close_cb')
def uv_close_cb(uv_handle):
handle = library.detach(uv_handle)
with handle.loop.callback_context: handle.on_closed(handle)
""" :type: uv.Handle """
try:
handle.on_closed(handle)
except:
handle.loop.handle_exception()
handle.destroy()


Expand Down
5 changes: 4 additions & 1 deletion uv/handles/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
@ffi.callback('uv_async_cb')
def uv_async_cb(uv_async):
async = library.detach(uv_async)
with async.loop.callback_context:
""" :type: uv.Async """
try:
async.on_wakeup(async)
except:
async.loop.handle_exception()


@handle.HandleType.ASYNC
Expand Down
5 changes: 4 additions & 1 deletion uv/handles/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
@ffi.callback('uv_check_cb')
def uv_check_cb(uv_check):
check = library.detach(uv_check)
with check.loop.callback_context:
""" :type: uv.Check """
try:
check.on_check(check)
except:
check.loop.handle_exception()


@handle.HandleType.CHECK
Expand Down
11 changes: 7 additions & 4 deletions uv/handles/fs_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,13 @@ class FSEvents(common.Enumeration):

@ffi.callback('uv_fs_event_cb')
def uv_fs_event_cb(uv_fs_event, c_filename, events, status):
fs_monitor = library.detach(uv_fs_event)
with fs_monitor.loop.callback_context:
filename = library.str_c2py(c_filename)
fs_monitor.on_event(fs_monitor, status, filename, events)
fs_event = library.detach(uv_fs_event)
""" :type: uv.FSEvent """
filename = library.str_c2py(c_filename)
try:
fs_event.on_event(fs_event, status, filename, events)
except:
fs_event.loop.handle_exception()


@handle.HandleType.FS_EVENT
Expand Down
5 changes: 4 additions & 1 deletion uv/handles/fs_poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ def uv_fs_poll_cb(uv_fs_poll, status, uv_previous_stat, uv_current_stat):
previous_stat = fs.unpack_stat(uv_previous_stat) if uv_previous_stat else None
current_stat = fs.unpack_stat(uv_current_stat) if uv_current_stat else None
fs_poll = library.detach(uv_fs_poll)
with fs_poll.loop.callback_context:
""" :type: uv.FSPoll """
try:
fs_poll.callback(fs_poll, status, previous_stat, current_stat)
except:
fs_poll.loop.handle_exception()


@handle.HandleType.FS_POLL
Expand Down
4 changes: 3 additions & 1 deletion uv/handles/idle.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
@ffi.callback('uv_idle_cb')
def uv_idle_cb(uv_idle):
idle = library.detach(uv_idle)
with idle.loop.callback_context:
try:
idle.on_idle(idle)
except:
idle.loop.handle_exception()


@handle.HandleType.IDLE
Expand Down
4 changes: 3 additions & 1 deletion uv/handles/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ class PollEvent(common.Enumeration):
@ffi.callback('uv_poll_cb')
def poll_callback(uv_poll, status, events):
poll = library.detach(uv_poll)
with poll.loop.callback_context:
try:
poll.on_event(poll, status, events)
except:
poll.loop.handle_exception()


@handle.HandleType.POLL
Expand Down
4 changes: 3 additions & 1 deletion uv/handles/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
@ffi.callback('uv_prepare_cb')
def uv_prepare_cb(uv_prepare):
prepare = library.detach(uv_prepare)
with prepare.loop.callback_context:
try:
prepare.on_prepare(prepare)
except:
prepare.loop.handle_exception()


@handle.HandleType.PREPARE
Expand Down
5 changes: 4 additions & 1 deletion uv/handles/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ class ProcessFlags(common.Enumeration):
@ffi.callback('uv_exit_cb')
def uv_exit_cb(uv_process, exit_status, term_signum):
process = library.detach(uv_process)
with process.loop.callback_context:
""" :type: uv.Process """
try:
process.on_exit(process, exit_status, term_signum)
except:
process.loop.handle_exception()


def populate_stdio_container(uv_stdio, file_base=None):
Expand Down
5 changes: 4 additions & 1 deletion uv/handles/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ class Signals(common.Enumeration):
@ffi.callback('uv_signal_cb')
def uv_signal_cb(uv_signal, signum):
signal = library.detach(uv_signal)
with signal.loop.callback_context:
""" :type: uv.Signal """
try:
signal.on_signal(sig, signum)
except:
signal.loop.handle_exception()


@handle.HandleType.SIGNAL
Expand Down
33 changes: 24 additions & 9 deletions uv/handles/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
@ffi.callback('uv_shutdown_cb')
def uv_shutdown_cb(uv_request, status):
shutdown_request = library.detach(uv_request)
shutdown_request.destroy()
with shutdown_request.loop.callback_context:
""" :type: uv.ShutdownRequest """
try:
shutdown_request.on_shutdown(shutdown_request, status)
except:
shutdown_request.loop.handle_exception()
shutdown_request.destroy()


@request.RequestType.SHUTDOWN
Expand Down Expand Up @@ -77,9 +80,12 @@ def __init__(self, stream, on_shutdown=None):
@ffi.callback('uv_write_cb')
def uv_write_cb(uv_request, status):
write_request = library.detach(uv_request)
write_request.destroy()
with write_request.loop.callback_context:
""" :type: uv.WriteRequest """
try:
write_request.on_write(write_request, status)
except:
write_request.loop.handle_exception()
write_request.destroy()


@request.RequestType.WRITE
Expand Down Expand Up @@ -149,9 +155,12 @@ def __init__(self, stream, buffers, send_stream=None, on_write=None):
@ffi.callback('uv_connect_cb')
def uv_connect_cb(uv_request, status):
connect_request = library.detach(uv_request)
connect_request.destroy()
with connect_request.loop.callback_context:
""" :type: uv.ConnectRequest """
try:
connect_request.on_connect(connect_request, status)
except:
connect_request.loop.handle_exception()
connect_request.destroy()


@request.RequestType.CONNECT
Expand Down Expand Up @@ -199,17 +208,23 @@ def __init__(self, stream, on_connect=None):
@ffi.callback('uv_connection_cb')
def uv_connection_cb(uv_stream, status):
stream = library.detach(uv_stream)
with stream.loop.callback_context:
""" :type: uv.Stream """
try:
stream.on_connection(stream, status)
except:
stream.loop.handle_exception()


@ffi.callback('uv_read_cb')
def uv_read_cb(uv_stream, length, uv_buf):
stream = library.detach(uv_stream)
""" :type: uv.Stream """
data = stream.loop.allocator.finalize(uv_stream, length, uv_buf)
length, status = (0, length) if length < 0 else (length, error.StatusCode.SUCCESS)
with stream.loop.callback_context:
try:
stream.on_read(stream, status, length, data)
except:
stream.loop.handle_exception()


@handle.HandleType.STREAM
Expand Down Expand Up @@ -255,7 +270,7 @@ def __init__(self, uv_stream, ipc=False, loop=None):
self.ipc = ipc
"""
Stream supports inter process communication or not.
0
:readonly: True
:type: bool
"""
Expand Down
5 changes: 4 additions & 1 deletion uv/handles/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
@ffi.callback('uv_timer_cb')
def uv_timer_cb(uv_timer):
timer = library.detach(uv_timer)
with timer.loop.callback_context:
""" :type: uv.Timer """
try:
timer.on_timeout(timer)
except:
timer.loop.handle_exception()


@handle.HandleType.TIMER
Expand Down
2 changes: 1 addition & 1 deletion uv/handles/tty.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from __future__ import absolute_import, division, print_function, unicode_literals

from .. import common, error, handle, library
from .. import common, error, handle
from ..library import ffi, lib

from . import stream
Expand Down
12 changes: 9 additions & 3 deletions uv/handles/udp.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ class UDPMembership(common.Enumeration):
@ffi.callback('uv_udp_send_cb')
def uv_udp_send_cb(uv_request, status):
send_request = library.detach(uv_request)
send_request.destroy()
with send_request.loop.callback_context:
""" :type: uv.SendRequest """
try:
send_request.on_send(send_request, status)
except:
send_request.loop.handle_exception()
send_request.destroy()


@request.RequestType.SEND
Expand Down Expand Up @@ -127,11 +130,14 @@ def __init__(self, udp, buffers, address, on_send=None):
@ffi.callback('uv_udp_recv_cb')
def uv_udp_recv_cb(uv_udp, length, uv_buf, c_sockaddr, flags):
udp = library.detach(uv_udp)
""" :type: uv.UDP """
data = udp.loop.allocator.finalize(uv_udp, length, uv_buf)
length, status = (0, length) if length < 0 else (length, error.StatusCode.SUCCESS)
address = dns.unpack_sockaddr(c_sockaddr)
with udp.loop.callback_context:
try:
udp.on_receive(udp, status, address, length, data, flags)
except:
udp.loop.handle_exception()


@handle.HandleType.UDP
Expand Down

0 comments on commit 4daedfe

Please sign in to comment.