<s> '''<STR_LIT>''' <EOL> import contextlib <EOL> import errno <EOL> import os <EOL> import shutil <EOL> import sys <EOL> import tempfile <EOL> import types <EOL> try : <EOL> _replace_file = os . replace <EOL> except AttributeError : <EOL> if sys . platform != '<STR_LIT:win32>' : <EOL> _replace_file = os . rename <EOL> else : <EOL> import win32api <EOL> import win32con <EOL> def _replace_file ( src , dst ) : <EOL> win32api . MoveFileEx ( src , dst , win32con . MOVEFILE_REPLACE_EXISTING ) <EOL> @ contextlib . contextmanager <EOL> def atomic_open ( filename , mode , * args , ** kwargs ) : <EOL> if mode [ <NUM_LIT:0> ] not in '<STR_LIT>' or len ( mode ) > <NUM_LIT:1> and mode [ <NUM_LIT:1> ] == '<STR_LIT:+>' : <EOL> raise ValueError ( "<STR_LIT>" . format ( mode ) ) <EOL> f = tempfile . NamedTemporaryFile ( mode = mode , <EOL> prefix = os . path . basename ( filename ) , <EOL> dir = os . path . dirname ( filename ) , <EOL> suffix = '<STR_LIT>' , <EOL> delete = False ) <EOL> _discard = [ False ] <EOL> try : <EOL> if mode [ <NUM_LIT:0> ] == '<STR_LIT:a>' : <EOL> try : <EOL> with open ( filename , '<STR_LIT:r>' + mode [ <NUM_LIT:1> : ] , * args , ** kwargs ) as fin : <EOL> shutil . copyfileobj ( fin , f ) <EOL> except ( OSError , IOError ) as e : <EOL> if e . errno == errno . ENOENT : <EOL> pass <EOL> def discard ( self , _discard = _discard ) : <EOL> _discard [ <NUM_LIT:0> ] = True <EOL> f . discard = types . MethodType ( discard , f ) <EOL> yield f <EOL> except : <EOL> _discard = [ True ] <EOL> raise <EOL> finally : <EOL> f . close ( ) <EOL> if _discard [ <NUM_LIT:0> ] : <EOL> os . unlink ( f . name ) <EOL> else : <EOL> _replace_file ( f . name , filename ) </s>
