Skip to content

Allow "raise Exception()" #250

@antocuni

Description

@antocuni

This works:

raise Exception
raise Exception("hello")

This doesn't:

raise Exception()

We should probably allow it and make it equivalent to raise Exception("").
We can do it by checking number of arguments in W_Exception.w_NEW:

spy/spy/vm/exc.py

Lines 54 to 72 in ead6d95

@builtin_method('__new__', color='blue', kind='metafunc')
@staticmethod
def w_NEW(vm: 'SPyVM', wam_cls: W_MetaArg, *args_wam: W_MetaArg) -> W_OpSpec:
# we cannot use the default __new__ because we want to pass w_cls
w_cls = wam_cls.w_blueval
assert isinstance(w_cls, W_Type)
fqn = w_cls.fqn
T = Annotated[W_Exception, w_cls]
# the whole "raise Exception(...)" is a bit of a hack at the moment:
# the C backend can raise only BLUE exceptions, so here we make sure
# that Exception("...") is blue
@vm.register_builtin_func(fqn, '__new__', color='blue')
def w_new(vm: 'SPyVM', w_cls: W_Type, w_message: W_Str) -> T:
pyclass = w_cls.pyclass
assert issubclass(pyclass, W_Exception)
message = vm.unwrap_str(w_message)
return pyclass(message)
return W_OpSpec(w_new, [wam_cls] + list(args_wam))

Originally posted by @dpdani in #172 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions