<s> r"""<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import errno <EOL> import time <EOL> import random <EOL> from . _compat import to_unicode <EOL> from . filesystem import get_filesystem_encoding <EOL> can_rename_open_file = False <EOL> if os . name == '<STR_LIT>' : <EOL> _rename = lambda src , dst : False <EOL> _rename_atomic = lambda src , dst : False <EOL> try : <EOL> import ctypes <EOL> _MOVEFILE_REPLACE_EXISTING = <NUM_LIT> <EOL> _MOVEFILE_WRITE_THROUGH = <NUM_LIT> <EOL> _MoveFileEx = ctypes . windll . kernel32 . MoveFileExW <EOL> def _rename ( src , dst ) : <EOL> src = to_unicode ( src , get_filesystem_encoding ( ) ) <EOL> dst = to_unicode ( dst , get_filesystem_encoding ( ) ) <EOL> if _rename_atomic ( src , dst ) : <EOL> return True <EOL> retry = <NUM_LIT:0> <EOL> rv = False <EOL> while not rv and retry < <NUM_LIT:100> : <EOL> rv = _MoveFileEx ( src , dst , _MOVEFILE_REPLACE_EXISTING | <EOL> _MOVEFILE_WRITE_THROUGH ) <EOL> if not rv : <EOL> time . sleep ( <NUM_LIT> ) <EOL> retry += <NUM_LIT:1> <EOL> return rv <EOL> _CreateTransaction = ctypes . windll . ktmw32 . CreateTransaction <EOL> _CommitTransaction = ctypes . windll . ktmw32 . CommitTransaction <EOL> _MoveFileTransacted = ctypes . windll . kernel32 . MoveFileTransactedW <EOL> _CloseHandle = ctypes . windll . kernel32 . CloseHandle <EOL> can_rename_open_file = True <EOL> def _rename_atomic ( src , dst ) : <EOL> ta = _CreateTransaction ( None , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1000> , '<STR_LIT>' ) <EOL> if ta == - <NUM_LIT:1> : <EOL> return False <EOL> try : <EOL> retry = <NUM_LIT:0> <EOL> rv = False <EOL> while not rv and retry < <NUM_LIT:100> : <EOL> rv = _MoveFileTransacted ( src , dst , None , None , <EOL> _MOVEFILE_REPLACE_EXISTING | <EOL> _MOVEFILE_WRITE_THROUGH , ta ) <EOL> if rv : <EOL> rv = _CommitTransaction ( ta ) <EOL> break <EOL> else : <EOL> time . sleep ( <NUM_LIT> ) <EOL> retry += <NUM_LIT:1> <EOL> return rv <EOL> finally : <EOL> _CloseHandle ( ta ) <EOL> except Exception : <EOL> pass <EOL> def rename ( src , dst ) : <EOL> if _rename ( src , dst ) : <EOL> return <EOL> try : <EOL> os . rename ( src , dst ) <EOL> except OSError as e : <EOL> if e . errno != errno . EEXIST : <EOL> raise <EOL> old = "<STR_LIT>" % ( dst , random . randint ( <NUM_LIT:0> , sys . maxint ) ) <EOL> os . rename ( dst , old ) <EOL> os . rename ( src , dst ) <EOL> try : <EOL> os . unlink ( old ) <EOL> except Exception : <EOL> pass <EOL> else : <EOL> rename = os . rename <EOL> can_rename_open_file = True </s>
