Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
9ca8c326
Commit
9ca8c326
authored
Apr 08, 2014
by
James Cammarata
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Switch module safe_eval to ast.literal_eval
parent
b73cddc4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
2 deletions
+40
-2
lib/ansible/module_utils/basic.py
+40
-2
No files found.
lib/ansible/module_utils/basic.py
View file @
9ca8c326
...
...
@@ -101,6 +101,44 @@ except ImportError:
import
syslog
has_journal
=
False
try
:
from
ast
import
literal_eval
as
_literal_eval
except
ImportError
:
# a replacement for literal_eval that works with python 2.4. from:
# https://mail.python.org/pipermail/python-list/2009-September/551880.html
# which is essentially a cut/past from an earlier (2.6) version of python's
# ast.py
from
compiler
import
parse
from
compiler.ast
import
*
def
_literal_eval
(
node_or_string
):
"""
Safely evaluate an expression node or a string containing a Python
expression. The string or node provided may only consist of the following
Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
and None.
"""
_safe_names
=
{
'None'
:
None
,
'True'
:
True
,
'False'
:
False
}
if
isinstance
(
node_or_string
,
basestring
):
node_or_string
=
parse
(
node_or_string
,
mode
=
'eval'
)
if
isinstance
(
node_or_string
,
Expression
):
node_or_string
=
node_or_string
.
node
def
_convert
(
node
):
if
isinstance
(
node
,
Const
)
and
isinstance
(
node
.
value
,
(
basestring
,
int
,
float
,
long
,
complex
)):
return
node
.
value
elif
isinstance
(
node
,
Tuple
):
return
tuple
(
map
(
_convert
,
node
.
nodes
))
elif
isinstance
(
node
,
List
):
return
list
(
map
(
_convert
,
node
.
nodes
))
elif
isinstance
(
node
,
Dict
):
return
dict
((
_convert
(
k
),
_convert
(
v
))
for
k
,
v
in
node
.
items
)
elif
isinstance
(
node
,
Name
):
if
node
.
name
in
_safe_names
:
return
_safe_names
[
node
.
name
]
elif
isinstance
(
node
,
UnarySub
):
return
-
_convert
(
node
.
expr
)
raise
ValueError
(
'malformed string'
)
return
_convert
(
node_or_string
)
FILE_COMMON_ARGUMENTS
=
dict
(
src
=
dict
(),
mode
=
dict
(),
...
...
@@ -655,9 +693,9 @@ class AnsibleModule(object):
try
:
result
=
None
if
not
locals
:
result
=
eval
(
str
)
result
=
_literal_
eval
(
str
)
else
:
result
=
eval
(
str
,
None
,
locals
)
result
=
_literal_
eval
(
str
,
None
,
locals
)
if
include_exceptions
:
return
(
result
,
None
)
else
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment