def read_or_raise(reader, n=-1): """ Read a given byte number from Stream. NoDataException is raised if read gives no data :param reader: reader adapter :param n: number of bytes to read :return: bytes read """ data = yield from reader.read(n) if not data: raise NoDataException("No more data") return data
async def read_or_raise(reader, n=-1): """ Read a given byte number from Stream. NoDataException is raised if read gives no data :param reader: reader adapter :param n: number of bytes to read :return: bytes read """ try: data = await reader.read(n) except (asyncio.IncompleteReadError, ConnectionResetError, BrokenPipeError): data = None if not data: raise NoDataException("No more data") return data