mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-03 23:14:02 -07:00
Migrate basestring to a python3 compatible type (#17199)
This commit is contained in:
parent
a695e18615
commit
a22909c226
28 changed files with 137 additions and 101 deletions
|
@ -20,8 +20,6 @@ from __future__ import (absolute_import, division, print_function)
|
|||
__metaclass__ = type
|
||||
|
||||
import os
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.utils.unicode import to_bytes
|
||||
|
||||
# Note, sha1 is the only hash algorithm compatible with python2.4 and with
|
||||
# FIPS-140 mode (as of 11-2014)
|
||||
|
@ -40,12 +38,16 @@ except ImportError:
|
|||
# Assume we're running in FIPS mode here
|
||||
_md5 = None
|
||||
|
||||
from ansible.compat.six import string_types
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.utils.unicode import to_bytes
|
||||
|
||||
def secure_hash_s(data, hash_func=sha1):
|
||||
''' Return a secure hash hex digest of data. '''
|
||||
|
||||
digest = hash_func()
|
||||
try:
|
||||
if not isinstance(data, basestring):
|
||||
if not isinstance(data, string_types):
|
||||
data = "%s" % data
|
||||
digest.update(data)
|
||||
except UnicodeEncodeError:
|
||||
|
|
|
@ -24,10 +24,12 @@ __metaclass__ = type
|
|||
import os
|
||||
import sys
|
||||
import ast
|
||||
from ansible.parsing.yaml.loader import AnsibleLoader
|
||||
import traceback
|
||||
|
||||
from collections import MutableMapping, MutableSet, MutableSequence
|
||||
|
||||
from ansible.compat.six import string_types
|
||||
from ansible.parsing.yaml.loader import AnsibleLoader
|
||||
from ansible.plugins import fragment_loader
|
||||
|
||||
try:
|
||||
|
@ -76,7 +78,7 @@ def get_docstring(filename, verbose=False):
|
|||
doc = AnsibleLoader(child.value.s, file_name=filename).get_single_data()
|
||||
fragments = doc.get('extends_documentation_fragment', [])
|
||||
|
||||
if isinstance(fragments, basestring):
|
||||
if isinstance(fragments, string_types):
|
||||
fragments = [ fragments ]
|
||||
|
||||
# Allow the module to specify a var other than DOCUMENTATION
|
||||
|
|
|
@ -37,9 +37,6 @@ _LATIN1_ALIASES = frozenset(('latin-1', 'LATIN-1', 'latin1', 'LATIN1',
|
|||
|
||||
# EXCEPTION_CONVERTERS is defined below due to using to_unicode
|
||||
|
||||
if PY3:
|
||||
basestring = (str, bytes)
|
||||
|
||||
def to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None):
|
||||
'''Convert an object into a :class:`unicode` string
|
||||
|
||||
|
@ -93,9 +90,9 @@ def to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None):
|
|||
'''
|
||||
# Could use isbasestring/isunicode here but we want this code to be as
|
||||
# fast as possible
|
||||
if isinstance(obj, basestring):
|
||||
if isinstance(obj, text_type):
|
||||
return obj
|
||||
if isinstance(obj, text_type):
|
||||
return obj
|
||||
if isinstance(obj, binary_type):
|
||||
if encoding in _UTF8_ALIASES:
|
||||
return text_type(obj, 'utf-8', errors)
|
||||
if encoding in _LATIN1_ALIASES:
|
||||
|
@ -202,9 +199,9 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None):
|
|||
'''
|
||||
# Could use isbasestring, isbytestring here but we want this to be as fast
|
||||
# as possible
|
||||
if isinstance(obj, basestring):
|
||||
if isinstance(obj, binary_type):
|
||||
return obj
|
||||
if isinstance(obj, binary_type):
|
||||
return obj
|
||||
if isinstance(obj, text_type):
|
||||
return obj.encode(encoding, errors)
|
||||
if not nonstring:
|
||||
nonstring = 'simplerepr'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue