| 1 |
|
| 2 |
""" test proxy internals like code, traceback, frame |
| 3 |
""" |
| 4 |
import py |
| 5 |
|
| 6 |
class AppProxy(object): |
| 7 |
spaceconfig = {"objspace.std.withtproxy": True} |
| 8 |
|
| 9 |
def setup_class(cls): |
| 10 |
pass |
| 11 |
|
| 12 |
def setup_method(self, meth): |
| 13 |
self.w_get_proxy = self.space.appexec([], """(): |
| 14 |
class Controller(object): |
| 15 |
def __init__(self, obj): |
| 16 |
self.obj = obj |
| 17 |
|
| 18 |
def perform(self, name, *args, **kwargs): |
| 19 |
return getattr(self.obj, name)(*args, **kwargs) |
| 20 |
def get_proxy(f): |
| 21 |
from __pypy__ import tproxy as proxy |
| 22 |
return proxy(type(f), Controller(f).perform) |
| 23 |
return get_proxy |
| 24 |
""") |
| 25 |
|
| 26 |
class AppTestProxyInterpOnly(AppProxy): |
| 27 |
def setup_class(cls): |
| 28 |
if cls.runappdirect: |
| 29 |
py.test.skip("interp only test") |
| 30 |
from pypy.interpreter.typedef import TypeDef, interp2app |
| 31 |
from pypy.interpreter.baseobjspace import W_Root |
| 32 |
|
| 33 |
class W_Stuff(W_Root): |
| 34 |
pass |
| 35 |
|
| 36 |
def descr_new(space, w_subtype): |
| 37 |
return W_Stuff() |
| 38 |
|
| 39 |
W_Stuff.typedef = TypeDef( |
| 40 |
'Stuff', |
| 41 |
__new__ = interp2app(descr_new), |
| 42 |
) |
| 43 |
cls.w_Stuff = cls.space.gettypefor(W_Stuff) |
| 44 |
|
| 45 |
def test_unproxyable(self): |
| 46 |
raises(TypeError, self.get_proxy, self.Stuff()) |
| 47 |
|
| 48 |
class AppTestProxyInternals(AppProxy): |
| 49 |
def test_traceback_basic(self): |
| 50 |
try: |
| 51 |
1/0 |
| 52 |
except: |
| 53 |
import sys |
| 54 |
e = sys.exc_info() |
| 55 |
|
| 56 |
tb = self.get_proxy(e[2]) |
| 57 |
assert tb.tb_frame is e[2].tb_frame |
| 58 |
|
| 59 |
def test_traceback_catch(self): |
| 60 |
try: |
| 61 |
try: |
| 62 |
1/0 |
| 63 |
except ZeroDivisionError as e: |
| 64 |
ex = self.get_proxy(e) |
| 65 |
raise ex |
| 66 |
except ZeroDivisionError: |
| 67 |
pass |
| 68 |
else: |
| 69 |
raise AssertionError("Did not raise") |
| 70 |
|
| 71 |
def test_traceback_reraise(self): |
| 72 |
|
| 73 |
try: |
| 74 |
1/0 |
| 75 |
except: |
| 76 |
import sys |
| 77 |
e = sys.exc_info() |
| 78 |
|
| 79 |
tb = self.get_proxy(e[2]) |
| 80 |
raises(ZeroDivisionError, "raise e[0](e[1]).with_traceback(tb)") |
| 81 |
raises(ZeroDivisionError, "raise e[0](self.get_proxy(e[1])).with_traceback(tb)") |
| 82 |
import traceback |
| 83 |
assert len(traceback.format_tb(tb)) == 1 |
| 84 |
|
| 85 |
def test_simple_frame(self): |
| 86 |
import sys |
| 87 |
frame = sys._getframe(0) |
| 88 |
fp = self.get_proxy(frame) |
| 89 |
assert fp.f_locals == frame.f_locals |
| 90 |
|
| 91 |
class AppTestProxyTracebackController(AppProxy): |
| 92 |
def test_controller(self): |
| 93 |
import types |
| 94 |
import sys |
| 95 |
import traceback |
| 96 |
|
| 97 |
def get_proxy(f): |
| 98 |
from __pypy__ import tproxy as proxy |
| 99 |
return proxy(type(f), Controller(f).perform) |
| 100 |
|
| 101 |
class FakeTb(object): |
| 102 |
def __init__(self, tb): |
| 103 |
self.tb_lasti = tb.tb_lasti |
| 104 |
self.tb_lineno = tb.tb_lineno |
| 105 |
if tb.tb_next: |
| 106 |
self.tb_next = FakeTb(tb.tb_next) |
| 107 |
else: |
| 108 |
self.tb_next = None |
| 109 |
self.tb_frame = get_proxy(tb.tb_frame) |
| 110 |
|
| 111 |
class Controller(object): |
| 112 |
def __init__(self, tb): |
| 113 |
if isinstance(tb, types.TracebackType): |
| 114 |
self.obj = FakeTb(tb) |
| 115 |
else: |
| 116 |
self.obj = tb |
| 117 |
|
| 118 |
def perform(self, name, *args, **kwargs): |
| 119 |
return getattr(self.obj, name)(*args, **kwargs) |
| 120 |
|
| 121 |
def f(): |
| 122 |
1/0 |
| 123 |
|
| 124 |
def g(): |
| 125 |
f() |
| 126 |
|
| 127 |
try: |
| 128 |
g() |
| 129 |
except: |
| 130 |
e = sys.exc_info() |
| 131 |
|
| 132 |
last_tb = e[2] |
| 133 |
tb = get_proxy(e[2]) |
| 134 |
try: |
| 135 |
raise e[0](e[1]).with_traceback(tb) |
| 136 |
except: |
| 137 |
e = sys.exc_info() |
| 138 |
|
| 139 |
assert traceback.format_tb(last_tb) == traceback.format_tb(e[2])[1:] |
| 140 |
|
| 141 |
def test_proxy_get(self): |
| 142 |
from __pypy__ import tproxy, get_tproxy_controller |
| 143 |
|
| 144 |
class A(object): |
| 145 |
pass |
| 146 |
|
| 147 |
def f(name, *args, **kwargs): |
| 148 |
pass |
| 149 |
lst = tproxy(A, f) |
| 150 |
assert get_tproxy_controller(lst) is f |