Skip to content

Commit fc1bf5a

Browse files
committed
socket: sync error handling with posix version
fixes pyserial#220
1 parent 1c8dbee commit fc1bf5a

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

serial/urlhandler/protocol_socket.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,15 @@ def read(self, size=1):
170170
read.extend(buf)
171171
except OSError as e:
172172
# this is for Python 3.x where select.error is a subclass of
173-
# OSError ignore EAGAIN errors. other errors are shown
174-
if e.errno not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS):
173+
# OSError ignore BlockingIOErrors and EINTR. other errors are shown
174+
# https://www.python.org/dev/peps/pep-0475.
175+
if e.errno not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS, errno.EINTR):
175176
raise SerialException('read failed: {}'.format(e))
176177
except (select.error, socket.error) as e:
177178
# this is for Python 2.x
178-
# ignore EAGAIN errors. all other errors are shown
179+
# ignore BlockingIOErrors and EINTR. all errors are shown
179180
# see also http://www.python.org/dev/peps/pep-3151/#select
180-
if e[0] not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS):
181+
if e[0] not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS, errno.EINTR):
181182
raise SerialException('read failed: {}'.format(e))
182183
if timeout.expired():
183184
break
@@ -220,12 +221,20 @@ def write(self, data):
220221
tx_len -= n
221222
except SerialException:
222223
raise
223-
except OSError as v:
224-
if v.errno != errno.EAGAIN:
225-
raise SerialException('write failed: {}'.format(v))
226-
# still calculate and check timeout
227-
if timeout.expired():
228-
raise writeTimeoutError
224+
except OSError as e:
225+
# this is for Python 3.x where select.error is a subclass of
226+
# OSError ignore BlockingIOErrors and EINTR. other errors are shown
227+
# https://www.python.org/dev/peps/pep-0475.
228+
if e.errno not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS, errno.EINTR):
229+
raise SerialException('write failed: {}'.format(e))
230+
except select.error as e:
231+
# this is for Python 2.x
232+
# ignore BlockingIOErrors and EINTR. all errors are shown
233+
# see also http://www.python.org/dev/peps/pep-3151/#select
234+
if e[0] not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS, errno.EINTR):
235+
raise SerialException('write failed: {}'.format(e))
236+
if not timeout.is_non_blocking and timeout.expired():
237+
raise writeTimeoutError
229238
return length - len(d)
230239

231240
def reset_input_buffer(self):
@@ -241,15 +250,16 @@ def reset_input_buffer(self):
241250
self._socket.recv(4096)
242251
except OSError as e:
243252
# this is for Python 3.x where select.error is a subclass of
244-
# OSError ignore EAGAIN errors. all other errors are shown
245-
if e.errno not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS):
246-
raise SerialException('reset_input_buffer failed: {}'.format(e))
253+
# OSError ignore BlockingIOErrors and EINTR. other errors are shown
254+
# https://www.python.org/dev/peps/pep-0475.
255+
if e.errno not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS, errno.EINTR):
256+
raise SerialException('read failed: {}'.format(e))
247257
except (select.error, socket.error) as e:
248258
# this is for Python 2.x
249-
# ignore EAGAIN errors. all other errors are shown
259+
# ignore BlockingIOErrors and EINTR. all errors are shown
250260
# see also http://www.python.org/dev/peps/pep-3151/#select
251-
if e[0] not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS):
252-
raise SerialException('reset_input_buffer failed: {}'.format(e))
261+
if e[0] not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS, errno.EINTR):
262+
raise SerialException('read failed: {}'.format(e))
253263

254264
def reset_output_buffer(self):
255265
"""\

0 commit comments

Comments
 (0)