Skip to content

Commit

Permalink
cleanup and illustrate tests I should write
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinHock committed Aug 29, 2017
1 parent 8826bbd commit c3ecb8e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 43 deletions.
13 changes: 13 additions & 0 deletions example/example_inputs/try_orelse_with_no_variables_to_save.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def does_this_kill_us(diff):
return subprocess.call(diff, shell=True)

# @app.route('/poc', methods=['POST'])
# def poc():
try:
print('A5')
except ImportError:
print('Wagyu')
else:
does_this_kill_us("hard-coded string")
print('So')
print('Good')
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def does_this_kill_us():
return subprocess.call('ls', shell=True)

# @app.route('/poc', methods=['POST'])
# def poc():
try:
print('A5')
except ImportError:
print('Wagyu')
else:
does_this_kill_us()
print('So')
print('Good')
46 changes: 14 additions & 32 deletions pyt/base_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,54 +292,36 @@ def stmt_star_handler(self, stmts, use_prev_node=True):
self.use_prev_node.append(use_prev_node)
first_node = None
for stmt in stmts:
logger.debug("[pay attention] stmt is %s", stmt)
node = self.visit(stmt)

if isinstance(node, ControlFlowNode):
break_nodes.extend(node.break_statements)
elif isinstance(node, BreakNode):
break_nodes.append(node)

logger.debug("[pay attention] node is %s", node)

# this if should maybe be in the `if self.node_to_connect(node) and node:`
if node and not first_node:
# note: multiple ingoing nodes kills this!! fix that
if node and not first_node: # (Make sure first_node isn't already set.)
# first_node is always a "node_to_connect", because it won't have ingoing otherwise
# If we have e.g.
# import os # An ignored node
# value = None
# first_node will be `value = None`
if hasattr(node, 'ingoing'):
logger.debug("[pay attention] node.ingoing is %s", node.ingoing)
ingoing = None
loop_node = node
logger.debug("[pa] loop_node.ingoing was %s", loop_node.ingoing)
while loop_node.ingoing:
logger.debug("IMPORTANT loop_node.ingoing[0] is %s", loop_node.ingoing[0])
# Is it an Entry node? Let's not backwards traverse any more.
if loop_node.ingoing[0].label.startswith('Entry module'):
current_node = node
while current_node.ingoing:
# Is it an Entry to a module? Let's not backwards traverse any more.
# Entries to functions are fine
if current_node.ingoing[0].label.startswith('Entry module'):
break
# if isinstance(loop_node.ingoing[0], EntryOrExitNode):
# logger.debug("[Kaffe1668] So instead of %s ingoing is %s", loop_node.ingoing[0], ingoing)
# break
ingoing = loop_node.ingoing
loop_node = loop_node.ingoing[0]
logger.debug("ingoing list is %s", ingoing)
ingoing = current_node.ingoing
current_node = current_node.ingoing[0]
if ingoing:
logger.debug("[pa] ingoing[0] is now %s", ingoing[0])
logger.debug("[pa] type(ingoing[0]) is now %s", type(ingoing[0]))
# Only set it once from the first stmt
logger.debug("making first_node be %s", ingoing[0])
# Only set it once
first_node = ingoing[0]

# cfg_statements.append(ingoing[0])

if self.node_to_connect(node) and node:
if not first_node:
logger.debug("shit, should first_node be %s", node)
logger.debug("shit, should type(first_node) be %s", type(node))
if isinstance(node, ControlFlowNode):
logger.debug("dir(node) is %s", dir(node))
logger.debug("node.test is %s", node.test)
logger.debug("type(node.test) is %s", type(node.test))
first_node = node.test
# raise
else:
first_node = node
cfg_statements.append(node)
Expand Down
8 changes: 8 additions & 0 deletions pyt/interprocedural_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ def save_local_scope(self, line_number, original_previous_node):
previous_node.connect(saved_scope_node)
logger.debug("[Flux]Connecting")
else:
logger.debug("original previous node is %s", original_previous_node)
logger.debug("[Flux]Not connecting")
# try_orelse hits here.
# raise

return saved_variables

Expand All @@ -279,6 +282,8 @@ def save_actual_parameters_in_temp(self, args, arguments, line_number, original_
logger.debug("[2Flux]Connecting")
else:
logger.debug("[2Flux]Not connecting")
# example/example_inputs/try_orelse_with_no_variables_to_save.py
# raise

self.nodes.append(restore_node)

Expand Down Expand Up @@ -394,6 +399,9 @@ def get_function_nodes(self, definition, original_previous_node):
logger.debug("[3Flux]Connecting")
else:
logger.debug("[3Flux]Not connecting")
logger.debug("[3Flux]original_previous_node is %s", original_previous_node)
# example/example_inputs/try_orelse_with_no_variables_to_save_and_no_args.py
# raise
function_body_connect_statements = self.stmt_star_handler(definition.node.body)

entry_node.connect(function_body_connect_statements.first_statement)
Expand Down
12 changes: 1 addition & 11 deletions tests/cfg_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,6 @@ def test_orelse(self):
print_good = 16
_exit = 17

# OUTDATED COMMENT:
# With our added stmt_star_handler loop:
# We have save_node->print_so instead of return_handler->print_so
# If we comment out our stmt_star_handler loop that we added:
# print_a5 is connected to return_handler
# save_node has no predecessors
# ... but at least we have return_handler->print_so


self.assertInCfg([self.connected(entry, try_),

self.connected(try_, try_body),
Expand All @@ -226,14 +217,13 @@ def test_orelse(self):
self.connected(print_wagyu, print_good),

self.connected(save_node, assign_to_temp),
# self.connected(save_node, print_so), # THIS SHOULD NOT BE!
self.connected(assign_to_temp, assign_from_temp),
self.connected(assign_from_temp, function_entry),
self.connected(function_entry, ret_of_subprocess_call),
self.connected(ret_of_subprocess_call, function_exit),
self.connected(function_exit, restore_node),
self.connected(restore_node, return_handler),
self.connected(return_handler, print_so), # THIS SHOULD BE
self.connected(return_handler, print_so),

self.connected(print_so, print_good),
self.connected(print_good, _exit)])
Expand Down

0 comments on commit c3ecb8e

Please sign in to comment.