Rev 5324: ``bzrlib.osutils.get_terminal_encoding`` will now only mutter its in http://bazaar.launchpad.net/~lifeless/bzr/globalzapping

Robert Collins robertc at robertcollins.net
Fri Jun 25 21:34:21 BST 2010


At http://bazaar.launchpad.net/~lifeless/bzr/globalzapping

------------------------------------------------------------
revno: 5324
revision-id: robertc at robertcollins.net-20100625203405-c74lxd3enklhaqf9
parent: robertc at robertcollins.net-20100625062308-qx287gzfrehs1d21
committer: Robert Collins <robertc at robertcollins.net>
branch nick: globalzapping
timestamp: Sat 2010-06-26 06:34:05 +1000
message:
  ``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)
=== modified file 'NEWS'
--- a/NEWS	2010-06-25 06:11:21 +0000
+++ b/NEWS	2010-06-25 20:34:05 +0000
@@ -177,6 +177,12 @@
 Internals
 *********
 
+* ``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)
+
 * Improved ``bzrlib.urlutils`` to handle lp:foo/bar URLs. (Gordon Tyler)
 
 * The symbol_versioning module can now cleanup after itself -

=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2010-06-18 10:57:20 +0000
+++ b/bzrlib/osutils.py	2010-06-25 20:34:05 +0000
@@ -441,7 +441,7 @@
     getcwd = _mac_getcwd
 
 
-def get_terminal_encoding():
+def get_terminal_encoding(trace=False):
     """Find the best encoding for printing to the screen.
 
     This attempts to check both sys.stdout and sys.stdin to see
@@ -453,6 +453,8 @@
 
     On my standard US Windows XP, the preferred encoding is
     cp1252, but the console is cp437
+
+    :param trace: If True trace the selected encoding via mutter().
     """
     from bzrlib.trace import mutter
     output_encoding = getattr(sys.stdout, 'encoding', None)
@@ -460,17 +462,22 @@
         input_encoding = getattr(sys.stdin, 'encoding', None)
         if not input_encoding:
             output_encoding = get_user_encoding()
-            mutter('encoding stdout as osutils.get_user_encoding() %r',
+            if trace:
+                mutter('encoding stdout as osutils.get_user_encoding() %r',
                    output_encoding)
         else:
             output_encoding = input_encoding
-            mutter('encoding stdout as sys.stdin encoding %r', output_encoding)
+            if trace:
+                mutter('encoding stdout as sys.stdin encoding %r',
+                    output_encoding)
     else:
-        mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
+        if trace:
+            mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
     if output_encoding == 'cp0':
         # invalid encoding (cp0 means 'no codepage' on Windows)
         output_encoding = get_user_encoding()
-        mutter('cp0 is invalid encoding.'
+        if trace:
+            mutter('cp0 is invalid encoding.'
                ' encoding stdout as osutils.get_user_encoding() %r',
                output_encoding)
     # check encoding

=== modified file 'bzrlib/tests/test_osutils_encodings.py'
--- a/bzrlib/tests/test_osutils_encodings.py	2010-01-25 17:48:22 +0000
+++ b/bzrlib/tests/test_osutils_encodings.py	2010-06-25 20:34:05 +0000
@@ -114,6 +114,26 @@
         # and in the worst case, use osutils.get_user_encoding()
         self.assertEqual('user_encoding', osutils.get_terminal_encoding())
 
+    def test_get_terminal_encoding_silent(self):
+        self.make_wrapped_streams('stdout_encoding',
+                                  'stderr_encoding',
+                                  'stdin_encoding')
+        # Calling get_terminal_encoding should not mutter when silent=True is
+        # passed.
+        log = self.get_log()
+        osutils.get_terminal_encoding()
+        self.assertEqual(log, self.get_log())
+
+    def test_get_terminal_encoding_trace(self):
+        self.make_wrapped_streams('stdout_encoding',
+                                  'stderr_encoding',
+                                  'stdin_encoding')
+        # Calling get_terminal_encoding should not mutter when silent=True is
+        # passed.
+        log = self.get_log()
+        osutils.get_terminal_encoding(trace=True)
+        self.assertNotEqual(log, self.get_log())
+
     def test_terminal_cp0(self):
         # test cp0 encoding (Windows returns cp0 when there is no encoding)
         self.make_wrapped_streams('cp0',

=== modified file 'bzrlib/trace.py'
--- a/bzrlib/trace.py	2010-06-21 03:15:55 +0000
+++ b/bzrlib/trace.py	2010-06-25 20:34:05 +0000
@@ -307,9 +307,6 @@
 
     Output can be redirected away by calling _push_log_file.
     """
-    # Do this before we open the log file, so we prevent
-    # get_terminal_encoding() from mutter()ing multiple times
-    term_encoding = osutils.get_terminal_encoding()
     start_time = osutils.format_local_date(_bzr_log_start_time,
                                            timezone='local')
     # create encoded wrapper around stderr
@@ -321,6 +318,7 @@
         r'%Y-%m-%d %H:%M:%S')
     # after hooking output into bzr_log, we also need to attach a stderr
     # handler, writing only at level info and with encoding
+    term_encoding = osutils.get_terminal_encoding()
     writer_factory = codecs.getwriter(term_encoding)
     encoded_stderr = writer_factory(sys.stderr, errors='replace')
     stderr_handler = logging.StreamHandler(encoded_stderr)

=== modified file 'bzrlib/ui/__init__.py'
--- a/bzrlib/ui/__init__.py	2010-06-23 08:14:21 +0000
+++ b/bzrlib/ui/__init__.py	2010-06-25 20:34:05 +0000
@@ -192,7 +192,7 @@
             encoding = config.GlobalConfig().get_user_option(
                 'output_encoding')
         if encoding is None:
-            encoding = osutils.get_terminal_encoding()
+            encoding = osutils.get_terminal_encoding(trace=True)
         if encoding_type is None:
             encoding_type = 'replace'
         out_stream = self._make_output_stream_explicit(encoding, encoding_type)




More information about the bazaar-commits mailing list