[MERGE] Before actually using encoding need to check that Python has corresponding codec (v.3)

Alexander Belchenko bialix at ukr.net
Tue Jan 2 16:11:00 GMT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

It seems that my code and tests is incomplete because PQM fails when
running tests before merging. I have problem with one test so PQM give
me failure on merge request. It's Linux-specific, this test skipped on win32.

======================================================================
FAIL: test_log_BOGUS (bzrlib.tests.blackbox.test_locale.TestLocale)

vvvv[log from bzrlib.tests.blackbox.test_locale.TestLocale.test_log_BOGUS]----

[skip]

Output of bzr ('--no-aliases', '--no-plugins', '-q', 'log', '--log-format=long', 'tree'):

Error for bzr ('--no-aliases', '--no-plugins', '-q', 'log', '--log-format=long', 'tree'):
bzr: warning: unsupported locale setting
  Could not determine what text encoding to use.
  This error usually means your Python interpreter
  doesn't support the locale set by $LANG (BOGUS)
  Continuing with ascii encoding.
Traceback (most recent call last):
  File "/home/pqm/pqm-workdir/home/+trunk/bzr", line 62, in ?
    import bzrlib
  File "/home/pqm/pqm-workdir/home/+trunk/bzrlib/__init__.py", line 26, in ?
    user_encoding = get_user_encoding()
  File "/home/pqm/pqm-workdir/home/+trunk/bzrlib/osutils.py", line 1112, in get_user_encoding
    if user_encoding in (None, 'cp0'):
UnboundLocalError: local variable 'user_encoding' referenced before assignment

^^^^[log from bzrlib.tests.blackbox.test_locale.TestLocale.test_log_BOGUS]----
- ----------------------------------------------------------------------

So I add another test to check this code path. And fix problem.
Here the diff from previous (reviewed patch). Complete patch is attached.

================================================================================
=== modified file 'bzrlib/osutils.py'
- --- bzrlib/osutils.py	2006-12-26 15:29:50 +0000
+++ bzrlib/osutils.py	2007-01-02 16:01:32 +0000
@@ -1105,6 +1105,7 @@
                          '  doesn\'t support the locale set by $LANG (%s)\n'
                          "  Continuing with ascii encoding.\n"
                          % (e, os.environ.get('LANG')))
+        user_encoding = 'ascii'

     # Windows returns 'cp0' to indicate there is no code page. So we'll just
     # treat that as ASCII, and not support printing unicode characters to the

=== modified file 'bzrlib/tests/test_osutils_encodings.py'
- --- bzrlib/tests/test_osutils_encodings.py	2007-01-02 14:32:36 +0000
+++ bzrlib/tests/test_osutils_encodings.py	2007-01-02 15:52:14 +0000
@@ -18,6 +18,7 @@

 import codecs
 import locale   # XXX: Do I need a woodoo for MacOS?
+import os
 import sys

 import bzrlib
@@ -154,10 +155,18 @@
         self._getpreferredencoding = locale.getpreferredencoding
         self.addCleanup(self._reset)
         sys.stderr = StringIOWrapper()
+        # save $LANG
+        self._LANG = os.environ.get('LANG')

     def _reset(self):
         locale.getpreferredencoding = self._getpreferredencoding
         sys.stderr = self._stderr
+        # restore $LANG
+        if self._LANG is not None:
+            os.environ['LANG'] = self._LANG
+        else:
+            if os.environ.get('LANG') is not None:
+                del os.environ['LANG']

     def test_get_user_encoding(self):
         def f():
@@ -185,3 +194,17 @@
         self.assertEquals('bzr: warning: unknown encoding cpUNKNOWN.'
                           ' Continuing with ascii encoding.\n',
                           sys.stderr.getvalue())
+
+    def test_user_locale_error(self):
+        def f():
+            raise locale.Error, 'unsupported locale'
+
+        locale.getpreferredencoding = f
+        os.environ['LANG'] = 'BOGUS'
+        self.assertEquals('ascii', osutils.get_user_encoding(use_cache=False))
+        self.assertEquals('bzr: warning: unsupported locale\n'
+                          '  Could not determine what text encoding to use.\n'
+                          '  This error usually means your Python interpreter\n'
+                          '  doesn\'t support the locale set by $LANG (BOGUS)\n'
+                          '  Continuing with ascii encoding.\n',
+                          sys.stderr.getvalue())

=================================================================================


Alexander

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFmoQUzYr338mxwCURAlX2AKCGn1jD/a8sd5q4hbqSUtEvknKvfwCfVpEq
xLODNS9f6Y+AuoITyJrgfrE=
=4p0Q
-----END PGP SIGNATURE-----
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: check-encoding.v3.patch
Url: https://lists.ubuntu.com/archives/bazaar/attachments/20070102/d947a463/attachment.diff 


More information about the bazaar mailing list