~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Robert Collins
  • Date: 2010-06-25 20:34:05 UTC
  • mto: This revision was merged to the branch mainline in revision 5324.
  • Revision ID: robertc@robertcollins.net-20100625203405-c74lxd3enklhaqf9
``bzrlib.osutils.get_terminal_encoding`` will now only mutter its
selection when explicitly requested; this avoids many duplicate calls
being logged when helpers, wrappers and older code that manually calls
it are executed it is now logged deliberately by the ui setup code.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
441
441
    getcwd = _mac_getcwd
442
442
 
443
443
 
444
 
def get_terminal_encoding():
 
444
def get_terminal_encoding(trace=False):
445
445
    """Find the best encoding for printing to the screen.
446
446
 
447
447
    This attempts to check both sys.stdout and sys.stdin to see
453
453
 
454
454
    On my standard US Windows XP, the preferred encoding is
455
455
    cp1252, but the console is cp437
 
456
 
 
457
    :param trace: If True trace the selected encoding via mutter().
456
458
    """
457
459
    from bzrlib.trace import mutter
458
460
    output_encoding = getattr(sys.stdout, 'encoding', None)
460
462
        input_encoding = getattr(sys.stdin, 'encoding', None)
461
463
        if not input_encoding:
462
464
            output_encoding = get_user_encoding()
463
 
            mutter('encoding stdout as osutils.get_user_encoding() %r',
 
465
            if trace:
 
466
                mutter('encoding stdout as osutils.get_user_encoding() %r',
464
467
                   output_encoding)
465
468
        else:
466
469
            output_encoding = input_encoding
467
 
            mutter('encoding stdout as sys.stdin encoding %r', output_encoding)
 
470
            if trace:
 
471
                mutter('encoding stdout as sys.stdin encoding %r',
 
472
                    output_encoding)
468
473
    else:
469
 
        mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
 
474
        if trace:
 
475
            mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
470
476
    if output_encoding == 'cp0':
471
477
        # invalid encoding (cp0 means 'no codepage' on Windows)
472
478
        output_encoding = get_user_encoding()
473
 
        mutter('cp0 is invalid encoding.'
 
479
        if trace:
 
480
            mutter('cp0 is invalid encoding.'
474
481
               ' encoding stdout as osutils.get_user_encoding() %r',
475
482
               output_encoding)
476
483
    # check encoding