unicode and SVG

Anyone know of a simple fix for this

python unicode_demo.py -dSVG

  File "/usr/lib/python2.4/site-packages/matplotlib/backends/backend_svg.py", line 196, in draw_text
    self._svgwriter.write (svg)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 136: ordinal not in range(128)

John Hunter wrote:

Anyone know of a simple fix for this

python unicode_demo.py -dSVG

  File "/usr/lib/python2.4/site-packages/matplotlib/backends/backend_svg.py", line 196, in draw_text
    self._svgwriter.write (svg)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 136: ordinal not in range(128)

I recently had to fix the same bug for ipython. Here's what my stumble-in-the-dark approach to unicode bug fixing (no net access at home that day) produced:

     def _str(self,glob,loc):
         """Evaluate to a string in the given globals/locals.

         The final output is built by calling str(), but if this fails, the
         result is encoded with the instance's codec and error handling policy,
         via a call to out.encode(self.codec,self.encoding_errors)"""
         result = []
         app = result.append
         for live, chunk in self.chunks:
             if live: app(str(eval(chunk,glob,loc)))
             else: app(chunk)
         out = ''.join(result)
         try:
             return str(out)
         except UnicodeError:
             return out.encode(self.codec,self.encoding_errors)

where the codec stuff is set via:

         Optional arguments:

         - codec('utf_8'): a string containing the name of a valid Python
         codec.

         - encoding_errors('backslashreplace'): a string with a valid error handling
         policy. See the codecs module documentation for details.

         These are used to encode the format string if a call to str() fails on
         the expanded result."""

         if not isinstance(format,basestring):
             raise TypeError, "needs string initializer"
         self.format = format
         self.codec = codec
         self.encoding_errors = encoding_errors

This is all in ipython's Itpl.py module, in case you want to look closer (the crash was there b/c that's the machinery used for prompt printing).

cheers,

f