|
20 | 20 | from typing import Set
|
21 | 21 | from typing import Tuple
|
22 | 22 |
|
23 |
| -import atomicwrites |
24 |
| - |
25 | 23 | from _pytest._io.saferepr import saferepr
|
26 | 24 | from _pytest._version import version
|
27 | 25 | from _pytest.assertion import util
|
@@ -255,26 +253,59 @@ def get_data(self, pathname):
|
255 | 253 | return f.read()
|
256 | 254 |
|
257 | 255 |
|
258 |
| -def _write_pyc(state, co, source_stat, pyc): |
| 256 | +def _write_pyc_fp(fp, source_stat, co): |
259 | 257 | # Technically, we don't have to have the same pyc format as
|
260 | 258 | # (C)Python, since these "pycs" should never be seen by builtin
|
261 | 259 | # import. However, there's little reason deviate.
|
262 |
| - try: |
263 |
| - with atomicwrites.atomic_write(fspath(pyc), mode="wb", overwrite=True) as fp: |
264 |
| - fp.write(importlib.util.MAGIC_NUMBER) |
265 |
| - # as of now, bytecode header expects 32-bit numbers for size and mtime (#4903) |
266 |
| - mtime = int(source_stat.st_mtime) & 0xFFFFFFFF |
267 |
| - size = source_stat.st_size & 0xFFFFFFFF |
268 |
| - # "<LL" stands for 2 unsigned longs, little-ending |
269 |
| - fp.write(struct.pack("<LL", mtime, size)) |
270 |
| - fp.write(marshal.dumps(co)) |
271 |
| - except EnvironmentError as e: |
272 |
| - state.trace("error writing pyc file at {}: errno={}".format(pyc, e.errno)) |
273 |
| - # we ignore any failure to write the cache file |
274 |
| - # there are many reasons, permission-denied, pycache dir being a |
275 |
| - # file etc. |
276 |
| - return False |
277 |
| - return True |
| 260 | + fp.write(importlib.util.MAGIC_NUMBER) |
| 261 | + # as of now, bytecode header expects 32-bit numbers for size and mtime (#4903) |
| 262 | + mtime = int(source_stat.st_mtime) & 0xFFFFFFFF |
| 263 | + size = source_stat.st_size & 0xFFFFFFFF |
| 264 | + # "<LL" stands for 2 unsigned longs, little-ending |
| 265 | + fp.write(struct.pack("<LL", mtime, size)) |
| 266 | + fp.write(marshal.dumps(co)) |
| 267 | + |
| 268 | + |
| 269 | +if sys.platform == "win32": |
| 270 | + from atomicwrites import atomic_write |
| 271 | + |
| 272 | + def _write_pyc(state, co, source_stat, pyc): |
| 273 | + try: |
| 274 | + with atomic_write(fspath(pyc), mode="wb", overwrite=True) as fp: |
| 275 | + _write_pyc_fp(fp, source_stat, co) |
| 276 | + except EnvironmentError as e: |
| 277 | + state.trace("error writing pyc file at {}: errno={}".format(pyc, e.errno)) |
| 278 | + # we ignore any failure to write the cache file |
| 279 | + # there are many reasons, permission-denied, pycache dir being a |
| 280 | + # file etc. |
| 281 | + return False |
| 282 | + return True |
| 283 | + |
| 284 | + |
| 285 | +else: |
| 286 | + |
| 287 | + def _write_pyc(state, co, source_stat, pyc): |
| 288 | + proc_pyc = "{}.{}".format(pyc, os.getpid()) |
| 289 | + try: |
| 290 | + fp = open(proc_pyc, "wb") |
| 291 | + except EnvironmentError as e: |
| 292 | + state.trace( |
| 293 | + "error writing pyc file at {}: errno={}".format(proc_pyc, e.errno) |
| 294 | + ) |
| 295 | + return False |
| 296 | + |
| 297 | + try: |
| 298 | + _write_pyc_fp(fp, source_stat, co) |
| 299 | + os.rename(proc_pyc, fspath(pyc)) |
| 300 | + except BaseException as e: |
| 301 | + state.trace("error writing pyc file at {}: errno={}".format(pyc, e.errno)) |
| 302 | + # we ignore any failure to write the cache file |
| 303 | + # there are many reasons, permission-denied, pycache dir being a |
| 304 | + # file etc. |
| 305 | + return False |
| 306 | + finally: |
| 307 | + fp.close() |
| 308 | + return True |
278 | 309 |
|
279 | 310 |
|
280 | 311 | def _rewrite_test(fn, config):
|
|
0 commit comments