Skip to content

Commit

Permalink
Merge pull request #140 from duncf/master
Browse files Browse the repository at this point in the history
Fix to_python() when strings are indented by stripping whitespace before evaluating.
  • Loading branch information
Psycojoker authored Jun 13, 2017
2 parents 8fecf13 + fa24569 commit 9082c8c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion redbaron/base_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def get_holder_on_attribute(class_, node):
class LiteralyEvaluable(object):
def to_python(self):
try:
return ast.literal_eval(self.dumps())
return ast.literal_eval(self.dumps().strip())
except ValueError as e:
message = 'to_python method only works on numbers, strings, list, tuple, dict, boolean and None. (using ast.literal_eval). The piece of code that you are trying to convert contains an illegale value, for example, a variable.'
e.message = message
Expand Down
5 changes: 5 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ def test_to_python_name_node_None():
assert red[0].to_python() == None


def test_to_python_with_spacing():
red = RedBaron("{ 'pouet': d}")
assert red.find("string").to_python() == 'pouet'


def test_to_python_name_node_otherwise_raise():
red = RedBaron("foo")
try:
Expand Down

0 comments on commit 9082c8c

Please sign in to comment.