| 1 |
from pypy.module.exceptions.interp_exceptions import W_BaseException, W_Exception, _new_exception |
| 2 |
from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault, applevel |
| 3 |
from pypy.interpreter.typedef import ( |
| 4 |
TypeDef, GetSetProperty, interp_attrproperty_w, |
| 5 |
descr_get_dict, descr_set_dict, descr_del_dict) |
| 6 |
from pypy.objspace.std.util import generic_alias_class_getitem |
| 7 |
|
| 8 |
class W_BaseExceptionGroup(W_BaseException): |
| 9 |
"""A combination of unrelated exceptions.""" |
| 10 |
|
| 11 |
def __init__(self, space): |
| 12 |
W_BaseException.__init__(self, space) |
| 13 |
|
| 14 |
@staticmethod |
| 15 |
def descr_new(space, w_subtype, w_message, w_exceptions): |
| 16 |
w_subtype, w_exceptions = space.unpackiterable(check_new_args(space, w_subtype, w_message, w_exceptions), 2) |
| 17 |
exc = space.allocate_instance(W_BaseExceptionGroup, w_subtype) |
| 18 |
W_BaseExceptionGroup.__init__(exc, space) |
| 19 |
exc.w_message = w_message |
| 20 |
exc.w_exceptions = w_exceptions |
| 21 |
exc.args_w = [w_message, w_exceptions] |
| 22 |
return exc |
| 23 |
|
| 24 |
def descr_init(self, space, args_w): |
| 25 |
if len(args_w) > 0: |
| 26 |
self.w_value = args_w[0] |
| 27 |
W_BaseException.descr_init(self, space, args_w) |
| 28 |
|
| 29 |
def descr_str(self, space): |
| 30 |
return app_str(space, self) |
| 31 |
|
| 32 |
def descr_repr(self, space): |
| 33 |
return app_repr(space, self) |
| 34 |
|
| 35 |
def subgroup(self, space, w_condition): |
| 36 |
""" |
| 37 |
Returns an exception group that contains only the exceptions from the |
| 38 |
current group that match condition, or None if the result is empty. |
| 39 |
""" |
| 40 |
return subgroup(space, self, w_condition) |
| 41 |
|
| 42 |
def split(self, space, w_condition): |
| 43 |
""" |
| 44 |
Like subgroup(), but returns the pair (match, rest) where match is |
| 45 |
subgroup(condition) and rest is the remaining non-matching part. |
| 46 |
""" |
| 47 |
return split(space, self, w_condition) |
| 48 |
|
| 49 |
|
| 50 |
def derive(self, space, w_excs): |
| 51 |
""" |
| 52 |
Returns an exception group that contains only the exceptions from the |
| 53 |
current group that match condition, or None if the result is empty. |
| 54 |
""" |
| 55 |
return space.call_function(space.w_BaseExceptionGroup, self.w_message, w_excs) |
| 56 |
|
| 57 |
W_BaseExceptionGroup.typedef = TypeDef( |
| 58 |
'BaseExceptionGroup', |
| 59 |
W_BaseException.typedef, |
| 60 |
__doc__ = W_BaseExceptionGroup.__doc__, |
| 61 |
__module__ = 'builtins', |
| 62 |
__new__ = interp2app(W_BaseExceptionGroup.descr_new), |
| 63 |
__init__ = interp2app(W_BaseExceptionGroup.descr_init), |
| 64 |
__str__ = interp2app(W_BaseExceptionGroup.descr_str), |
| 65 |
__repr__ = interp2app(W_BaseExceptionGroup.descr_repr), |
| 66 |
__class_getitem__ = interp2app( |
| 67 |
generic_alias_class_getitem, as_classmethod=True), |
| 68 |
subgroup = interp2app(W_BaseExceptionGroup.subgroup), |
| 69 |
split = interp2app(W_BaseExceptionGroup.split), |
| 70 |
derive = interp2app(W_BaseExceptionGroup.derive), |
| 71 |
message = interp_attrproperty_w('w_message', W_BaseExceptionGroup), |
| 72 |
exceptions = interp_attrproperty_w('w_exceptions', W_BaseExceptionGroup), |
| 73 |
) |
| 74 |
|
| 75 |
class W_ExceptionGroup(W_BaseExceptionGroup): |
| 76 |
pass |
| 77 |
|
| 78 |
W_ExceptionGroup.typedef = TypeDef( |
| 79 |
'ExceptionGroup', |
| 80 |
(W_BaseExceptionGroup.typedef, W_Exception.typedef), |
| 81 |
__module__ = 'builtins', |
| 82 |
) |
| 83 |
W_ExceptionGroup.typedef.applevel_subclasses_base = W_BaseExceptionGroup |
| 84 |
|
| 85 |
def prep_reraise_star(space, w_obj, w_exc_list): |
| 86 |
return _prep_reraise_star(space, w_obj, w_exc_list) |
| 87 |
|
| 88 |
def collect_eg_leafs(space, w_exc, w_result_set): |
| 89 |
return _collect_eg_leafs(space, w_exc, w_result_set) |
| 90 |
|
| 91 |
def exception_group_projection(space, w_eg, w_keep_list): |
| 92 |
return _exception_group_projection(space, w_eg, w_keep_list) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
import os |
| 98 |
appfile = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'app_group.py') |
| 99 |
|
| 100 |
with open(appfile, "r") as f: |
| 101 |
content = f.read() |
| 102 |
|
| 103 |
app = applevel(content, filename=__file__) |
| 104 |
check_new_args = app.interphook('check_new_args') |
| 105 |
app_str = app.interphook('__str__') |
| 106 |
app_repr = app.interphook('__repr__') |
| 107 |
subgroup = app.interphook('subgroup') |
| 108 |
split = app.interphook('split') |
| 109 |
_prep_reraise_star = app.interphook('_prep_reraise_star') |
| 110 |
_collect_eg_leafs = app.interphook('_collect_eg_leafs') |
| 111 |
_exception_group_projection = app.interphook('_exception_group_projection') |