/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Patch Queue Manager
  • Date: 2012-01-05 11:39:43 UTC
  • mfrom: (6383.1.7 no_locale_hacks_570495)
  • Revision ID: pqm@pqm.ubuntu.com-20120105113943-zeel4wligxkp0tcf
(gz) Simplify osutils.get_user_encoding and remove implicit setlocale calls
 from the function (Martin Packman)

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
lazy_import(globals(), """
29
29
from datetime import datetime
30
30
import getpass
 
31
import locale
31
32
import ntpath
32
33
import posixpath
33
34
import select
54
55
""")
55
56
 
56
57
from bzrlib.symbol_versioning import (
 
58
    DEPRECATED_PARAMETER,
57
59
    deprecated_function,
58
60
    deprecated_in,
 
61
    deprecated_passed,
 
62
    warn as warn_deprecated,
59
63
    )
60
64
 
61
65
from hashlib import (
1976
1980
_cached_user_encoding = None
1977
1981
 
1978
1982
 
1979
 
def get_user_encoding(use_cache=True):
 
1983
def get_user_encoding(use_cache=DEPRECATED_PARAMETER):
1980
1984
    """Find out what the preferred user encoding is.
1981
1985
 
1982
1986
    This is generally the encoding that is used for command line parameters
1983
1987
    and file contents. This may be different from the terminal encoding
1984
1988
    or the filesystem encoding.
1985
1989
 
1986
 
    :param  use_cache:  Enable cache for detected encoding.
1987
 
                        (This parameter is turned on by default,
1988
 
                        and required only for selftesting)
1989
 
 
1990
1990
    :return: A string defining the preferred user encoding
1991
1991
    """
1992
1992
    global _cached_user_encoding
1993
 
    if _cached_user_encoding is not None and use_cache:
 
1993
    if deprecated_passed(use_cache):
 
1994
        warn_deprecated("use_cache should only have been used for tests",
 
1995
            DeprecationWarning, stacklevel=2) 
 
1996
    if _cached_user_encoding is not None:
1994
1997
        return _cached_user_encoding
1995
1998
 
1996
 
    if sys.platform == 'darwin':
1997
 
        # python locale.getpreferredencoding() always return
1998
 
        # 'mac-roman' on darwin. That's a lie.
1999
 
        sys.platform = 'posix'
2000
 
        try:
2001
 
            if os.environ.get('LANG', None) is None:
2002
 
                # If LANG is not set, we end up with 'ascii', which is bad
2003
 
                # ('mac-roman' is more than ascii), so we set a default which
2004
 
                # will give us UTF-8 (which appears to work in all cases on
2005
 
                # OSX). Users are still free to override LANG of course, as
2006
 
                # long as it give us something meaningful. This work-around
2007
 
                # *may* not be needed with python 3k and/or OSX 10.5, but will
2008
 
                # work with them too -- vila 20080908
2009
 
                os.environ['LANG'] = 'en_US.UTF-8'
2010
 
            import locale
2011
 
        finally:
2012
 
            sys.platform = 'darwin'
 
1999
    if os.name == 'posix' and getattr(locale, 'CODESET', None) is not None:
 
2000
        # Use the existing locale settings and call nl_langinfo directly
 
2001
        # rather than going through getpreferredencoding. This avoids
 
2002
        # <http://bugs.python.org/issue6202> on OSX Python 2.6 and the
 
2003
        # possibility of the setlocale call throwing an error.
 
2004
        user_encoding = locale.nl_langinfo(locale.CODESET)
2013
2005
    else:
2014
 
        import locale
 
2006
        # GZ 2011-12-19: On windows could call GetACP directly instead.
 
2007
        user_encoding = locale.getpreferredencoding(False)
2015
2008
 
2016
2009
    try:
2017
 
        user_encoding = locale.getpreferredencoding()
2018
 
    except locale.Error, e:
2019
 
        sys.stderr.write('bzr: warning: %s\n'
2020
 
                         '  Could not determine what text encoding to use.\n'
2021
 
                         '  This error usually means your Python interpreter\n'
2022
 
                         '  doesn\'t support the locale set by $LANG (%s)\n'
2023
 
                         "  Continuing with ascii encoding.\n"
2024
 
                         % (e, os.environ.get('LANG')))
2025
 
        user_encoding = 'ascii'
2026
 
 
2027
 
    # Windows returns 'cp0' to indicate there is no code page. So we'll just
2028
 
    # treat that as ASCII, and not support printing unicode characters to the
2029
 
    # console.
2030
 
    #
2031
 
    # For python scripts run under vim, we get '', so also treat that as ASCII
2032
 
    if user_encoding in (None, 'cp0', ''):
2033
 
        user_encoding = 'ascii'
2034
 
    else:
2035
 
        # check encoding
2036
 
        try:
2037
 
            codecs.lookup(user_encoding)
2038
 
        except LookupError:
 
2010
        user_encoding = codecs.lookup(user_encoding).name
 
2011
    except LookupError:
 
2012
        if user_encoding not in ("", "cp0"):
2039
2013
            sys.stderr.write('bzr: warning:'
2040
2014
                             ' unknown encoding %s.'
2041
2015
                             ' Continuing with ascii encoding.\n'
2042
2016
                             % user_encoding
2043
2017
                            )
2044
 
            user_encoding = 'ascii'
2045
 
 
2046
 
    if use_cache:
2047
 
        _cached_user_encoding = user_encoding
2048
 
 
 
2018
        user_encoding = 'ascii'
 
2019
    else:
 
2020
        # Get 'ascii' when setlocale has not been called or LANG=C or unset.
 
2021
        if user_encoding == 'ascii':
 
2022
            if sys.platform == 'darwin':
 
2023
                # OSX is special-cased in Python to have a UTF-8 filesystem
 
2024
                # encoding and previously had LANG set here if not present.
 
2025
                user_encoding = 'utf-8'
 
2026
            # GZ 2011-12-19: Maybe UTF-8 should be the default in this case
 
2027
            #                for some other posix platforms as well.
 
2028
 
 
2029
    _cached_user_encoding = user_encoding
2049
2030
    return user_encoding
2050
2031
 
2051
2032
 
2053
2034
    return get_terminal_encoding()
2054
2035
 
2055
2036
 
2056
 
_message_encoding = None
2057
 
 
2058
 
 
2059
 
def get_message_encoding():
2060
 
    """Return the encoding used for messages
2061
 
 
2062
 
    While the message encoding is a general setting it should usually only be
2063
 
    needed for decoding system error strings such as from OSError instances.
2064
 
    """
2065
 
    global _message_encoding
2066
 
    if _message_encoding is None:
2067
 
        if os.name == "posix":
2068
 
            import locale
2069
 
            # This is a process-global setting that can change, but should in
2070
 
            # general just get set once at process startup then be constant.
2071
 
            _message_encoding = locale.getlocale(locale.LC_MESSAGES)[1]
2072
 
        else:
2073
 
            # On windows want the result of GetACP() which this boils down to.
2074
 
            _message_encoding = get_user_encoding()
2075
 
    return _message_encoding or "ascii"
2076
 
 
2077
 
 
2078
2037
def get_host_name():
2079
2038
    """Return the current unicode host name.
2080
2039