From c516f607b616b2be606e418f17483afc1812782e Mon Sep 17 00:00:00 2001 From: Elmer de Looff Date: Wed, 3 Jun 2020 12:24:41 -0400 Subject: Folds two identical exception handlers into a single one Fixes a `TODO` that searches for py2/3 compatible syntax to match multiple exception types. ### Description Merges the two exception clauses using the syntax that exists for both Python 2 and 3 as per the exception handling tutorials ([Python 2](https://docs.python.org/2/tutorial/errors.html#handling-exceptions), [Python 3](https://docs.python.org/3/tutorial/errors.html#handling-exceptions)) ### Checklist This pull request is: - [ ] A documentation / typographical error fix - Good to go, no issue or tests are needed - [x] A short code fix - please include the issue number, and create an issue if none exists, which must include a complete example of the issue. one line code fixes without an issue and demonstration will not be accepted. - Please include: `Fixes: #` in the commit message - please include tests. one line code fixes without tests will not be accepted. - [ ] A new feature implementation - please include the issue number, and create an issue if none exists, which must include a complete example of how the feature would look. - Please include: `Fixes: #` in the commit message - please include tests. **Have a nice day!** Closes: #5325 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5325 Pull-request-sha: d0835511e5509fb368a2193ebe4ecbbe3504b129 Change-Id: If3a8854e4de023e0ec68ae8649d24af9412f0717 --- lib/sqlalchemy/orm/base.py | 6 +----- lib/sqlalchemy/orm/exc.py | 4 +--- 2 files changed, 2 insertions(+), 8 deletions(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/orm/base.py b/lib/sqlalchemy/orm/base.py index 77a85425e..54e45cd1a 100644 --- a/lib/sqlalchemy/orm/base.py +++ b/lib/sqlalchemy/orm/base.py @@ -319,11 +319,7 @@ def object_state(instance): def _inspect_mapped_object(instance): try: return instance_state(instance) - # TODO: whats the py-2/3 syntax to catch two - # different kinds of exceptions at once ? - except exc.UnmappedClassError: - return None - except exc.NO_STATE: + except (exc.UnmappedClassError,) + exc.NO_STATE: return None diff --git a/lib/sqlalchemy/orm/exc.py b/lib/sqlalchemy/orm/exc.py index 7b0f84866..19f8ca3bc 100644 --- a/lib/sqlalchemy/orm/exc.py +++ b/lib/sqlalchemy/orm/exc.py @@ -196,9 +196,7 @@ def _default_unmapped(cls): try: mappers = base.manager_of_class(cls).mappers - except NO_STATE: - mappers = {} - except TypeError: + except (TypeError,) + NO_STATE: mappers = {} name = _safe_cls_name(cls) -- cgit v1.2.1