a simple readline class that uses an arbitrary read function and buffering
| 1776 | |
| 1777 | |
| 1778 | class Readliner: |
| 1779 | """ |
| 1780 | a simple readline class that uses an arbitrary read function and buffering |
| 1781 | """ |
| 1782 | def __init__(self, readfunc): |
| 1783 | self.readfunc = readfunc |
| 1784 | self.remainder = b"" |
| 1785 | |
| 1786 | def readline(self, limit): |
| 1787 | data = [] |
| 1788 | datalen = 0 |
| 1789 | read = self.remainder |
| 1790 | try: |
| 1791 | while True: |
| 1792 | idx = read.find(b'\n') |
| 1793 | if idx != -1: |
| 1794 | break |
| 1795 | if datalen + len(read) >= limit: |
| 1796 | idx = limit - datalen - 1 |
| 1797 | # read more data |
| 1798 | data.append(read) |
| 1799 | read = self.readfunc() |
| 1800 | if not read: |
| 1801 | idx = 0 #eof condition |
| 1802 | break |
| 1803 | idx += 1 |
| 1804 | data.append(read[:idx]) |
| 1805 | self.remainder = read[idx:] |
| 1806 | return b"".join(data) |
| 1807 | except: |
| 1808 | self.remainder = b"".join(data) |
| 1809 | raise |
| 1810 | |
| 1811 | |
| 1812 | class OfflineTest(TestCase): |
no outgoing calls
searching dependent graphs…