File tree Expand file tree Collapse file tree 1 file changed +14
-6
lines changed Expand file tree Collapse file tree 1 file changed +14
-6
lines changed Original file line number Diff line number Diff line change @@ -97,18 +97,26 @@ def _temp_file(path):
97
97
return _tempfile (path .read_bytes , suffix = path .name )
98
98
99
99
100
+ def _is_present_dir (path : Traversable ) -> bool :
101
+ """
102
+ Some Traversables implement ``is_dir()`` to raise an
103
+ exception (i.e. ``FileNotFoundError``) when the
104
+ directory doesn't exist. This function wraps that call
105
+ to always return a boolean and only return True
106
+ if there's a dir and it exists.
107
+ """
108
+ with contextlib .suppress (FileNotFoundError ):
109
+ return path .is_dir ()
110
+ return False
111
+
112
+
100
113
@functools .singledispatch
101
114
def as_file (path ):
102
115
"""
103
116
Given a Traversable object, return that object as a
104
117
path on the local file system in a context manager.
105
118
"""
106
- try :
107
- is_dir = path .is_dir ()
108
- except FileNotFoundError :
109
- is_dir = False
110
-
111
- return _temp_dir (path ) if is_dir else _temp_file (path )
119
+ return _temp_dir (path ) if _is_present_dir (path ) else _temp_file (path )
112
120
113
121
114
122
@as_file .register (pathlib .Path )
You can’t perform that action at this time.
0 commit comments