From c15f4b5aaa469f658edf15fc6cc375fa8aafddd5 Mon Sep 17 00:00:00 2001 From: Bryan Weber Date: Mon, 30 Oct 2023 10:00:00 -0400 Subject: [PATCH 1/2] Fix an error when incomplete S3Client is destroyed If an error occurs during initialization of the S3Client instance, the initializer for the Client superclass is never called. This could happen, for example, when the S3Client has a bad profile name passed which botocore doesn't understand. When the remainder of the class is garbage collected (for example, at interpreter shutdown), Python calls the __del__ method of the Client superclass. The specific error is that the file_cache_mode attribute is not set because the superclass initializer is not called in the S3Client initializer if an error occurs during S3 session setup. This change uses getattr with a default value to avoid the AttributeError in the __del__ method. --- cloudpathlib/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudpathlib/client.py b/cloudpathlib/client.py index 71ec1b23..831fcd4b 100644 --- a/cloudpathlib/client.py +++ b/cloudpathlib/client.py @@ -85,7 +85,7 @@ def __init__( def __del__(self) -> None: # remove containing dir, even if a more aggressive strategy # removed the actual files - if self.file_cache_mode in [ + if getattr(self, "file_cache_mode", None) in [ FileCacheMode.tmp_dir, FileCacheMode.close_file, FileCacheMode.cloudpath_object, From bd14bcae4bbca9a03dc7361b882643314d294990 Mon Sep 17 00:00:00 2001 From: Bryan Weber Date: Mon, 30 Oct 2023 10:28:13 -0400 Subject: [PATCH 2/2] Update history file --- HISTORY.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 52a73d40..1a6178ec 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,8 +1,11 @@ # cloudpathlib Changelog +## Unreleased +- Fix `S3Client` cleanup via `Client.__del__` when `S3Client` encounters an exception during initialization. (Issue [#372](https://github.com/drivendataorg/cloudpathlib/issues/372), PR [#373](https://github.com/drivendataorg/cloudpathlib/pull/373)) + ## v0.16.0 (2023-10-09) - Add "CloudPath" as return type on `__init__` for mypy issues. ([Issue #179](https://github.com/drivendataorg/cloudpathlib/issues/179), [PR #342](https://github.com/drivendataorg/cloudpathlib/pull/342)) - - Add `with_stem` to all path types when python version supports it (>=3.9). ([Issue #287](https://github.com/drivendataorg/cloudpathlib/issues/287), [PR #290](https://github.com/drivendataorg/cloudpathlib/pull/290), thanks to [@Gilthans](https://github.com/Gilthans)) + - Add `with_stem` to all path types when python version supports it (>=3.9). ([Issue #287](https://github.com/drivendataorg/cloudpathlib/issues/287), [PR #290](https://github.com/drivendataorg/cloudpathlib/pull/290), thanks to [@Gilthans](https://github.com/Gilthans)) - Add `newline` parameter to the `write_text` method to align to `pathlib` functionality as of Python 3.10. [PR #362](https://github.com/drivendataorg/cloudpathlib/pull/362), thanks to [@pricemg](https://github.com/pricemg). - Add support for Python 3.12 ([PR #364](https://github.com/drivendataorg/cloudpathlib/pull/364)) - Add `CLOUDPATHLIB_LOCAL_CACHE_DIR` env var for setting local_cache_dir default for clients ([Issue #352](https://github.com/drivendataorg/cloudpathlib/issues/352), [PR #357](https://github.com/drivendataorg/cloudpathlib/pull/357))