Hi Steve,

Thank you for trying out the implementation. Please do let me know about
bugs you find.

This part of your code looks good:

--- a/Lib/tempfile.py
> +++ b/Lib/tempfile.py
> @@ -819,8 +819,14 @@ def __repr__(self):
>      def __enter__(self):
>          return self.name
>
> -    def __exit__(self, exc, value, tb):
> -        self.cleanup()
> +    def __exit__(self, exc_cls, exc_value, tb):
> +        try:
> +            self.cleanup()
> +        except Exception as clean_exc:
> +            if exc_value is not None:
> +                raise ExceptionGroup('Exception occurred during cleanup',
> [exc_value, clean_exc])
> +            else:
> +                raise
>
>      def cleanup(self):
>          if self._finalizer.detach():
>

def do_some_stuff():
    with tempfile.TemporaryDirectory() as td:
        os.rmdir(td)
        pathlib.Path(td).write_text("Surprise!")
        1/0


Then, use it like this:

if __name__ == '__main__':
    try:
        do_some_stuff()
    except *NotADirectoryError as e:
        print("Fail Site 2", repr(e))
    except *Exception as e:
        print("Fail Site 3", repr(e))
    else:
        print("No error")


Output:
   Fail Site 2 ExceptionGroup('Exception occurred during cleanup',
[NotADirectoryError(20, 'The directory name is invalid')])
   Fail Site 3 ExceptionGroup('Exception occurred during cleanup',
[ZeroDivisionError('division by zero')])

This works whether do_some_stuff raises naked exceptions or
ExceptionGroups, because "except *T"  catches a naked T (and wraps it in an
ExceptionGroup).
So if I comment out the 1/0 I get:

    Fail Site 2 ExceptionGroup('', (NotADirectoryError(20, 'The directory
name is invalid'),))

There is no need for it to always raise ExceptionGroup. It can propagate
the user's exception as is if it has no exceptions to add. But the caller
needs to assume that it may raise an ExceptionGroup, and use except*.

Irit
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/AI7YFJK7KSCVNBAMAKQWMLSIGTT3OJ5X/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to