Author Vlastimil.Zíma
Recipients Vlastimil.Zíma, michael.foord, peter.otten, r.david.murray
Date 2014-02-28.21:45:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1393623924.72.0.877740917683.issue20804@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2014-02-28 21:45:24Vlastimil.Zímasetrecipients: + Vlastimil.Zíma, peter.otten, r.david.murray, michael.foord
2014-02-28 21:45:24Vlastimil.Zímasetmessageid: <1393623924.72.0.877740917683.issue20804@psf.upfronthosting.co.za>
2014-02-28 21:45:24Vlastimil.Zímalinkissue20804 messages
2014-02-28 21:45:23Vlastimil.Zímacreate