python 3: reading bytes from stdin pipe with readahead

前端 未结 3 1819
长情又很酷
长情又很酷 2021-01-11 18:08

i want to read bytes. sys.stdin is opened in textmode, yet it has a buffer that can be used to read bytes: sys.stdin.buffer.

my problem is

3条回答
  •  清歌不尽
    2021-01-11 18:45

    The exception doesn't come from Python, but from the operating system, which doesn't allow seeking on pipes. (If you redirect output from a regular pipe, it can be seeked, even though it's standard input.) This is why you get the error in one case and not in the other, even though the classes are the same.

    The classic Python 2 solution for readahead would be to wrap the stream in your own stream implementation that implements readahead:

    class Peeker(object):
        def __init__(self, fileobj):
            self.fileobj = fileobj
            self.buf = cStringIO.StringIO()
    
        def _append_to_buf(self, contents):
            oldpos = self.buf.tell()
            self.buf.seek(0, os.SEEK_END)
            self.buf.write(contents)
            self.buf.seek(oldpos)
    
        def peek(self, size):
            contents = self.fileobj.read(size)
            self._append_to_buf(contents)
            return contents
    
        def read(self, size=None):
            if size is None:
                return self.buf.read() + self.fileobj.read()
            contents = self.buf.read(size)
            if len(contents) < size:
                contents += self.fileobj.read(size - len(contents))
            return contents
    
        def readline(self):
            line = self.buf.readline()
            if not line.endswith('\n'):
                line += self.fileobj.readline()
            return line
    
    sys.stdin = Peeker(sys.stdin)
    

    In Python 3 supporting the full sys.stdin while peeking the undecoded stream is complicated—one would wrap stdin.buffer as shown above, then instantiate a new TextIOWrapper over your peekable stream, and install that TextIOWrapper as sys.stdin.

    However, since you only need to peek at sys.stdin.buffer, the above code will work just fine, after changing cStringIO.StringIO to io.BytesIO and '\n' to b'\n'.

提交回复
热议问题