Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
fallback when function has error handling (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
SigureMo authored Oct 10, 2023
1 parent f6b6318 commit bcfc693
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions sot/opcode_translator/transform.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import dis
import sys
from functools import partial
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -46,6 +47,14 @@ def eval_frame_callback(frame, **kwargs) -> CustomCode:
if frame.f_code.co_flags & 0x20 > 0:
return CustomCode(None, True)

# NOTE(SigureMo): Temporary fallback when code has exception handling.
if sys.version_info >= (3, 11) and frame.f_code.co_exceptiontable:
log(
3,
f"[eval_frame_callback] {frame.f_code} has co_exceptiontable\n",
)
return CustomCode(None, False)

if need_skip(frame):
log(3, f"[eval_frame_callback] skip {frame.f_code}\n")
custom_code = CustomCode(None, False)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_error_handling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import unittest

from test_case_base import TestCaseBase, strict_mode_guard

import sot


def fn_with_try_except():
sot.psdb.breakgraph()
sot.psdb.fallback()
try:
raise ValueError("ValueError")
except ValueError:
print("catch ValueError")
return True


class TestErrorHandling(TestCaseBase):
@strict_mode_guard(0)
def test_fn_with_try_except(self):
self.assert_results(fn_with_try_except)


if __name__ == "__main__":
unittest.main()

0 comments on commit bcfc693

Please sign in to comment.