uasyncio: Use DEBUG variable as imported from uasyncio.core.

Avoids heap allocation to render disabled debug logging strings.
pull/43/merge
Paul Sokolovsky 2016-12-29 11:37:41 +03:00
rodzic f29be360c0
commit 5403e0f710
1 zmienionych plików z 14 dodań i 14 usunięć

Wyświetl plik

@ -12,7 +12,7 @@ class EpollEventLoop(EventLoop):
self.objmap = {}
def add_reader(self, fd, cb, *args):
if __debug__:
if DEBUG and __debug__:
log.debug("add_reader%s", (fd, cb, args))
if args:
self.poller.register(fd, select.POLLIN)
@ -22,13 +22,13 @@ class EpollEventLoop(EventLoop):
self.objmap[fd] = cb
def remove_reader(self, fd):
if __debug__:
if DEBUG and __debug__:
log.debug("remove_reader(%s)", fd)
self.poller.unregister(fd)
del self.objmap[fd]
def add_writer(self, fd, cb, *args):
if __debug__:
if DEBUG and __debug__:
log.debug("add_writer%s", (fd, cb, args))
if args:
self.poller.register(fd, select.POLLOUT)
@ -38,7 +38,7 @@ class EpollEventLoop(EventLoop):
self.objmap[fd] = cb
def remove_writer(self, fd):
if __debug__:
if DEBUG and __debug__:
log.debug("remove_writer(%s)", fd)
try:
self.poller.unregister(fd)
@ -52,7 +52,7 @@ class EpollEventLoop(EventLoop):
raise
def wait(self, delay):
if __debug__:
if DEBUG and __debug__:
log.debug("epoll.wait(%d)", delay)
# We need one-shot behavior (second arg of 1 to .poll())
if delay == -1:
@ -65,7 +65,7 @@ class EpollEventLoop(EventLoop):
if res:
for fd, ev in res:
cb = self.objmap[fd]
if __debug__:
if DEBUG and __debug__:
log.debug("Calling IO callback: %r", cb)
if isinstance(cb, tuple):
cb[0](*cb[1])
@ -93,7 +93,7 @@ class StreamReader:
if __debug__:
log.debug("StreamReader.readline()")
yield IORead(self.s)
# if __debug__:
# if DEBUG and __debug__:
# log.debug("StreamReader.readline(): after IORead: %s", s)
while True:
res = self.s.readline()
@ -102,7 +102,7 @@ class StreamReader:
log.warn("Empty read")
if not res:
yield IOReadDone(self.s)
if __debug__:
if DEBUG and __debug__:
log.debug("StreamReader.readline(): res: %s", res)
return res
@ -127,18 +127,18 @@ class StreamWriter:
# to return immediately (which means it has to buffer all the
# data), this method is a coroutine.
sz = len(buf)
if __debug__:
if DEBUG and __debug__:
log.debug("StreamWriter.awrite(): spooling %d bytes", sz)
while True:
res = self.s.write(buf)
# If we spooled everything, return immediately
if res == sz:
if __debug__:
if DEBUG and __debug__:
log.debug("StreamWriter.awrite(): completed spooling %d bytes", res)
return
if res is None:
res = 0
if __debug__:
if DEBUG and __debug__:
log.debug("StreamWriter.awrite(): spooled partial %d bytes", res)
assert res < sz
buf = buf[res:]
@ -160,7 +160,7 @@ class StreamWriter:
def open_connection(host, port):
if __debug__:
if DEBUG and __debug__:
log.debug("open_connection(%s, %s)", host, port)
s = _socket.socket()
s.setblocking(False)
@ -176,7 +176,7 @@ def open_connection(host, port):
yield IOWrite(s)
# if __debug__:
# assert s2.fileno() == s.fileno()
if __debug__:
if DEBUG and __debug__:
log.debug("open_connection: After iowait: %s", s)
return StreamReader(s), StreamWriter(s, {})
@ -199,7 +199,7 @@ def start_server(client_coro, host, port, backlog=10):
log.debug("start_server: After iowait")
s2, client_addr = s.accept()
s2.setblocking(False)
if __debug__:
if DEBUG and __debug__:
log.debug("start_server: After accept: %s", s2)
extra = {"peername": client_addr}
yield client_coro(StreamReader(s2), StreamWriter(s2, extra))