[Python-Dev] Backporting PEP 3101 to 2.6

André Malo nd at perlig.de
Sun Feb 17 01:41:58 CET 2008


* Nick Coghlan wrote:

> Eric Smith wrote:
> > The bad error message is a result of __format__ passing on unicode to
> > strftime.
> >
> > There are, of course, various ugly ways to work around this involving
> > nested format calls.
>
> I don't know if this fits your definition of "ugly workaround", but what
> if datetime.__format__ did something like:
>
>    def __format__(self, spec):
>      encoding = None
>      if isinstance(spec, unicode):
>          encoding = 'utf-8'
>          spec = spec.encode(encoding)
>      result = strftime(spec, self)
>      if encoding is not None:
>          result = result.decode(encoding)
>      return result

Note that hardcoding utf-8 is a bad guess here as strftime(3) emits locale 
strings, so decoding will easily fail.

I guess, a clean and complete solution (besides re-implementing the whole 
thing) would be to resolve each single format character with strftime, 
decode according to the locale and re-assemble the result string piece by 
piece. Doh!

nd
-- 
> [...] weiß jemand zufällig, was der Tag DIV ausgeschrieben bedeutet?
DIVerses. Benannt nach all dem unstrukturierten Zeug, was die Leute da
so reinpacken und dann absolut positionieren ...
                           -- Florian Hartig und Lars Kasper in dciwam


More information about the Python-Dev mailing list