Add facility to deepcopy bound methods in Py2.6 and apply to grains

In #28587, we failed to account for the inability of Py2.6 to deepcopy bound methods. This provides a workaround for that.
This commit is contained in:
Mike Place 2015-11-05 10:41:20 -07:00
parent 464aa6b062
commit f519661875
2 changed files with 43 additions and 2 deletions

View file

@ -15,6 +15,7 @@ import collections
from functools import reduce
# Import 3rd-party libs
import salt.utils.copy
from salt.utils.odict import OrderedDict
import yaml
import salt.ext.six as six
@ -238,8 +239,15 @@ def setvals(grains, destructive=False):
grains[key] = val
__grains__[key] = val
# Cast defaultdict to dict; is there a more central place to put this?
yaml_reps = copy.deepcopy(yaml.representer.SafeRepresenter.yaml_representers)
yaml_multi_reps = copy.deepcopy(yaml.representer.SafeRepresenter.yaml_multi_representers)
try:
yaml_reps = copy.deepcopy(yaml.representer.SafeRepresenter.yaml_representers)
yaml_multi_reps = copy.deepcopy(yaml.representer.SafeRepresenter.yaml_multi_representers)
except (TypeError, NameError):
# This likely means we are running under Python 2.6 which cannot deepcopy
# bound methods. Fallback to a modification of deepcopy which can support
# this behavoir.
yaml_reps = salt.utils.copy.deepcopy_bound(yaml.representer.SafeRepresenter.yaml_representers)
yaml_multi_reps = salt.utils.copy.deepcopy_bound(yaml.representer.SafeRepresenter.yaml_multi_representers)
yaml.representer.SafeRepresenter.add_representer(collections.defaultdict,
yaml.representer.SafeRepresenter.represent_dict)
yaml.representer.SafeRepresenter.add_representer(OrderedDict,

33
salt/utils/copy.py Normal file
View file

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
'''
Compatibility functions for copying
'''
# Import python libs
from __future__ import absolute_import
import copy
import types
def deepcopy_bound(name):
'''
Compatibility helper function to allow copy.deepcopy copy bound methods
which is broken on Python 2.6, due to the following bug:
https://bugs.python.org/issue1515
Warnings:
- This method will mutate the global deepcopy dispatcher, which means that
this function is NOT threadsafe!
- Not Py3 compatable. The intended use case is deepcopy compat for Py2.6
'''
def _deepcopy_method(x, memo):
return type(x)(x.im_func, copy.deepcopy(x.im_self, memo), x.im_class) # pylint: disable=W1699
try:
pre_dispatch = copy._deepcopy_dispatch
copy._deepcopy_dispatch[types.MethodType] = _deepcopy_method
ret = copy.deepcopy(name)
finally:
copy._deepcopy_dispatch = pre_dispatch
return ret