Skip to content

AttributeError when __del__ is called on S3Client that errors during __init__ #372

@bryanwweber

Description

@bryanwweber

If an exception is raised during the creation of an S3Client instance, subsequently tearing down that instance via Client.__del__ results n an AttributeError:

poetry run python        
Python 3.11.5 (main, Sep 22 2023, 12:39:47) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cloudpathlib
>>> cloudpathlib.S3Client(profile_name="some-bad-profile")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".venv/lib/python3.11/site-packages/cloudpathlib/s3/s3client.py", line 86, in __init__
    self.sess = Session(
                ^^^^^^^^
  File "venv/lib/python3.11/site-packages/boto3/session.py", line 90, in __init__
    self._setup_loader()
  File ".venv/lib/python3.11/site-packages/boto3/session.py", line 131, in _setup_loader
    self._loader = self._session.get_component('data_loader')
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/botocore/session.py", line 802, in get_component
    return self._components.get_component(name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/botocore/session.py", line 1140, in get_component
    self._components[name] = factory()
                             ^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/botocore/session.py", line 199, in <lambda>
    lambda: create_loader(self.get_config_variable('data_path')),
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.11/site-packages/botocore/session.py", line 323, in get_config_variable
    return self.get_component('config_store').get_config_variable(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/botocore/configprovider.py", line 459, in get_config_v
ariable
    return provider.provide()
           ^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/botocore/configprovider.py", line 665, in provide
    value = provider.provide()
            ^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/botocore/configprovider.py", line 755, in provide
    scoped_config = self._session.get_scoped_config()
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/botocore/session.py", line 422, in get_scoped_config
    raise ProfileNotFound(profile=profile_name)
botocore.exceptions.ProfileNotFound: The config profile (some-bad-profile) could not be found
>>> quit()
Exception ignored in: <function Client.__del__ at 0x1031bcc20>
Traceback (most recent call last):
  File ".venv/lib/python3.11/site-packages/cloudpathlib/client.py", line 88, in __del__
    if self.file_cache_mode in [
       ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'S3Client' object has no attribute 'file_cache_mode'

This is happening because the super().__init__() call happens at the bottom of the S3Client.__init__() method (here), so Client.file_cache_mode is not set before the error occurs. I checked and there doesn't seem to be a particular reason that super().__init__() needs to be called at the bottom (all the args are just pass through from the S3Client.__init__() signature). I'm happy to submit a PR to fix this by moving the super().__init__() up in S3Client.__init__() and other related classes too, if that seems like the right fix. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions