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

John Arbash Meinel john at arbash-meinel.com
Tue Jan 2 17:32:06 GMT 2007


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

Alexander Belchenko wrote:
> 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.


v- You can use "osutils.set_or_unset_env('LANG', self._LANG)
Which handles all of this if/else logic.

>      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']
> 

...


+
+
+class FakeCodec(object):
+    """Special singleton class that helps testing
+    over several non-existed encodings.
+
+    Clients could add new encoding names, but cannot remove it.
+    """
+    _registered = False
+    _enabled_encodings = set()
+
+    def add(self, encoding_name):
+        if not self._registered:
+            codecs.register(self)
+            self._registered = True
+        self._enabled_encodings.add(encoding_name)
+
+    def __call__(self, encoding_name):
+        """Called indirectly by codecs module during lookup"""
+        if encoding_name in self._enabled_encodings:
+            return codecs.lookup('latin-1')
+
+
+fake_codec = FakeCodec()


^- I think it is important that clients can remove the registered
encoding, to leave things in a pristine state. It is fine to leave
'FakeCodec' registered, because it is just a wrapper. But you should
remove entries from FakeCode._enabled_encodings(). That way we don't
have transference of state between tests.

Also, we avoid using mutable class-level variables if we access them
over self. because unless you understand python well, it can be
confusing what is happening. So it is better to use:

def __init__(self):
  self._registered = False
  self._enabled_encodings = set()

def reset(self):
  self._enabled_encodings = set()

And then in the test cleanup, add fake_codec.reset()


Actually, if you wanted, you could change FakeCodec.add() so that it can
take a TestCase. Something like:

def add(self, encoding_name, test_case=None):
  ...
  if test_case is not None:
    test_case.addCleanup(self.reset)

It means reset() may be called multiple times, but it helps prevent us
from forgetting to clean up after ourselves.

Otherwise +1, I'll mark it so in BB.

John
=:->


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

iD8DBQFFmpcWJdeBCYSNAAMRAhR5AKC69nCjQo4Yv3YTtXOCeKehCaSdMACeMOdp
6TloekDjk4umHV2LghkOctI=
=gydq
-----END PGP SIGNATURE-----




More information about the bazaar mailing list