~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Robert Collins
  • Date: 2007-03-07 01:14:11 UTC
  • mfrom: (2321 +trunk)
  • mto: (2321.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 2322.
  • Revision ID: robertc@robertcollins.net-20070307011411-0cmmc8atx67v3nv7
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
""")
55
55
 
56
56
import bzrlib
 
57
from bzrlib import symbol_versioning
57
58
from bzrlib.symbol_versioning import (
58
59
    deprecated_function,
59
60
    zero_nine,
904
905
    return unicode_or_utf8_string.encode('utf-8')
905
906
 
906
907
 
907
 
def safe_revision_id(unicode_or_utf8_string):
 
908
_revision_id_warning = ('Unicode revision ids were deprecated in bzr 0.15.'
 
909
                        ' Revision id generators should be creating utf8'
 
910
                        ' revision ids.')
 
911
 
 
912
 
 
913
def safe_revision_id(unicode_or_utf8_string, warn=True):
908
914
    """Revision ids should now be utf8, but at one point they were unicode.
909
915
 
 
916
    :param unicode_or_utf8_string: A possibly Unicode revision_id. (can also be
 
917
        utf8 or None).
 
918
    :param warn: Functions that are sanitizing user data can set warn=False
 
919
    :return: None or a utf8 revision id.
 
920
    """
 
921
    if (unicode_or_utf8_string is None
 
922
        or unicode_or_utf8_string.__class__ == str):
 
923
        return unicode_or_utf8_string
 
924
    if warn:
 
925
        symbol_versioning.warn(_revision_id_warning, DeprecationWarning,
 
926
                               stacklevel=2)
 
927
    return cache_utf8.encode(unicode_or_utf8_string)
 
928
 
 
929
 
 
930
_file_id_warning = ('Unicode file ids were deprecated in bzr 0.15. File id'
 
931
                    ' generators should be creating utf8 file ids.')
 
932
 
 
933
 
 
934
def safe_file_id(unicode_or_utf8_string, warn=True):
 
935
    """File ids should now be utf8, but at one point they were unicode.
 
936
 
910
937
    This is the same as safe_utf8, except it uses the cached encode functions
911
938
    to save a little bit of performance.
 
939
 
 
940
    :param unicode_or_utf8_string: A possibly Unicode file_id. (can also be
 
941
        utf8 or None).
 
942
    :param warn: Functions that are sanitizing user data can set warn=False
 
943
    :return: None or a utf8 file id.
912
944
    """
913
 
    if unicode_or_utf8_string is None:
914
 
        return None
915
 
    if isinstance(unicode_or_utf8_string, str):
916
 
        # TODO: jam 20070209 Eventually just remove this check.
917
 
        try:
918
 
            utf8_str = cache_utf8.get_cached_utf8(unicode_or_utf8_string)
919
 
        except UnicodeDecodeError:
920
 
            raise errors.BzrBadParameterNotUnicode(unicode_or_utf8_string)
921
 
        return utf8_str
 
945
    if (unicode_or_utf8_string is None
 
946
        or unicode_or_utf8_string.__class__ == str):
 
947
        return unicode_or_utf8_string
 
948
    if warn:
 
949
        symbol_versioning.warn(_file_id_warning, DeprecationWarning,
 
950
                               stacklevel=2)
922
951
    return cache_utf8.encode(unicode_or_utf8_string)
923
952
 
924
953
 
925
 
# TODO: jam 20070217 We start by just re-using safe_revision_id, but ultimately
926
 
#       we want to use a different dictionary cache, because trapping file ids
927
 
#       and revision ids in the same dict seemed to have a noticable effect on
928
 
#       performance.
929
 
safe_file_id = safe_revision_id
930
 
 
931
 
 
932
954
_platform_normalizes_filenames = False
933
955
if sys.platform == 'darwin':
934
956
    _platform_normalizes_filenames = True