'Read and return a line from the stream.
If limit is specified, at most limit bytes will be read.
The line terminator is always b\'\n\' for binary files; for text
files, the newlines argument to open can be used to select the line
terminator(s) recognized.'

def readline(self, limit=(-1)):

    if hasattr(self, u'peek'):
        def nreadahead():
            readahead = self.peek(1)
            if (not readahead):
                return 1
            n = ((readahead.find('\n') + 1) or len(readahead))
            if (limit >= 0):
                n = min(n, limit)
            return n
    else:
        def nreadahead():
            return 1
    if (limit is None):
        limit = (-1)
    elif (not isinstance(limit, (int, long))):
        raise TypeError(u'limit    must    be    an    integer')
    res = bytearray()
    while ((limit < 0) or (len(res) < limit)):
        b = self.read(nreadahead())
        if (not b):
            break
        res += b
        if res.endswith('\n'):
            break
    return bytes(res)
