Add some caching to

Hi all,

I just had this crash in mpl:

/usr/lib/python2.3/site-packages/matplotlib/ in get_dvipng_version(self)
     299 def get_dvipng_version(self):
     300 if self.dvipngVersion is not None: return self.dvipngVersion
--> 301 sin, sout = os.popen2('dvipng --version')
     302 for line in sout.readlines():
     303 if line.startswith('dvipng '):

/usr/lib/python2.3/ in popen2(cmd, mode, bufsize)
     614 def popen2(cmd, mode="t", bufsize=-1):
     615 import popen2
--> 616 stdout, stdin = popen2.popen2(cmd, bufsize)
     617 return stdin, stdout
     618 __all__.append("popen2")

/usr/lib/python2.3/ in popen2(cmd, bufsize, mode)
     145 specified, it sets the buffer size for the I/O pipes. The file objects
     146 (child_stdout, child_stdin) are returned."""
--> 147 inst = Popen3(cmd, False, bufsize)
     148 return inst.fromchild, inst.tochild

/usr/lib/python2.3/ in __init__(self, cmd, capturestderr, bufsize)
      40 if capturestderr:
      41 errout, errin = os.pipe()
---> 42 = os.fork()
      43 if == 0:
      44 # Child

OSError: [Errno 12] Cannot allocate memory

This was in a long-running session with very large memory allocations, but it dawned on me that get_dvipng_version(self) should cache its return value. There's no point in forcing a popen() call every single time, is there?

I don't know the texmanager code, so I don't want to touch it myself. But if the idea is OK, a simple property tag is enough to cache:

In [3]: def cached():
    ...: try:
    ...: return cached.cachedvalue
    ...: except AttributeError:
    ...: cached.cachedvalue=42
    ...: return cached.cachedvalue

In [4]: cached()
Out[4]: 42

I also just saw pylab crash when a user was trying to run with $PWD being something he didn't have write access to. Are there any checks in the code to fall back to /tmp or something sensible if texmanager can't write the temp files it needs? Sorry for not giving a traceback, but I only saw this on someone else's screen while helping them, and for some odd reason it's not happening on my box.