28
28
from _pytest .assertion .util import ( # noqa: F401
29
29
format_explanation as _format_explanation ,
30
30
)
31
+ from _pytest .compat import fspath
31
32
from _pytest .pathlib import fnmatch_ex
32
33
from _pytest .pathlib import PurePath
33
34
@@ -120,7 +121,7 @@ def exec_module(self, module):
120
121
write = not sys .dont_write_bytecode
121
122
cache_dir = get_cache_dir (fn )
122
123
if write :
123
- ok = try_mkdir (cache_dir )
124
+ ok = try_makedirs (cache_dir )
124
125
if not ok :
125
126
write = False
126
127
state .trace ("read only directory: {}" .format (cache_dir ))
@@ -259,7 +260,7 @@ def _write_pyc(state, co, source_stat, pyc):
259
260
# (C)Python, since these "pycs" should never be seen by builtin
260
261
# import. However, there's little reason deviate.
261
262
try :
262
- with atomicwrites .atomic_write (str (pyc ), mode = "wb" , overwrite = True ) as fp :
263
+ with atomicwrites .atomic_write (fspath (pyc ), mode = "wb" , overwrite = True ) as fp :
263
264
fp .write (importlib .util .MAGIC_NUMBER )
264
265
# as of now, bytecode header expects 32-bit numbers for size and mtime (#4903)
265
266
mtime = int (source_stat .st_mtime ) & 0xFFFFFFFF
@@ -278,7 +279,7 @@ def _write_pyc(state, co, source_stat, pyc):
278
279
279
280
def _rewrite_test (fn , config ):
280
281
"""read and rewrite *fn* and return the code object."""
281
- fn = str (fn )
282
+ fn = fspath (fn )
282
283
stat = os .stat (fn )
283
284
with open (fn , "rb" ) as f :
284
285
source = f .read ()
@@ -294,12 +295,12 @@ def _read_pyc(source, pyc, trace=lambda x: None):
294
295
Return rewritten code if successful or None if not.
295
296
"""
296
297
try :
297
- fp = open (str (pyc ), "rb" )
298
+ fp = open (fspath (pyc ), "rb" )
298
299
except IOError :
299
300
return None
300
301
with fp :
301
302
try :
302
- stat_result = os .stat (str (source ))
303
+ stat_result = os .stat (fspath (source ))
303
304
mtime = int (stat_result .st_mtime )
304
305
size = stat_result .st_size
305
306
data = fp .read (12 )
@@ -751,7 +752,7 @@ def visit_Assert(self, assert_):
751
752
"assertion is always true, perhaps remove parentheses?"
752
753
),
753
754
category = None ,
754
- filename = str (self .module_path ),
755
+ filename = fspath (self .module_path ),
755
756
lineno = assert_ .lineno ,
756
757
)
757
758
@@ -874,7 +875,7 @@ def warn_about_none_ast(self, node, module_path, lineno):
874
875
lineno={lineno},
875
876
)
876
877
""" .format (
877
- filename = str (module_path ), lineno = lineno
878
+ filename = fspath (module_path ), lineno = lineno
878
879
)
879
880
).body
880
881
return ast .If (val_is_none , send_warning , [])
@@ -1020,18 +1021,15 @@ def visit_Compare(self, comp: ast.Compare):
1020
1021
return res , self .explanation_param (self .pop_format_context (expl_call ))
1021
1022
1022
1023
1023
- def try_mkdir (cache_dir ):
1024
- """Attempts to create the given directory, returns True if successful"""
1024
+ def try_makedirs (cache_dir ) -> bool :
1025
+ """Attempts to create the given directory and sub-directories exist, returns True if
1026
+ successful or it already exists"""
1025
1027
try :
1026
- os .makedirs (str (cache_dir ))
1027
- except FileExistsError :
1028
- # Either the pycache directory already exists (the
1029
- # common case) or it's blocked by a non-dir node. In the
1030
- # latter case, we'll ignore it in _write_pyc.
1031
- return True
1032
- except (FileNotFoundError , NotADirectoryError ):
1033
- # One of the path components was not a directory, likely
1034
- # because we're in a zip file.
1028
+ os .makedirs (fspath (cache_dir ), exist_ok = True )
1029
+ except (FileNotFoundError , NotADirectoryError , FileExistsError ):
1030
+ # One of the path components was not a directory:
1031
+ # - we're in a zip file
1032
+ # - it is a file
1035
1033
return False
1036
1034
except PermissionError :
1037
1035
return False
0 commit comments