Skip to content

Commit

Permalink
Increased coverage slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaxson committed Jan 26, 2018
1 parent 2c49795 commit 80ee092
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion miniutils/pragma/core/resolve.py
Expand Up @@ -250,7 +250,7 @@ def _resolve_literal(node, ctxt):
elif isinstance(node, ast.Index):
return _resolve_literal(node.value, ctxt)
elif isinstance(node, (ast.Slice, ast.ExtSlice)):
raise NotImplemented()
raise NotImplementedError()
elif isinstance(node, ast.Subscript):
# print("Attempting to subscript {}".format(astor.to_source(node)))
lst = constant_iterable(node.value, ctxt)
Expand Down
1 change: 1 addition & 0 deletions miniutils/pragma/deindex.py
Expand Up @@ -23,6 +23,7 @@ def deindex(iterable, iterable_name, *args, **kwargs):
if hasattr(iterable, 'items'): # Support dicts and the like
internal_iterable = {k: '{}_{}'.format(iterable_name, k) for k, val in iterable.items()}
mapping = {internal_iterable[k]: val for k, val in iterable.items()}
raise NotImplementedError('Dictionary indices are not yet supported')
else: # Support lists, tuples, and the like
internal_iterable = {i: '{}_{}'.format(iterable_name, i) for i, val in enumerate(iterable)}
mapping = {internal_iterable[i]: val for i, val in enumerate(iterable)}
Expand Down
8 changes: 2 additions & 6 deletions miniutils/pragma/inline.py
Expand Up @@ -69,10 +69,6 @@ def make_name(fname, var, n, ctx=ast.Load):
ctx=ctx())


def make_return(fname, n, ctx):
return ast.Name(id=DICT_FMT.format(fname=fname, n=n), ctx=ctx())


class _InlineBodyTransformer(TrackedContextTransformer):
def __init__(self, func_name, param_names, n):
self.func_name = func_name
Expand Down Expand Up @@ -380,8 +376,8 @@ def visit_AsyncWith(self, node):

def visit_Try(self, node):
node.body = self.nested_visit(node.body)
node.body = self.nested_visit(node.orelse)
node.body = self.nested_visit(node.finalbody)
node.orelse = self.nested_visit(node.orelse)
node.finalbody = self.nested_visit(node.finalbody)
return self.generic_visit_less(node, 'body', 'orelse', 'finalbody')

def visit_Module(self, node):
Expand Down
38 changes: 24 additions & 14 deletions tests/test_pragma.py
Expand Up @@ -402,15 +402,15 @@ def test_invalid_collapse(self):
def f():
return 1 + "2"

self.assertTrue(issubclass(w[-1].category, UserWarning))
self.assertIsInstance(w[-1].category(), UserWarning)

warnings.resetwarnings()
with warnings.catch_warnings(record=True) as w:
@pragma.collapse_literals
def f():
return -"5"

self.assertTrue(issubclass(w[-1].category, UserWarning))
self.assertIsInstance(w[-1].category(), UserWarning)

# TODO: implement the features to get this test to work
# def test_conditional_erasure(self):
Expand Down Expand Up @@ -560,6 +560,22 @@ def f(x):
''')
self.assertEqual(f.strip(), result.strip())

# Not yet supported
def test_dict(self):
d = {'a': 1, 'b': 2}

def f(x):
yield d['a']
yield d[x]

self.assertRaises(NotImplementedError, pragma.deindex, d, 'd')
# result = dedent('''
# def f(x):
# yield v_a
# yield v[x]
# ''')
# self.assertEqual(f.strip(), result.strip())

def test_dynamic_function_calls(self):
funcs = [lambda x: x, lambda x: x ** 2, lambda x: x ** 3]

Expand Down Expand Up @@ -844,20 +860,14 @@ def g(y):
while False:
print(y)

@pragma.inline(g, return_source=True)
def f():
g(5)
try:
g(5)
except:
raise

result = dedent('''
def f():
_g_0 = dict(y=5)
for ____ in [None]:
while False:
print(_g_0['y'])
del _g_0
None
''')
self.assertEqual(f.strip(), result.strip())
print(pragma.inline(g, return_source=True)(f))
self.assertEqual(f(), pragma.inline(g)(f)())


class TestDictStack(PragmaTest):
Expand Down

0 comments on commit 80ee092

Please sign in to comment.