-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from ndif-team/hacks2.0
Hacks2.0
- Loading branch information
Showing
9 changed files
with
286 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import ast | ||
from types import FrameType | ||
|
||
from ..graph import Graph | ||
from .conditional import handle as handle_conditional | ||
from .iterator import handle as handle_iterator | ||
|
||
|
||
def handle_inner(node:ast.stmt, frame: FrameType, graph: Graph): | ||
|
||
if isinstance(node, ast.If): | ||
|
||
handle_conditional(node, frame, graph) | ||
|
||
return True | ||
|
||
elif isinstance(node, ast.For): | ||
|
||
handle_iterator(node, frame, graph) | ||
|
||
return True | ||
|
||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import ast | ||
import ctypes | ||
import inspect | ||
import sys | ||
from types import FrameType | ||
from typing import TYPE_CHECKING | ||
|
||
from ..contexts import Iterator | ||
from ..graph import Graph | ||
from .util import execute, execute_body, execute_until, visit | ||
|
||
if TYPE_CHECKING: | ||
from ..graph import Proxy | ||
|
||
COMPS = [ast.SetComp, ast.DictComp, ast.ListComp, ast.GeneratorExp] | ||
|
||
def handle(node: ast.For, frame: FrameType, graph: Graph): | ||
|
||
iter_expr = ast.Expression( | ||
body=node.iter, lineno=node.lineno, col_offset=node.col_offset | ||
) | ||
|
||
iter = execute(iter_expr, frame) | ||
|
||
context = Iterator(iter, parent=graph) | ||
|
||
target = node.target | ||
|
||
with context as item: | ||
if isinstance(target, ast.Name): | ||
frame.f_locals[target.id] = item | ||
elif isinstance(target, ast.Tuple): | ||
for t, v in zip(target.elts, item): | ||
if isinstance(t, ast.Name): | ||
frame.f_locals[t.id] = v | ||
|
||
ctypes.pythonapi.PyFrame_LocalsToFast(ctypes.py_object(frame), 0) | ||
|
||
execute_body(node.body, frame, context.graph) | ||
|
||
|
||
def handle_proxy(node:ast.stmt, frame: FrameType, collection:"Proxy"): | ||
|
||
graph = collection.node.graph | ||
|
||
|
||
iterator = Iterator(collection, parent=graph) | ||
|
||
item = iterator.__enter__() | ||
|
||
def callback(new_frame:FrameType, list_proxy, iterator:Iterator): | ||
|
||
|
||
key, result = next(iter(new_frame.f_locals.items())) | ||
print(node, node.elt.elt.ctx.__dict__) | ||
|
||
# list_proxy.append(result[0]) | ||
|
||
# new_frame.f_locals[key] = list_proxy | ||
# ctypes.pythonapi.PyFrame_LocalsToFast(ctypes.py_object(new_frame), 0) | ||
|
||
iterator.__exit__(None, None, None) | ||
|
||
|
||
execute_until(frame.f_lineno -1, frame.f_lineno - 1, frame, callback= lambda new_frame: callback(new_frame, [], iterator)) | ||
|
||
return iter([item]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.