[merge] Unicode Exception fixes

Aaron Bentley aaron.bentley at utoronto.ca
Mon Aug 21 21:06:50 BST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

John Arbash Meinel wrote:
> There are a few bugs listed, which occur because of how we raise errors
> with Unicode strings.
> -    cmd = str(argv.pop(0))
> +    cmd = argv.pop(0)
> +    try:
> +        cmd = cmd.encode('ascii')
> +    except UnicodeError:
> +        raise errors.UnicodeCommand(cmd)

Do we actually want a specific error here?  I would think we should
continue, and let get_cmd_object raise "unknown command" as usual.  No
need to complain about the fact that it's unicode; the fact that it's
unknown is enough.


> --- bzrlib/errors.py
> +++ bzrlib/errors.py
> @@ -126,7 +126,12 @@
>  
>      def __str__(self):
>          try:
> -            return self.__doc__ % self.__dict__
> +            # __str__() should always return a 'str' object
> +            # never a 'unicode' object.
> +            s = self.__doc__ % self.__dict__
> +            if isinstance(s, unicode):
> +                return s.encode('utf8')
> +            return s
>          except (NameError, ValueError, KeyError), e:
>              return 'Unprintable exception %s: %s' \
>                  % (self.__class__.__name__, str(e))

I think this is going too far; a string may be unicode while still being
convertible-to-ascii.  In any case, we may well be able to print errors
in unicode, which would be preferable.

How about:

    def __unicode__(self):
            s = self.__doc__ % self.__dict__
            return s

    def __str__(self):
            # __str__() should always return a 'str' object
            # never a 'unicode' object.
            s = unicode(self)
            try:
                return str(s)
            except UnicodeEncodeError:
                return s.encode('utf8')
Aaron
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFE6hJa0F+nu1YWqI0RAsFbAJwKZPzUcuxTCS0tWK5vUQ1//net4QCfUnJO
e3E5yZLEfxBmj2Or6KbjE8M=
=HkgH
-----END PGP SIGNATURE-----




More information about the bazaar mailing list