feature request / max size limit on StringIO

Clark C. Evans cce at clarkevans.com
Tue Oct 25 14:15:02 EDT 2005


Hello.  I've not been able to use cStringIO since I have the need to
ensure that the memory buffers created are bounded within a resonable
limit set by specifications.  No, this code does not properly belong
in my application as the modules that use "files" should not have
to care about any resource limitations that may be imposed.

class LimitedBuffer(StringIO):
    def __init__(self, buffer = None, maxsize = 5 * 1000 * 1000):
        StringIO.__init__(self,buffer)
        self.cursize = 0
        self.maxsize = maxsize
    def write(self,str):
        self.cursize += len(str)
        if self.cursize > self.maxsize:
            raise IOError("allocated buffer space exceeded")
        return StringIO.write(self,str)

Kind Regards,

Clark Evans



More information about the Python-list mailing list