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
4cdd9b2a
Commit
4cdd9b2a
authored
Sep 11, 2015
by
James Cammarata
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #12318 from mgedmin/py3k
Python 3: make ansible.template.safe_eval() work
parents
52f587ca
f91b28ef
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
3 deletions
+16
-3
lib/ansible/template/safe_eval.py
+12
-3
test/units/template/test_safe_eval.py
+4
-0
No files found.
lib/ansible/template/safe_eval.py
View file @
4cdd9b2a
...
@@ -20,6 +20,7 @@ __metaclass__ = type
...
@@ -20,6 +20,7 @@ __metaclass__ = type
import
ast
import
ast
import
sys
import
sys
from
six
import
string_types
from
six.moves
import
builtins
from
six.moves
import
builtins
from
ansible
import
constants
as
C
from
ansible
import
constants
as
C
...
@@ -66,13 +67,21 @@ def safe_eval(expr, locals={}, include_exceptions=False):
...
@@ -66,13 +67,21 @@ def safe_eval(expr, locals={}, include_exceptions=False):
)
)
# AST node types were expanded after 2.6
# AST node types were expanded after 2.6
if
not
sys
.
version
.
startswith
(
'2.6'
):
if
sys
.
version_info
[:
2
]
>=
(
2
,
7
):
SAFE_NODES
.
u
nion
(
SAFE_NODES
.
u
pdate
(
set
(
set
(
(
ast
.
Set
,)
(
ast
.
Set
,)
)
)
)
)
# And in Python 3.4 too
if
sys
.
version_info
[:
2
]
>=
(
3
,
4
):
SAFE_NODES
.
update
(
set
(
(
ast
.
NameConstant
,)
)
)
filter_list
=
[]
filter_list
=
[]
for
filter
in
filter_loader
.
all
():
for
filter
in
filter_loader
.
all
():
filter_list
.
extend
(
filter
.
filters
()
.
keys
())
filter_list
.
extend
(
filter
.
filters
()
.
keys
())
...
@@ -96,7 +105,7 @@ def safe_eval(expr, locals={}, include_exceptions=False):
...
@@ -96,7 +105,7 @@ def safe_eval(expr, locals={}, include_exceptions=False):
for
child_node
in
ast
.
iter_child_nodes
(
node
):
for
child_node
in
ast
.
iter_child_nodes
(
node
):
self
.
generic_visit
(
child_node
,
inside_call
)
self
.
generic_visit
(
child_node
,
inside_call
)
if
not
isinstance
(
expr
,
basestring
):
if
not
isinstance
(
expr
,
string_types
):
# already templated to a datastructure, perhaps?
# already templated to a datastructure, perhaps?
if
include_exceptions
:
if
include_exceptions
:
return
(
expr
,
None
)
return
(
expr
,
None
)
...
...
test/units/template/test_safe_eval.py
View file @
4cdd9b2a
...
@@ -19,6 +19,7 @@
...
@@ -19,6 +19,7 @@
from
__future__
import
(
absolute_import
,
division
,
print_function
)
from
__future__
import
(
absolute_import
,
division
,
print_function
)
__metaclass__
=
type
__metaclass__
=
type
import
sys
from
collections
import
defaultdict
from
collections
import
defaultdict
from
ansible.compat.tests
import
unittest
from
ansible.compat.tests
import
unittest
...
@@ -45,3 +46,6 @@ class TestSafeEval(unittest.TestCase):
...
@@ -45,3 +46,6 @@ class TestSafeEval(unittest.TestCase):
self
.
assertEqual
(
safe_eval
(
'[]'
,
locals
=
locals_vars
),
[])
self
.
assertEqual
(
safe_eval
(
'[]'
,
locals
=
locals_vars
),
[])
self
.
assertEqual
(
safe_eval
(
'{}'
,
locals
=
locals_vars
),
{})
self
.
assertEqual
(
safe_eval
(
'{}'
,
locals
=
locals_vars
),
{})
@unittest.skipUnless
(
sys
.
version_info
[:
2
]
>=
(
2
,
7
),
"Python 2.6 has no set literals"
)
def
test_set_literals
(
self
):
self
.
assertEqual
(
safe_eval
(
'{0}'
),
set
([
0
]))
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