Skip to content

raise_ fails to reraise exception which requires arguments #387

@pbasista

Description

@pbasista

When the future.utils.raise_ function is used with all its three parameters, it is in some cases not able to reraise exceptions which need arguments for their initialization. For example, this code:

import sys

from future.utils import raise_

class CustomException(Exception):
    def __init__(self, severity: int, message: str) -> None:
        super().__init__(f"custom message of severity {severity}: {message}")

try:
    raise CustomException(1, "hello")
except CustomException:
    raise_(*sys.exc_info())

fails with a TypeError:

Traceback (most recent call last):
  File "./future_test.py", line 11, in <module>
    raise CustomException(1, "hello")
__main__.CustomException: custom message of severity 1: hello

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./future_test.py", line 13, in <module>
    raise_(*sys.exc_info())
  File "../python3.6/site-packages/future/utils/__init__.py", line 409, in raise_
    exc = tp(value)
TypeError: __init__() missing 1 required positional argument: 'message'

The problem is caused by the fact that the raise_ function tries to initialize the exception class (i.e. its first argument) using exception value (i.e. its second argument):

That is not always possible for exception types with custom initializers.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions