Commit ccfb0fe8 authored by Denis Bilenko's avatar Denis Bilenko

gevent.socket: rename internal helpers wait_reader/wait_writer to wait_read/wait_write

parent 8d3bc8d6
...@@ -54,20 +54,20 @@ def _wait_helper(ev, evtype): ...@@ -54,20 +54,20 @@ def _wait_helper(ev, evtype):
current.switch(ev) current.switch(ev)
def wait_reader(fileno, timeout=-1, timeout_exc=_socket.timeout): def wait_read(fileno, timeout=-1, timeout_exc=_socket.timeout):
evt = core.read_event(fileno, _wait_helper, timeout, (getcurrent(), timeout_exc)) evt = core.read_event(fileno, _wait_helper, timeout, (getcurrent(), timeout_exc))
try: try:
switch_result = get_hub().switch() switch_result = get_hub().switch()
assert evt is switch_result, 'Invalid switch into wait_reader(): %r' % (switch_result, ) assert evt is switch_result, 'Invalid switch into wait_read(): %r' % (switch_result, )
finally: finally:
evt.cancel() evt.cancel()
def wait_writer(fileno, timeout=-1, timeout_exc=_socket.timeout): def wait_write(fileno, timeout=-1, timeout_exc=_socket.timeout):
evt = core.write_event(fileno, _wait_helper, timeout, (getcurrent(), timeout_exc)) evt = core.write_event(fileno, _wait_helper, timeout, (getcurrent(), timeout_exc))
try: try:
switch_result = get_hub().switch() switch_result = get_hub().switch()
assert evt is switch_result, 'Invalid switch into wait_writer(): %r' % (switch_result, ) assert evt is switch_result, 'Invalid switch into wait_write(): %r' % (switch_result, )
finally: finally:
evt.cancel() evt.cancel()
...@@ -204,7 +204,7 @@ class GreenSocket(object): ...@@ -204,7 +204,7 @@ class GreenSocket(object):
if res is not None: if res is not None:
client, addr = res client, addr = res
return type(self)(client), addr return type(self)(client), addr
wait_reader(fd.fileno(), timeout=self.timeout, timeout_exc=timeout) wait_read(fd.fileno(), timeout=self.timeout, timeout_exc=timeout)
def close(self): def close(self):
self.fd = _closedsocket() self.fd = _closedsocket()
...@@ -220,7 +220,7 @@ class GreenSocket(object): ...@@ -220,7 +220,7 @@ class GreenSocket(object):
fd = self.fd fd = self.fd
if self.timeout is None: if self.timeout is None:
while not socket_connect(fd, address): while not socket_connect(fd, address):
wait_writer(fd.fileno(), timeout_exc=timeout) wait_write(fd.fileno(), timeout_exc=timeout)
else: else:
end = time.time() + self.timeout end = time.time() + self.timeout
while True: while True:
...@@ -228,7 +228,7 @@ class GreenSocket(object): ...@@ -228,7 +228,7 @@ class GreenSocket(object):
return return
if time.time() >= end: if time.time() >= end:
raise timeout raise timeout
wait_writer(fd.fileno(), timeout=end-time.time(), timeout_exc=timeout) wait_write(fd.fileno(), timeout=end-time.time(), timeout_exc=timeout)
def connect_ex(self, address): def connect_ex(self, address):
if self.timeout==0.0: if self.timeout==0.0:
...@@ -237,7 +237,7 @@ class GreenSocket(object): ...@@ -237,7 +237,7 @@ class GreenSocket(object):
if self.timeout is None: if self.timeout is None:
while not socket_connect(fd, address): while not socket_connect(fd, address):
try: try:
wait_writer(fd.fileno(), timeout_exc=timeout) wait_write(fd.fileno(), timeout_exc=timeout)
except error, ex: except error, ex:
return ex[0] return ex[0]
else: else:
...@@ -248,7 +248,7 @@ class GreenSocket(object): ...@@ -248,7 +248,7 @@ class GreenSocket(object):
if time.time() >= end: if time.time() >= end:
raise timeout raise timeout
try: try:
wait_writer(fd.fileno(), timeout=end-time.time(), timeout_exc=timeout) wait_write(fd.fileno(), timeout=end-time.time(), timeout_exc=timeout)
except error, ex: except error, ex:
return ex[0] return ex[0]
...@@ -263,30 +263,30 @@ class GreenSocket(object): ...@@ -263,30 +263,30 @@ class GreenSocket(object):
def recv(self, *args): def recv(self, *args):
if self.timeout!=0.0: if self.timeout!=0.0:
wait_reader(self.fileno(), timeout=self.timeout, timeout_exc=timeout) wait_read(self.fileno(), timeout=self.timeout, timeout_exc=timeout)
res = self.fd.recv(*args) res = self.fd.recv(*args)
return res return res
def recvfrom(self, *args): def recvfrom(self, *args):
if self.timeout!=0.0: if self.timeout!=0.0:
wait_reader(self.fileno(), timeout=self.timeout, timeout_exc=timeout) wait_read(self.fileno(), timeout=self.timeout, timeout_exc=timeout)
return self.fd.recvfrom(*args) return self.fd.recvfrom(*args)
def recvfrom_into(self, *args): def recvfrom_into(self, *args):
if self.timeout!=0.0: if self.timeout!=0.0:
wait_reader(self.fileno(), timeout=self.timeout, timeout_exc=timeout) wait_read(self.fileno(), timeout=self.timeout, timeout_exc=timeout)
return self.fd.recvfrom_into(*args) return self.fd.recvfrom_into(*args)
def recv_into(self, *args): def recv_into(self, *args):
if self.timeout!=0.0: if self.timeout!=0.0:
wait_reader(self.fileno(), timeout=self.timeout, timeout_exc=timeout) wait_read(self.fileno(), timeout=self.timeout, timeout_exc=timeout)
return self.fd.recv_into(*args) return self.fd.recv_into(*args)
def send(self, data, timeout=timeout_default): def send(self, data, timeout=timeout_default):
if timeout is timeout_default: if timeout is timeout_default:
timeout = self.timeout timeout = self.timeout
if timeout!=0.0: if timeout!=0.0:
wait_writer(self.fileno(), timeout=timeout, timeout_exc=_socket.timeout) wait_write(self.fileno(), timeout=timeout, timeout_exc=_socket.timeout)
return self.fd.send(data) return self.fd.send(data)
def sendall(self, data): def sendall(self, data):
...@@ -308,7 +308,7 @@ class GreenSocket(object): ...@@ -308,7 +308,7 @@ class GreenSocket(object):
def sendto(self, *args): def sendto(self, *args):
if self.timeout!=0.0: if self.timeout!=0.0:
wait_writer(self.fileno(), timeout=self.timeout, timeout_exc=timeout) wait_write(self.fileno(), timeout=self.timeout, timeout_exc=timeout)
return self.fd.sendto(*args) return self.fd.sendto(*args)
def setblocking(self, flag): def setblocking(self, flag):
...@@ -372,7 +372,7 @@ class GreenSSL(GreenSocket): ...@@ -372,7 +372,7 @@ class GreenSSL(GreenSocket):
accepted = type(self)(client, server_side=True) accepted = type(self)(client, server_side=True)
accepted.do_handshake() accepted.do_handshake()
return accepted, addr return accepted, addr
wait_reader(fd.fileno(), timeout=self.timeout, timeout_exc=timeout) wait_read(fd.fileno(), timeout=self.timeout, timeout_exc=timeout)
def do_handshake(self): def do_handshake(self):
while True: while True:
...@@ -380,9 +380,9 @@ class GreenSSL(GreenSocket): ...@@ -380,9 +380,9 @@ class GreenSSL(GreenSocket):
self.fd.do_handshake() self.fd.do_handshake()
break break
except SSL.WantReadError: except SSL.WantReadError:
wait_reader(self.fileno()) wait_read(self.fileno())
except SSL.WantWriteError: except SSL.WantWriteError:
wait_writer(self.fileno()) wait_write(self.fileno())
except SSL.SysCallError, ex: except SSL.SysCallError, ex:
raise sslerror(SysCallError_code_mapping.get(ex.args[0], ex.args[0]), ex.args[1]) raise sslerror(SysCallError_code_mapping.get(ex.args[0], ex.args[0]), ex.args[1])
except SSL.Error, ex: except SSL.Error, ex:
...@@ -402,12 +402,12 @@ class GreenSSL(GreenSocket): ...@@ -402,12 +402,12 @@ class GreenSSL(GreenSocket):
if self.timeout==0.0: if self.timeout==0.0:
raise timeout(str(ex)) raise timeout(str(ex))
else: else:
wait_writer(self.fileno(), timeout=timeout, timeout_exc=_socket.timeout) wait_write(self.fileno(), timeout=timeout, timeout_exc=_socket.timeout)
except SSL.WantReadError, ex: except SSL.WantReadError, ex:
if self.timeout==0.0: if self.timeout==0.0:
raise timeout(str(ex)) raise timeout(str(ex))
else: else:
wait_reader(self.fileno(), timeout=timeout, timeout_exc=_socket.timeout) wait_read(self.fileno(), timeout=timeout, timeout_exc=_socket.timeout)
except SSL.SysCallError, e: except SSL.SysCallError, e:
if e[0] == -1 and data == "": if e[0] == -1 and data == "":
# errors when writing empty strings are expected and can be ignored # errors when writing empty strings are expected and can be ignored
...@@ -427,12 +427,12 @@ class GreenSSL(GreenSocket): ...@@ -427,12 +427,12 @@ class GreenSSL(GreenSocket):
if self.timeout==0.0: if self.timeout==0.0:
raise timeout(str(ex)) raise timeout(str(ex))
else: else:
wait_reader(self.fileno(), timeout=self.timeout, timeout_exc=timeout) wait_read(self.fileno(), timeout=self.timeout, timeout_exc=timeout)
except SSL.WantWriteError, ex: except SSL.WantWriteError, ex:
if self.timeout==0.0: if self.timeout==0.0:
raise timeout(str(ex)) raise timeout(str(ex))
else: else:
wait_reader(self.fileno(), timeout=self.timeout, timeout_exc=timeout) wait_read(self.fileno(), timeout=self.timeout, timeout_exc=timeout)
except SSL.ZeroReturnError: except SSL.ZeroReturnError:
return '' return ''
except SSL.SysCallError, ex: except SSL.SysCallError, ex:
......
...@@ -69,15 +69,15 @@ class Test(greentest.TestCase): ...@@ -69,15 +69,15 @@ class Test(greentest.TestCase):
assert isinstance(result, AssertionError), result assert isinstance(result, AssertionError), result
assert 'Invalid switch' in str(result), repr(str(result)) assert 'Invalid switch' in str(result), repr(str(result))
def test_wait_reader_invalid_switch(self): def test_wait_read_invalid_switch(self):
p = gevent.spawn(util.wrap_errors(AssertionError, socket.wait_reader), 0) p = gevent.spawn(util.wrap_errors(AssertionError, socket.wait_read), 0)
gevent.spawn(p.switch, None) gevent.spawn(p.switch, None)
result = p.get() result = p.get()
assert isinstance(result, AssertionError), result assert isinstance(result, AssertionError), result
assert 'Invalid switch' in str(result), repr(str(result)) assert 'Invalid switch' in str(result), repr(str(result))
def test_wait_writer_invalid_switch(self): def test_wait_write_invalid_switch(self):
p = gevent.spawn(util.wrap_errors(AssertionError, socket.wait_writer), 0) p = gevent.spawn(util.wrap_errors(AssertionError, socket.wait_write), 0)
gevent.spawn(p.switch, None) gevent.spawn(p.switch, None)
result = p.get() result = p.get()
assert isinstance(result, AssertionError), result assert isinstance(result, AssertionError), result
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment