Skip to content

Commit 289baeb

Browse files
pjbullbryanwweberbweber-rebellion
authored
Move _handle attribute up in CloudPath __init__ (#300) (#301)
* Move _handle attribute up in CloudPath __init__ The _handle attribute needs to be set before any code can raise an exception. This is because if something tries to call __del__ after the exception as the process is exiting, _handle must be set to avoid an AttributeError in __del__. Resolves #299. * Formatting Co-authored-by: Bryan Weber <bweber@rebelliondefense.com> Co-authored-by: Bryan Weber <bryan.w.weber@gmail.com> Co-authored-by: Bryan Weber <bweber@rebelliondefense.com>
1 parent 31f93ac commit 289baeb

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

cloudpathlib/cloudpath.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ class CloudPath(metaclass=CloudPathMeta):
166166
cloud_prefix: str
167167

168168
def __init__(self, cloud_path: Union[str, "CloudPath"], client: Optional["Client"] = None):
169+
170+
# handle if local file gets opened. must be set at the top of the method in case any code
171+
# below raises an exception, this prevents __del__ from raising an AttributeError
172+
self._handle = None
173+
169174
self.is_valid_cloudpath(cloud_path, raise_on_error=True)
170175

171176
# versions of the raw string that provide useful methods
@@ -190,9 +195,6 @@ def __init__(self, cloud_path: Union[str, "CloudPath"], client: Optional["Client
190195
# track if local has been written to, if so it may need to be uploaded
191196
self._dirty = False
192197

193-
# handle if local file gets opened
194-
self._handle = None
195-
196198
def __del__(self):
197199
# make sure that file handle to local path is closed
198200
if self._handle is not None:

0 commit comments

Comments
 (0)