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

Alexander Belchenko bialix at ukr.net
Tue Jan 2 18:38:55 GMT 2007


John Arbash Meinel пишет:
> 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.
> 
  ...
> +
> +
> +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.

Unfortunately I cannot unregister encoding name from codecs registry. 
Standard codecs module use internal caching of looked-up encodings 
(probably to speed-up operations) and I don't find a way to reset their 
cache.

So, simply remove encoding name from self._enabled_encoding is not 
enough. Because __call__ method for each non-standard encoding invoked 
only 1 time.

To check this fact run attached file.
On my win32 output is:

C:\work\Bazaar\__tests>python fake_codec.py
cp0 is unknown encoding
1
__call__(cp0)
2

C:\work\Bazaar\__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()

In this case fake_codec no more singleton, IIUC.

> 
> def reset(self):
>   self._enabled_encodings = set()
> 
> And then in the test cleanup, add fake_codec.reset()

As I explain above it's not working.

--
Alexander
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: fake_codec.py
Url: https://lists.ubuntu.com/archives/bazaar/attachments/20070102/3629a9b9/attachment.diff 


More information about the bazaar mailing list