Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit 3a62f64

Browse files
ronfasvetlov
authored andcommitted
Fix ordering issues in UNIX read/write pipe transport constructors (#408)
This commit re-orders the initialization code in the _UnixReadPipeTransport and _UnixWritePipeTransport constructors to make sure all members are assigned a value, even in the case where ValueError is raised due to an incompatible type of pipe. This avoids exceptions being raised in __repr__() due to the missing members. In the case where ValueError is raised, this commit also clears the values of _pipe, _fileno, and _protocol since this transport isn't returned, avoiding a spurious "unclosed transport" warning when the object is garbage-collected.
1 parent a30934a commit 3a62f64

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

asyncio/unix_events.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,20 @@ def __init__(self, loop, pipe, protocol, waiter=None, extra=None):
305305
self._loop = loop
306306
self._pipe = pipe
307307
self._fileno = pipe.fileno()
308+
self._protocol = protocol
309+
self._closing = False
310+
308311
mode = os.fstat(self._fileno).st_mode
309312
if not (stat.S_ISFIFO(mode) or
310313
stat.S_ISSOCK(mode) or
311314
stat.S_ISCHR(mode)):
315+
self._pipe = None
316+
self._fileno = None
317+
self._protocol = None
312318
raise ValueError("Pipe transport is for pipes/sockets only.")
319+
313320
_set_nonblocking(self._fileno)
314-
self._protocol = protocol
315-
self._closing = False
321+
316322
self._loop.call_soon(self._protocol.connection_made, self)
317323
# only start reading when connection_made() has been called
318324
self._loop.call_soon(self._loop.add_reader,
@@ -421,18 +427,23 @@ def __init__(self, loop, pipe, protocol, waiter=None, extra=None):
421427
self._extra['pipe'] = pipe
422428
self._pipe = pipe
423429
self._fileno = pipe.fileno()
430+
self._protocol = protocol
431+
self._buffer = []
432+
self._conn_lost = 0
433+
self._closing = False # Set when close() or write_eof() called.
434+
424435
mode = os.fstat(self._fileno).st_mode
425436
is_char = stat.S_ISCHR(mode)
426437
is_fifo = stat.S_ISFIFO(mode)
427438
is_socket = stat.S_ISSOCK(mode)
428439
if not (is_char or is_fifo or is_socket):
440+
self._pipe = None
441+
self._fileno = None
442+
self._protocol = None
429443
raise ValueError("Pipe transport is only for "
430444
"pipes, sockets and character devices")
445+
431446
_set_nonblocking(self._fileno)
432-
self._protocol = protocol
433-
self._buffer = []
434-
self._conn_lost = 0
435-
self._closing = False # Set when close() or write_eof() called.
436447

437448
self._loop.call_soon(self._protocol.connection_made, self)
438449

0 commit comments

Comments
 (0)