mirror of
https://github.com/ansible/ansible.git
synced 2026-07-30 16:14:40 +02:00
isidentifier: Remove Python 2 specific code (#83688)
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
---
|
||||
minor_changes:
|
||||
- isidentifier - remove unwanted Python 2 specific code.
|
||||
+16
-25
@@ -32,8 +32,6 @@ from ansible.module_utils.common.text.converters import to_native, to_text
|
||||
from ansible.parsing.splitter import parse_kv
|
||||
|
||||
|
||||
ADDITIONAL_PY2_KEYWORDS = frozenset(("True", "False", "None"))
|
||||
|
||||
_MAXSIZE = 2 ** 32
|
||||
cur_id = 0
|
||||
node_mac = ("%012x" % uuid.getnode())[:12]
|
||||
@@ -237,7 +235,22 @@ def load_options_vars(version):
|
||||
return load_options_vars.options_vars
|
||||
|
||||
|
||||
def _isidentifier_PY3(ident):
|
||||
def isidentifier(ident):
|
||||
"""Determine if string is valid identifier.
|
||||
|
||||
The purpose of this function is to be used to validate any variables created in
|
||||
a play to be valid Python identifiers and to not conflict with Python keywords
|
||||
to prevent unexpected behavior. Since Python 2 and Python 3 differ in what
|
||||
a valid identifier is, this function unifies the validation so playbooks are
|
||||
portable between the two. The following changes were made:
|
||||
|
||||
* disallow non-ascii characters (Python 3 allows for them as opposed to Python 2)
|
||||
|
||||
:arg ident: A text string of identifier to check. Note: It is callers
|
||||
responsibility to convert ident to text if it is not already.
|
||||
|
||||
Originally posted at https://stackoverflow.com/a/29586366
|
||||
"""
|
||||
if not isinstance(ident, string_types):
|
||||
return False
|
||||
|
||||
@@ -251,25 +264,3 @@ def _isidentifier_PY3(ident):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
isidentifier = _isidentifier_PY3
|
||||
|
||||
|
||||
isidentifier.__doc__ = """Determine if string is valid identifier.
|
||||
|
||||
The purpose of this function is to be used to validate any variables created in
|
||||
a play to be valid Python identifiers and to not conflict with Python keywords
|
||||
to prevent unexpected behavior. Since Python 2 and Python 3 differ in what
|
||||
a valid identifier is, this function unifies the validation so playbooks are
|
||||
portable between the two. The following changes were made:
|
||||
|
||||
* disallow non-ascii characters (Python 3 allows for them as opposed to Python 2)
|
||||
* True, False and None are reserved keywords (these are reserved keywords
|
||||
on Python 3 as opposed to Python 2)
|
||||
|
||||
:arg ident: A text string of identifier to check. Note: It is callers
|
||||
responsibility to convert ident to text if it is not already.
|
||||
|
||||
Originally posted at http://stackoverflow.com/a/29586366
|
||||
"""
|
||||
|
||||
@@ -24,21 +24,13 @@ def test_valid_identifier(identifier):
|
||||
@pytest.mark.parametrize(
|
||||
"identifier", [
|
||||
"pass", "foo ", " foo", "1234", "1234abc", "", " ", "foo bar", "no-dashed-names-for-you",
|
||||
"True", "False", "None"
|
||||
]
|
||||
)
|
||||
def test_invalid_identifier(identifier):
|
||||
assert not isidentifier(identifier)
|
||||
|
||||
|
||||
def test_keywords_not_in_PY2():
|
||||
"""In Python 2 ("True", "False", "None") are not keywords. The isidentifier
|
||||
method ensures that those are treated as keywords on both Python 2 and 3.
|
||||
"""
|
||||
assert not isidentifier("True")
|
||||
assert not isidentifier("False")
|
||||
assert not isidentifier("None")
|
||||
|
||||
|
||||
def test_non_ascii():
|
||||
"""In Python 3 non-ascii characters are allowed as opposed to Python 2. The
|
||||
isidentifier method ensures that those are treated as keywords on both
|
||||
|
||||
Reference in New Issue
Block a user