[patch] small user_encoding fix

Robert Collins robertc at robertcollins.net
Mon Feb 20 23:27:11 GMT 2006


On Mon, 2006-02-20 at 17:06 +0100, Marien Zwart wrote:
> 
> -user_encoding = locale.getpreferredencoding() or 'ascii'
> +try:
> +    user_encoding = locale.getpreferredencoding() or 'ascii'
> +except locale.Error:
> +    user_encoding = 'ascii'
>  del locale 

Its not -quite- trivial... 

So to test this I would do the following: 
 * make the code in question a function to allow it to be invoked at
other times than the system startup:

def get_user_encoding():
    """Calculate the use encoding to be used by bzrlib."""
    user_encoding = locale.getpreferredencoding()
    if not user_encoding:
        user_encoding = 'ascii'
    return user_encoding

But its not quite testable yet... because the bit that blows up is
outside our codebase. So:

def get_user_encoding(locale):
    """Calculate the use encoding to be used by bzrlib.

    :param locale: A locale module. This must provide 
                   getpreferredencoding() and a Error 
                   exception which it throws on failure.
    """
    user_encoding = locale.getpreferredencoding()
    if not user_encoding:
        user_encoding = 'ascii'
    return user_encoding


We can now trivially mock up a locale to test with:

class FakeLocale(object):
    """"Fake locale that will throw an error."""

    class Error(Exception):
        """A Fake Locale Error."""

    def getpreferredencoding(self):
        raise self.Error


and in a test case:
    def test_locale_errors_result_in_ascii(self):
        self.assertEqual('ascii', get_user_encoding(FakeLocale())

will fail until you change the code:

def get_user_encoding(locale):
    """Calculate the use encoding to be used by bzrlib.

    :param locale: A locale module. This must provide 
                   getpreferredencoding() and a Error 
                   exception which it throws on failure.
    """
    try: 
       user_encoding = locale.getpreferredencoding()
    except locale.Error:
       user_encoding = None
    if not user_encoding:
        user_encoding = 'ascii'
    return user_encoding


Cheers,
Rob
-- 
GPG key available at: <http://www.robertcollins.net/keys.txt>.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20060221/c71db833/attachment.pgp 


More information about the bazaar mailing list