Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions homeassistant/components/python_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import glob
import os
import logging
import datetime

import voluptuous as vol

Expand Down Expand Up @@ -63,7 +64,8 @@ def execute_script(hass, name, data=None):
def execute(hass, filename, source, data=None):
"""Execute Python source."""
from RestrictedPython import compile_restricted_exec
from RestrictedPython.Guards import safe_builtins, full_write_guard
from RestrictedPython.Guards import safe_builtins, full_write_guard, \
guarded_iter_unpack_sequence, guarded_unpack_sequence
from RestrictedPython.Utilities import utility_builtins
from RestrictedPython.Eval import default_guarded_getitem

Expand Down Expand Up @@ -94,13 +96,16 @@ def protected_getattr(obj, name, default=None):

builtins = safe_builtins.copy()
builtins.update(utility_builtins)
builtins['datetime'] = datetime
restricted_globals = {
'__builtins__': builtins,
'_print_': StubPrinter,
'_getattr_': protected_getattr,
'_write_': full_write_guard,
'_getiter_': iter,
'_getitem_': default_guarded_getitem
'_getitem_': default_guarded_getitem,
'_iter_unpack_sequence_': guarded_iter_unpack_sequence,
'_unpack_sequence_': guarded_unpack_sequence,
}
logger = logging.getLogger('{}.{}'.format(__name__, filename))
local = {
Expand Down
21 changes: 21 additions & 0 deletions tests/components/test_python_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,24 @@ def test_iterating(hass):

assert hass.states.is_state('hello.1', 'world')
assert hass.states.is_state('hello.2', 'world')


@asyncio.coroutine

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines, found 1

def test_unpacking_sequence(hass):
"""Test compile error logs error."""
source = """
a,b = (1,2)
ab_list = [(a,b) for a,b in [(1,2), (3,4)]]
data['a'] = a
data['b'] = b
data['ab_list'] = ab_list
"""

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line contains whitespace

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line contains whitespace

data = {}
hass.async_add_job(execute, hass, 'test.py', source, data)
yield from hass.async_block_till_done()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line contains whitespace

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line contains whitespace

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line contains whitespace

assert data['a'] == 1
assert data['b'] == 2
assert data['ab_list'] == [(1,2),(3,4)]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing whitespace after ','


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line at end of file