'Read a line with approx. size. If size is negative,
read a whole line.'

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

    if (size < 0):
        size = 2147483647
    elif (size == 0):
        return ''
    (nl, nllen) = self._checkfornewline()
    if (nl >= 0):
        nl = min(nl, size)
    else:
        size -= len(self.linebuffer)
        while ((nl < 0) and (size > 0)):
            buf = self.read(min(size, 100))
            if (not buf):
                break
            self.linebuffer += buf
            size -= len(buf)
            (nl, nllen) = self._checkfornewline()
        if (nl < 0):
            s = self.linebuffer
            self.linebuffer = ''
            return s
    buf = self.linebuffer[:nl]
    self.lastdiscard = self.linebuffer[nl:(nl + nllen)]
    self.linebuffer = self.linebuffer[(nl + nllen):]
    return (buf + '\n')
