Vlastimil Zíma added the comment:

I encountered this problem when I tested code with cache, something like

    def test_foo(self):
        cache.set('a_key', sentinel.status) # Performs pickle
        self.assertEqual(
            cache.get('a_key'), # Performs unpickle
            sentinel.status)

I spent some time searching for reason why the test failed. Since both 
sentinels represent themselves as 'sentinel.status', I was quite surprised by 
assertion failure. Only after I looked at the code I realized where is the 
problem. Two sentinels are equal if and only if they are identical.

On my way home I realized this probably is and needs to be a feature of 
sentinels. Consider testing this function:

    def foo(value, do_copy):
        if do_copy:
            return bar(copy(value))
        else:
            return bar(value)

You would like to know whether `bar` was called with value or its copy. 
Sentinel is in a test for such function a great option to make sure `value` is 
not copied in process. This is especially usefull is you write tests for 
wrapper-like function which should only pass arguments (or a subset) to other 
interface.


After reading through comments and thinking about the problem I have following 
opinions:
 * Sentinels should not actually survive pickle/unpickle. 
 * Prevent sentinels from being pickled is a good idea, it is a valid way to 
tell developer he does something he shouldn't. This might also apply to 
copying, which would result in the same problem.
 * It should be noted in documentation that sentinels are equal only if 
identical. Since they represent themselves by their name it is hard to guess 
the name is only for the representation. Also I find the documentation snippet

>>> assert result is sentinel.some_object

somewhat misleading as you don't usually use 'is' for comparison and it 
provides no information about equality.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue20804>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to