943
949
return os.fstat(f.fileno())[stat.ST_SIZE]
946
# Define rand_bytes based on platform.
948
# Python 2.4 and later have os.urandom,
949
# but it doesn't work on some arches
951
rand_bytes = os.urandom
952
except (NotImplementedError, AttributeError):
953
# If python doesn't have os.urandom, or it doesn't work,
954
# then try to first pull random data from /dev/urandom
952
# Alias os.urandom to support platforms (which?) without /dev/urandom and
953
# override if it doesn't work. Avoid checking on windows where there is
954
# significant initialisation cost that can be avoided for some bzr calls.
956
rand_bytes = os.urandom
958
if rand_bytes.__module__ != "nt":
956
rand_bytes = file('/dev/urandom', 'rb').read
957
# Otherwise, use this hack as a last resort
958
except (IOError, OSError):
961
except NotImplementedError:
959
962
# not well seeded, but better than nothing
960
963
def rand_bytes(n):
1977
1980
_cached_user_encoding = None
1980
def get_user_encoding(use_cache=True):
1983
def get_user_encoding(use_cache=DEPRECATED_PARAMETER):
1981
1984
"""Find out what the preferred user encoding is.
1983
1986
This is generally the encoding that is used for command line parameters
1984
1987
and file contents. This may be different from the terminal encoding
1985
1988
or the filesystem encoding.
1987
:param use_cache: Enable cache for detected encoding.
1988
(This parameter is turned on by default,
1989
and required only for selftesting)
1991
1990
:return: A string defining the preferred user encoding
1993
1992
global _cached_user_encoding
1994
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:
1995
1997
return _cached_user_encoding
1997
if sys.platform == 'darwin':
1998
# python locale.getpreferredencoding() always return
1999
# 'mac-roman' on darwin. That's a lie.
2000
sys.platform = 'posix'
2002
if os.environ.get('LANG', None) is None:
2003
# If LANG is not set, we end up with 'ascii', which is bad
2004
# ('mac-roman' is more than ascii), so we set a default which
2005
# will give us UTF-8 (which appears to work in all cases on
2006
# OSX). Users are still free to override LANG of course, as
2007
# long as it give us something meaningful. This work-around
2008
# *may* not be needed with python 3k and/or OSX 10.5, but will
2009
# work with them too -- vila 20080908
2010
os.environ['LANG'] = 'en_US.UTF-8'
2013
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)
2006
# GZ 2011-12-19: On windows could call GetACP directly instead.
2007
user_encoding = locale.getpreferredencoding(False)
2018
user_encoding = locale.getpreferredencoding()
2019
except locale.Error, e:
2020
sys.stderr.write('bzr: warning: %s\n'
2021
' Could not determine what text encoding to use.\n'
2022
' This error usually means your Python interpreter\n'
2023
' doesn\'t support the locale set by $LANG (%s)\n'
2024
" Continuing with ascii encoding.\n"
2025
% (e, os.environ.get('LANG')))
2026
user_encoding = 'ascii'
2028
# Windows returns 'cp0' to indicate there is no code page. So we'll just
2029
# treat that as ASCII, and not support printing unicode characters to the
2032
# For python scripts run under vim, we get '', so also treat that as ASCII
2033
if user_encoding in (None, 'cp0', ''):
2034
user_encoding = 'ascii'
2038
codecs.lookup(user_encoding)
2010
user_encoding = codecs.lookup(user_encoding).name
2012
if user_encoding not in ("", "cp0"):
2040
2013
sys.stderr.write('bzr: warning:'
2041
2014
' unknown encoding %s.'
2042
2015
' Continuing with ascii encoding.\n'
2043
2016
% user_encoding
2045
user_encoding = 'ascii'
2048
_cached_user_encoding = user_encoding
2018
user_encoding = 'ascii'
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.
2029
_cached_user_encoding = user_encoding
2050
2030
return user_encoding
2054
2034
return get_terminal_encoding()
2057
_message_encoding = None
2060
def get_message_encoding():
2061
"""Return the encoding used for messages
2063
While the message encoding is a general setting it should usually only be
2064
needed for decoding system error strings such as from OSError instances.
2066
global _message_encoding
2067
if _message_encoding is None:
2068
if os.name == "posix":
2070
# This is a process-global setting that can change, but should in
2071
# general just get set once at process startup then be constant.
2072
_message_encoding = locale.getlocale(locale.LC_MESSAGES)[1]
2074
# On windows want the result of GetACP() which this boils down to.
2075
_message_encoding = get_user_encoding()
2076
return _message_encoding or "ascii"
2079
2037
def get_host_name():
2080
2038
"""Return the current unicode host name.