Skip to content

Commit 751aed4

Browse files
Prefer direct import over importlib
1 parent 7b3e746 commit 751aed4

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

localstack-core/localstack/aws/handlers/logging.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Handlers for logging."""
22

3-
import contextlib
4-
import importlib
53
import logging
64
import types
75
from functools import cached_property
@@ -24,12 +22,13 @@ class ExceptionLogger(ExceptionHandler):
2422
def __init__(self, logger=None):
2523
self.logger = logger or LOG
2624

27-
# Moto may not be available in stripped-down versions of LocalStack, like LocalStack S3 image.
28-
self._moto_service_exception = types.EllipsisType
29-
with contextlib.suppress(ModuleNotFoundError, AttributeError):
30-
self._moto_service_exception = importlib.import_module(
31-
"moto.core.exceptions"
32-
).ServiceException
25+
try:
26+
import moto.core.exceptions
27+
28+
self._moto_service_exception = moto.core.exceptions.ServiceException
29+
except (ModuleNotFoundError, AttributeError):
30+
# Moto may not be available in stripped-down versions of LocalStack, like LocalStack S3 image.
31+
self._moto_service_exception = types.EllipsisType
3332

3433
def __call__(
3534
self,

localstack-core/localstack/aws/handlers/service.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""A set of common handlers to parse and route AWS service requests."""
22

3-
import importlib
43
import logging
54
import traceback
65
import types
@@ -158,14 +157,14 @@ class ServiceExceptionSerializer(ExceptionHandler):
158157
def __init__(self):
159158
self.handle_internal_failures = True
160159

161-
# Moto may not be available in stripped-down versions of LocalStack, like LocalStack S3 image.
162-
self._moto_service_exception = types.EllipsisType
163160
try:
164-
self._moto_service_exception = importlib.import_module(
165-
"moto.core.exceptions"
166-
).ServiceException
161+
import moto.core.exceptions
162+
163+
self._moto_service_exception = moto.core.exceptions.ServiceException
167164
except (ModuleNotFoundError, AttributeError) as exc:
165+
# Moto may not be available in stripped-down versions of LocalStack, like LocalStack S3 image.
168166
LOG.debug("Unable to set up Moto ServiceException translation: %s", exc)
167+
self._moto_service_exception = types.EllipsisType
169168

170169
def __call__(
171170
self,

0 commit comments

Comments
 (0)