cannot savefig to cStringIO

Hello!

I'm dynamically dishing out plots via matplotlib through a web server,
so I really want to savefig to some type of memory object.

Using both os.pipe and cStringIO objects, I get this type of error:

In [9]:%history
1 : import matplotlib
2 : matplotlib.use('Agg')
3 : import pylab
4 : ax = pylab.gca()
5 : ax.specgram( range(100) )
6 : import cStringIO
7 : sbuf = cStringIO.StringIO()
8 : pylab.savefig( sbuf, format='png' )

In [8]:pylab.savefig( sbuf, format='png' )

···

---------------------------------------------------------------------------
exceptions.TypeError Traceback (most
recent call last)

/home/gmabey/src/R9619_zippy/Projects/R9619_NChannelDetection/AcqLibWeb/<ipython console>

/usr/local/stow/matplotlib-0.87.7/lib/python2.4/site-packages/matplotlib/pylab.py
in savefig(*args, **kwargs)
    811 def savefig(*args, **kwargs):
    812 fig = gcf()
--> 813 return fig.savefig(*args, **kwargs)
    814 if Figure.savefig.__doc__ is not None:
    815 savefig.__doc__ = _shift_string(Figure.savefig.__doc__)

/usr/local/stow/matplotlib-0.87.7/lib/python2.4/site-packages/matplotlib/figure.py
in savefig(self, *args, **kwargs)
    680 kwargs[key] = rcParams['savefig.%s'%key]
    681
--> 682 self.canvas.print_figure(*args, **kwargs)
    683
    684 def colorbar(self, mappable, cax=None, **kw):

/usr/local/stow/matplotlib-0.87.7/lib/python2.4/site-packages/matplotlib/backends/backend_agg.py
in print_figure(self, filename, dpi, facecolor, edgecolor, orientation,
**kwargs)
    458 if not is_string_like(filename):
    459 # assume png and write to fileobject
--> 460 self.renderer._renderer.write_png(filename)
    461 #pass
    462 else:

TypeError: Could not convert object to file pointer

/usr/local/stow/matplotlib-0.87.7/lib/python2.4/site-packages/matplotlib/backends/backend_agg.py(460)print_figure()

    459 # assume png and write to fileobject
--> 460 self.renderer._renderer.write_png(filename)
    461 #pass

I'm a bit confused as to where the exception is actually getting raised.
I suppose the renderer's write_png method must be in C?

I implemented a tempfile solution, but I'm surprised that the cStringIO
doesn't already work.

<fellowsophical_question>

Should it?

</fellowsophical_question>

Thanks,
Glen