#NEWLINE# @ contextlib . contextmanager #NEWLINE# def set_key( dictionary , key , value ) : #NEWLINE# #INDENT# key_is_set = ( key in dictionary ) #NEWLINE# original_value = dictionary . pop ( key , None ) #NEWLINE# dictionary [ key ] = value #NEWLINE# try : #NEWLINE# #INDENT# ( yield ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# if key_is_set : #NEWLINE# #INDENT# dictionary [ value ] = original_value #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# del dictionary [ key ]