Skip to content

Commit

Permalink
mypyc: save the ops as well
Browse files Browse the repository at this point in the history
  • Loading branch information
msullivan committed Aug 13, 2018
1 parent 32ace39 commit 5b654ac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
11 changes: 9 additions & 2 deletions mypyc/emitmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from mypyc.emitwrapper import (
generate_wrapper_function, wrapper_function_header,
)
from mypyc.ops import FuncIR, ClassIR, ModuleIR
from mypyc.ops import FuncIR, ClassIR, ModuleIR, format_func
from mypyc.refcount import insert_ref_count_opcodes
from mypyc.exceptions import insert_exception_handling
from mypyc.emit import EmitterContext, Emitter, HeaderDeclaration
Expand All @@ -31,7 +31,8 @@ def __init__(self, declaration: HeaderDeclaration, mark: bool) -> None:

def compile_modules_to_c(sources: List[BuildSource], module_names: List[str], options: Options,
use_shared_lib: bool,
alt_lib_path: Optional[str] = None) -> str:
alt_lib_path: Optional[str] = None,
ops: Optional[List[str]] = None) -> str:
"""Compile Python module(s) to C that can be used from Python C extension modules."""
assert options.strict_optional, 'strict_optional must be turned on'
result = build(sources=sources,
Expand All @@ -51,6 +52,12 @@ def compile_modules_to_c(sources: List[BuildSource], module_names: List[str], op
for _, module in modules:
for fn in module.functions:
insert_ref_count_opcodes(fn)
# Format ops for debugging
if ops is not None:
for _, module in modules:
for fn in module.functions:
ops.extend(format_func(fn))
ops.append('')
# Generate C code.
source_paths = {module_name: result.files[module_name].path
for module_name in module_names}
Expand Down
16 changes: 13 additions & 3 deletions scripts/mypyc
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,34 @@ def main() -> None:
# around with making the single module code handle packages.)
use_shared_lib = len(module_names) > 1 or any('.' in x for x in module_names)

# XXX: This is not the right way to pass in command line args
save_dir = os.getenv("MYPYC_DIR", None)

ops = [] if save_dir else None

try:
ctext = emitmodule.compile_modules_to_c(
sources=sources,
module_names=module_names,
options=options,
use_shared_lib=use_shared_lib)
use_shared_lib=use_shared_lib,
ops=ops)
except CompileError as e:
for line in e.messages:
print(line)
sys.exit(1)

# XXX: This is not the right way to pass in command line args
save_dir = os.getenv("MYPYC_DIR", None)
print("C generated!")

if save_dir:
try:
os.mkdir(save_dir)
except FileExistsError:
pass
if ops:
with open(os.path.join(save_dir, 'ops.txt'), 'w') as f:
f.write('\n'.join(ops))

dir = os.path.abspath(save_dir)
else:
dir = tempfile.mkdtemp(prefix='mypyc-')
Expand Down

0 comments on commit 5b654ac

Please sign in to comment.