Reading and writing pickles to an encoded stream
<p>A file format commonly used in our system is base64 encoded pickles - at the moment I can translate to and from strings in this trivial format with some simple code like this:</p>

<pre><code>def dumps( objinput ):
    """
    Return an encoded cPickle
    """
    return cpickle_dumps( objinput ).encode( ENCODING )

def loads( strinput ):
    """
    Return an object from an encoded cpickle
    """
    return cpickle_loads( strinput.decode( ENCODING ) )
</code></pre>

<p>I'd like to implement a new function, called "load" which works much like the standard pickle load function except that it will read just enough bytes from a stream and then return the decoded object. The trick here is to read just enough bytes - the cPickle.load function does this, it pops bytes off the stream until the pickled data is complete. My new function needs to look like this:</p>

<pre><code>def load( stream_input ):
    """"
    Return just one object popped from the stream. 
    If the stream has ended, raise an exception
    Do not over-read the stream.
</code></pre>

<p>How might I do this when the data is base64 encoded pickle? The difficulty here seems to be that it's not obvious that this can be done either by wrapping the cPickle.load function or by wrapping the input stream, an additional difficulty is that what may be one-byte of pickle data may be encoded to more than one byte of base64 so it's not clear how to adapt stream_input.read(n) into something which is guaranteed to return n bytes of decoded data when the encoded input stream is base64.</p>
