| 1 |
from rpython.flowspace.model import * |
| 2 |
from rpython.rlib.unroll import SpecTag |
| 3 |
from rpython.flowspace.flowcontext import FlowContext |
| 4 |
from rpython.flowspace.bytecode import HostCode |
| 5 |
from rpython.flowspace.pygraph import PyGraph |
| 6 |
|
| 7 |
class TestFrameState: |
| 8 |
def get_context(self, func): |
| 9 |
try: |
| 10 |
func = func.im_func |
| 11 |
except AttributeError: |
| 12 |
pass |
| 13 |
code = HostCode._from_code(func.__code__) |
| 14 |
graph = PyGraph(func, code) |
| 15 |
ctx = FlowContext(graph, code) |
| 16 |
|
| 17 |
ctx.setstate(graph.startblock.framestate) |
| 18 |
ctx.locals_w = ctx.locals_w[:] |
| 19 |
ctx.locals_w[-1] = Constant(None) |
| 20 |
return ctx |
| 21 |
|
| 22 |
def func_simple(x): |
| 23 |
spam = 5 |
| 24 |
return spam |
| 25 |
|
| 26 |
def test_eq_framestate(self): |
| 27 |
ctx = self.get_context(self.func_simple) |
| 28 |
fs1 = ctx.getstate(0) |
| 29 |
fs2 = ctx.getstate(0) |
| 30 |
assert fs1.matches(fs2) |
| 31 |
|
| 32 |
def test_neq_hacked_framestate(self): |
| 33 |
ctx = self.get_context(self.func_simple) |
| 34 |
fs1 = ctx.getstate(0) |
| 35 |
ctx.locals_w = ctx.locals_w[:] |
| 36 |
ctx.locals_w[-1] = Variable() |
| 37 |
fs2 = ctx.getstate(0) |
| 38 |
assert not fs1.matches(fs2) |
| 39 |
|
| 40 |
def test_union_on_equal_framestates(self): |
| 41 |
ctx = self.get_context(self.func_simple) |
| 42 |
fs1 = ctx.getstate(0) |
| 43 |
fs2 = ctx.getstate(0) |
| 44 |
assert fs1.union(fs2).matches(fs1) |
| 45 |
|
| 46 |
def test_union_on_hacked_framestates(self): |
| 47 |
ctx = self.get_context(self.func_simple) |
| 48 |
fs1 = ctx.getstate(0) |
| 49 |
ctx.locals_w = ctx.locals_w[:] |
| 50 |
ctx.locals_w[-1] = Variable() |
| 51 |
fs2 = ctx.getstate(0) |
| 52 |
assert fs1.union(fs2).matches(fs2) |
| 53 |
assert fs2.union(fs1).matches(fs2) |
| 54 |
|
| 55 |
def test_restore_frame(self): |
| 56 |
ctx = self.get_context(self.func_simple) |
| 57 |
fs1 = ctx.getstate(0) |
| 58 |
ctx.locals_w = ctx.locals_w[:] |
| 59 |
ctx.locals_w[-1] = Variable() |
| 60 |
ctx.setstate(fs1) |
| 61 |
assert fs1.matches(ctx.getstate(0)) |
| 62 |
|
| 63 |
def test_copy(self): |
| 64 |
ctx = self.get_context(self.func_simple) |
| 65 |
fs1 = ctx.getstate(0) |
| 66 |
fs2 = fs1.copy() |
| 67 |
assert fs1.matches(fs2) |
| 68 |
|
| 69 |
def test_getvariables(self): |
| 70 |
ctx = self.get_context(self.func_simple) |
| 71 |
fs1 = ctx.getstate(0) |
| 72 |
vars = fs1.getvariables() |
| 73 |
assert len(vars) == 1 |
| 74 |
|
| 75 |
def test_getoutputargs(self): |
| 76 |
ctx = self.get_context(self.func_simple) |
| 77 |
fs1 = ctx.getstate(0) |
| 78 |
ctx.locals_w = ctx.locals_w[:] |
| 79 |
ctx.locals_w[-1] = Variable() |
| 80 |
fs2 = ctx.getstate(0) |
| 81 |
outputargs = fs1.getoutputargs(fs2) |
| 82 |
|
| 83 |
|
| 84 |
assert outputargs == [ctx.locals_w[0], Constant(None)] |
| 85 |
|
| 86 |
def test_union_different_constants(self): |
| 87 |
ctx = self.get_context(self.func_simple) |
| 88 |
fs1 = ctx.getstate(0) |
| 89 |
ctx.locals_w = ctx.locals_w[:] |
| 90 |
ctx.locals_w[-1] = Constant(42) |
| 91 |
fs2 = ctx.getstate(0) |
| 92 |
fs3 = fs1.union(fs2) |
| 93 |
ctx.setstate(fs3) |
| 94 |
assert isinstance(ctx.locals_w[-1], Variable) |
| 95 |
|
| 96 |
def test_union_spectag(self): |
| 97 |
ctx = self.get_context(self.func_simple) |
| 98 |
fs1 = ctx.getstate(0) |
| 99 |
ctx.locals_w = ctx.locals_w[:] |
| 100 |
ctx.locals_w[-1] = Constant(SpecTag()) |
| 101 |
fs2 = ctx.getstate(0) |
| 102 |
assert fs1.union(fs2) is None |