Skip to content

Commit

Permalink
Migrate tests for attribute access in function defaults.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan-hof committed Jan 19, 2017
1 parent 986eca8 commit c3f5017
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/RestrictedPython/tests/before_and_after.py
Expand Up @@ -40,14 +40,6 @@ def no_unpack_before():
no_unpack_after = no_unpack_before # that is, should be untouched
def lambda_with_getattr_in_defaults_before():
f = lambda x=y.z: x


def lambda_with_getattr_in_defaults_after():
f = lambda x=_getattr_(y, "z"): x


# augmented operators
# Note that we don't have to worry about item, attr, or slice assignment,
# as they are disallowed. Yay!
Expand All @@ -57,3 +49,4 @@ def lambda_with_getattr_in_defaults_after():
#
# def inplace_id_add_after():
# x = _inplacevar_('+=', x, y+z)
"""
37 changes: 37 additions & 0 deletions tests/test_transformer.py
Expand Up @@ -212,6 +212,43 @@ def test_transformer__RestrictingNodeTransformer__visit_Attribute__6(compile):
'name because it starts with "_".'


TRANSFORM_ATTRIBUTE_ACCESS_FUNCTION_DEFAULT = """
def func_default(x=a.a):
return x
lambda_default = lambda x=b.b: x
"""


@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Attribute__7(compile, mocker):
code, errors = compile(TRANSFORM_ATTRIBUTE_ACCESS_FUNCTION_DEFAULT)[:2]
assert code is not None
assert errors == ()

_getattr_ = mocker.Mock()
_getattr_.side_effect = getattr

glb = {
'_getattr_': _getattr_,
'a': mocker.Mock(a=1),
'b': mocker.Mock(b=2)
}

six.exec_(code, glb)

_getattr_.assert_has_calls([
mocker.call(glb['a'], 'a'),
mocker.call(glb['b'], 'b')
])

ret = glb['func_default']()
assert ret == 1

ret = glb['lambda_default']()
assert ret == 2


@pytest.mark.skipif(sys.version_info < (3, 0),
reason="exec is a statement in Python 2")
@pytest.mark.parametrize(*compile)
Expand Down

0 comments on commit c3f5017

Please sign in to comment.