~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Robert Collins
  • Date: 2006-01-05 22:30:59 UTC
  • mto: (1534.1.4 integration)
  • mto: This revision was merged to the branch mainline in revision 1536.
  • Revision ID: robertc@robertcollins.net-20060105223059-a8b64f7b47cf12fb
 * bzrlib.osutils.safe_unicode now exists to provide parameter coercion
   for functions that need unicode strings. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
import tempfile
32
32
 
33
33
import bzrlib
34
 
from bzrlib.errors import BzrError, PathNotChild, NoSuchFile
 
34
from bzrlib.errors import (BzrError,
 
35
                           BzrBadParameter,
 
36
                           NoSuchFile,
 
37
                           PathNotChild,
 
38
                           )
35
39
from bzrlib.trace import mutter
36
40
 
37
41
 
578
582
        return ''
579
583
 
580
584
 
 
585
def safe_unicode(unicode_or_utf8_string):
 
586
    """Coerce unicode_or_utf8_string into unicode.
 
587
 
 
588
    If it is unicode, it is returned.
 
589
    Otherwise it is decoded from utf-8. If a decoding error
 
590
    occurs, it is wrapped as a If the decoding fails, the exception is wrapped 
 
591
    as a BzrBadParameter exception.
 
592
    """
 
593
    if isinstance(unicode_or_utf8_string, unicode):
 
594
        return unicode_or_utf8_string
 
595
    try:
 
596
        return unicode_or_utf8_string.decode('utf8')
 
597
    except UnicodeDecodeError:
 
598
        raise BzrBadParameter(unicode_or_utf8_string)
 
599
 
 
600
 
581
601
def terminal_width():
582
602
    """Return estimated terminal width."""
583
603