@@ -2992,7 +2992,13 @@ def __reduce__(self):
29922992 return str , (REDUCE_A ,)
29932993
29942994class BBB (object ):
2995- pass
2995+ def __init__ (self ):
2996+ # Add an instance attribute to enable state-saving routines at pickling
2997+ # time.
2998+ self .a = "some attribute"
2999+
3000+ def __setstate__ (self , state ):
3001+ self .a = "BBB.__setstate__"
29963002
29973003
29983004def setstate_bbb (obj , state ):
@@ -3004,7 +3010,7 @@ def setstate_bbb(obj, state):
30043010 it as the analogous of list_setitems or dict_setitems but for foreign
30053011 classes/functions.
30063012 """
3007- obj .a = 'foo'
3013+ obj .a = "custom state_setter"
30083014
30093015
30103016class AbstractDispatchTableTests (unittest .TestCase ):
@@ -3099,14 +3105,21 @@ def reduce_2(obj):
30993105 # state_setter is to tweak objects reduction behavior.
31003106 # In particular, state_setter is useful when the default __setstate__
31013107 # behavior is not flexible enough.
3108+
3109+ # No custom reducer for b has been registered for now, so
3110+ # BBB.__setstate__ should be used at unpickling time
3111+ self .assertEqual (default_load_dump (b ).a , "BBB.__setstate__" )
3112+
31023113 def reduce_bbb (obj ):
31033114 return BBB , (), obj .__dict__ , None , None , setstate_bbb
31043115
31053116 dispatch_table [BBB ] = reduce_bbb
31063117
31073118 b = BBB ()
31083119
3109- self .assertEqual (custom_load_dump (b ).a , 'foo' )
3120+ # The custom reducer reduce_bbb includes a state setter, that should
3121+ # have priority over BBB.__setstate__
3122+ self .assertEqual (custom_load_dump (b ).a , "custom state_setter" )
31103123
31113124
31123125if __name__ == "__main__" :
0 commit comments