Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
youkaichao committed Aug 28, 2024
1 parent 83e9e84 commit 6ea1309
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test_decompile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
pytest --cov=depyf tests/test.py
coverage run --append python_coverage.py
coverage run --append tests/test_code_owner.py
coverage run --append tests/test_ensure.py
python tests/assert.py
- name: Upload results to Codecov
Expand Down
11 changes: 0 additions & 11 deletions depyf/code_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,6 @@ def visit_FunctionDef(self, node):
# return self.generic_visit(node)


def structure_hash(source_code: str) -> str:
"""Compute the hash of code structure, ignore the function name difference.
This is because PyTorch dynamically generates function names.
"""
tree = ast.parse(source_code)
tree = IdentifierReplacer().visit(tree)
modified_code = astor.to_source(tree)
hash_value = hashlib.md5(modified_code.encode()).hexdigest()
return hash_value


def fix_irregular_code(
old_bytecode: CodeType,
src_code: str,
Expand Down
16 changes: 1 addition & 15 deletions depyf/explain/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@
from dataclasses import dataclass
import contextlib

import depyf
from depyf.decompiler import DecompilationError
from depyf.utils import get_function_signature


def decompile_ensure(fn, overwite_fn_name=None):
try:
decompiled_source_code = depyf.Decompiler(
fn).decompile(overwite_fn_name=overwite_fn_name)
except DecompilationError as e:
header = get_function_signature(fn, overwite_fn_name=overwite_fn_name)
decompiled_source_code = header + " 'Failed to decompile.'\n"
return decompiled_source_code


class CodeProxy:
instances: Dict[str, "CodeProxy"] = {}
used_instances: Set[str] = set()
Expand All @@ -49,6 +34,7 @@ def consume_new_name(name: str):

@staticmethod
def decompile_with_name(code: CodeType, name: str, skip_decompile=False):
from depyf.utils import decompile_ensure
if hasattr(code, "__code__"):
code = code.__code__
if code.co_name.startswith("transformed_code_") or code.co_name.startswith("__transformed_code_"):
Expand Down
13 changes: 13 additions & 0 deletions depyf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,16 @@ def func(self):
else:
break
return fn



def decompile_ensure(fn: CodeType, overwite_fn_name=None):
import depyf
from depyf.decompiler import DecompilationError
try:
decompiled_source_code = depyf.Decompiler(
fn).decompile(overwite_fn_name=overwite_fn_name)
except DecompilationError as e:
header = get_function_signature(fn, overwite_fn_name=overwite_fn_name)
decompiled_source_code = header + " 'Failed to decompile.'\n"
return decompiled_source_code
11 changes: 11 additions & 0 deletions tests/test_ensure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from depyf.utils import decompile_ensure

import asyncio

def f(a, b):
try:
return a + b
finally:
return a - b

print(decompile_ensure(f.__code__))

0 comments on commit 6ea1309

Please sign in to comment.