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

Commit

Permalink
update guard for ContainerVariable (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
SigureMo authored Jun 20, 2023
1 parent aba95c5 commit d0ede5e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions sot/opcode_translator/executor/variables/container.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from __future__ import annotations

import operator
from functools import reduce
from typing import TYPE_CHECKING, Any

from ....utils import log_do
from ....utils.exceptions import InnerError, NotImplementException
from ..guard import StringifyExpression
from ..pycode_generator import PyCodeGen
from ..tracker import (
ConstTracker,
Expand Down Expand Up @@ -38,6 +42,28 @@ def bool(self):
bool(self), self.graph, DummyTracker([self])
)

def make_stringify_guard(self) -> StringifyExpression:
assert (
self.tracker.is_traceable()
), "Cannot make guard from a non-traceable variable."

frame_value_tracer = self.tracker.trace_value_from_frame()
log_do(
4,
lambda: print(
f"[Guard]: guard_fn for {self}, tracker={self.tracker.__class__.__name__}, value={frame_value_tracer.expr}"
),
)
len_guard = StringifyExpression(
f"len({frame_value_tracer.expr}) == {len(self)}",
frame_value_tracer.free_vars,
)
return reduce(
operator.and_,
[len_guard]
+ [item.make_stringify_guard() for item in self.get_items()],
)


class ListVariable(ContainerVariable):
def __init__(
Expand Down

0 comments on commit d0ede5e

Please sign in to comment.