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

fallback when function has error handling #412

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()