Move to compat module to avoid namespace collisions in salt.utils

This commit is contained in:
Mike Place 2015-11-05 11:41:03 -07:00
parent f519661875
commit 2435b45195
3 changed files with 29 additions and 36 deletions

View file

@ -15,7 +15,7 @@ import collections
from functools import reduce
# Import 3rd-party libs
import salt.utils.copy
import salt.utils.compat
from salt.utils.odict import OrderedDict
import yaml
import salt.ext.six as six
@ -246,8 +246,8 @@ def setvals(grains, destructive=False):
# 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_reps = salt.utils.compat.deepcopy_bound(yaml.representer.SafeRepresenter.yaml_representers)
yaml_multi_reps = salt.utils.compat.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,

View file

@ -6,6 +6,8 @@ Compatibility functions for utils
# Import python libs
from __future__ import absolute_import
import sys
import copy
import types
# Import salt libs
import salt.loader
@ -20,3 +22,27 @@ def pack_dunder(name):
mod = sys.modules[name]
if not hasattr(mod, '__utils__'):
setattr(mod, '__utils__', salt.loader.utils(mod.__opts__))
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

View file

@ -1,33 +0,0 @@
# -*- 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