From 9b0d122c71d49687b45bb2a0e97a45bd0a4132b4 Mon Sep 17 00:00:00 2001 From: Zsolt Dollenstein Date: Tue, 18 Feb 2025 21:12:37 +0000 Subject: [PATCH 1/2] eliminate use of __file__ in boto3.session --- boto3/session.py | 7 ++++--- tests/functional/test_session.py | 9 +++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/boto3/session.py b/boto3/session.py index 19b6e5be71..4613e1bc07 100644 --- a/boto3/session.py +++ b/boto3/session.py @@ -12,6 +12,7 @@ # language governing permissions and limitations under the License. import copy +from importlib.resources import path import os import botocore.session @@ -127,9 +128,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 From 44a775c07225a445f789aaf47fd1cea4cbae59bc Mon Sep 17 00:00:00 2001 From: Zsolt Dollenstein Date: Tue, 18 Feb 2025 21:19:24 +0000 Subject: [PATCH 2/2] fix ruff lints --- boto3/session.py | 1 - 1 file changed, 1 deletion(-) diff --git a/boto3/session.py b/boto3/session.py index 4613e1bc07..e63e88da13 100644 --- a/boto3/session.py +++ b/boto3/session.py @@ -13,7 +13,6 @@ import copy from importlib.resources import path -import os import botocore.session from botocore.client import Config