diff --git a/boto3/session.py b/boto3/session.py index 19b6e5be71..e63e88da13 100644 --- a/boto3/session.py +++ b/boto3/session.py @@ -12,7 +12,7 @@ # language governing permissions and limitations under the License. import copy -import os +from importlib.resources import path import botocore.session from botocore.client import Config @@ -127,9 +127,9 @@ def _setup_loader(self): Setup loader paths so that we can load resources. """ self._loader = self._session.get_component('data_loader') - self._loader.search_paths.append( - os.path.join(os.path.dirname(__file__), 'data') - ) + # NOTE: this context manager is leaked, there's no good place to clean it up + data_path = path(__package__, "data").__enter__() + self._loader.search_paths.append(str(data_path)) def get_available_services(self): """ diff --git a/tests/functional/test_session.py b/tests/functional/test_session.py index 65c28b1743..b0f8d2d057 100644 --- a/tests/functional/test_session.py +++ b/tests/functional/test_session.py @@ -10,6 +10,9 @@ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. +from importlib.resources import path +from unittest.mock import patch + import boto3.session from tests import unittest @@ -45,3 +48,9 @@ def test_get_available_regions(self): regions = self.session.get_available_regions('s3') assert isinstance(regions, list) assert regions + + def test_loader_search_path(self): + with patch.object(boto3.session, '__file__', None): + session = boto3.session.Session() + with path(boto3, "data") as data_path: + assert str(data_path) in session._loader.search_paths