| 1 |
from rpython.annotator import model as annmodel |
| 2 |
from rpython.annotator.exception import standardexceptions |
| 3 |
from rpython.rtyper.llannotation import SomePtr |
| 4 |
from rpython.rtyper.rclass import ( |
| 5 |
ll_issubclass, ll_type, ll_cast_to_object, getclassrepr, getinstancerepr) |
| 6 |
|
| 7 |
class UnknownException(Exception): |
| 8 |
pass |
| 9 |
|
| 10 |
|
| 11 |
class ExceptionData(object): |
| 12 |
"""Public information for the code generators to help with exceptions.""" |
| 13 |
|
| 14 |
standardexceptions = standardexceptions |
| 15 |
|
| 16 |
def __init__(self, rtyper): |
| 17 |
|
| 18 |
r_type = rtyper.rootclass_repr |
| 19 |
r_instance = getinstancerepr(rtyper, None) |
| 20 |
r_type.setup() |
| 21 |
r_instance.setup() |
| 22 |
self.r_exception_type = r_type |
| 23 |
self.r_exception_value = r_instance |
| 24 |
self.lltype_of_exception_type = r_type.lowleveltype |
| 25 |
self.lltype_of_exception_value = r_instance.lowleveltype |
| 26 |
self.rtyper = rtyper |
| 27 |
|
| 28 |
def finish(self, rtyper): |
| 29 |
bk = rtyper.annotator.bookkeeper |
| 30 |
for cls in self.standardexceptions: |
| 31 |
classdef = bk.getuniqueclassdef(cls) |
| 32 |
getclassrepr(rtyper, classdef).setup() |
| 33 |
|
| 34 |
def get_standard_ll_exc_instance(self, rtyper, clsdef): |
| 35 |
r_inst = getinstancerepr(rtyper, clsdef) |
| 36 |
example = r_inst.get_reusable_prebuilt_instance() |
| 37 |
example = ll_cast_to_object(example) |
| 38 |
return example |
| 39 |
|
| 40 |
def get_standard_ll_exc_instance_by_class(self, exceptionclass): |
| 41 |
if exceptionclass not in self.standardexceptions: |
| 42 |
raise UnknownException(exceptionclass) |
| 43 |
clsdef = self.rtyper.annotator.bookkeeper.getuniqueclassdef( |
| 44 |
exceptionclass) |
| 45 |
return self.get_standard_ll_exc_instance(self.rtyper, clsdef) |
| 46 |
|
| 47 |
def make_helpers(self, rtyper): |
| 48 |
|
| 49 |
self.fn_exception_match = self.make_exception_matcher(rtyper) |
| 50 |
self.fn_type_of_exc_inst = self.make_type_of_exc_inst(rtyper) |
| 51 |
|
| 52 |
def make_exception_matcher(self, rtyper): |
| 53 |
|
| 54 |
s_typeptr = SomePtr(self.lltype_of_exception_type) |
| 55 |
helper_fn = rtyper.annotate_helper_fn(ll_issubclass, [s_typeptr, s_typeptr]) |
| 56 |
return helper_fn |
| 57 |
|
| 58 |
def make_type_of_exc_inst(self, rtyper): |
| 59 |
|
| 60 |
s_excinst = SomePtr(self.lltype_of_exception_value) |
| 61 |
helper_fn = rtyper.annotate_helper_fn(ll_type, [s_excinst]) |
| 62 |
return helper_fn |
| 63 |
|
| 64 |
def generate_exception_match(self, oplist, var_etype, const_etype): |
| 65 |
|
| 66 |
from rpython.rtyper.rtyper import LowLevelOpList |
| 67 |
from rpython.rtyper.lltypesystem import lltype |
| 68 |
from rpython.flowspace.model import Constant, Variable, SpaceOperation |
| 69 |
llops = LowLevelOpList(None) |
| 70 |
field = llops.genop( |
| 71 |
'getfield', [var_etype, llops.genvoidconst('subclassrange_min')], lltype.Signed) |
| 72 |
res = llops.genop( |
| 73 |
'int_between', [ |
| 74 |
llops.genconst(const_etype.value.subclassrange_min), |
| 75 |
field, |
| 76 |
llops.genconst(const_etype.value.subclassrange_max), |
| 77 |
], |
| 78 |
lltype.Bool) |
| 79 |
oplist.extend(llops) |
| 80 |
return res |