This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients Guido.van.Rossum, eryksun, jkloth, pablogsal, paul.moore, shreyanavigyan, steve.dower, tim.golden, tim.peters, vstinner, zach.ware
Date 2021-04-29.19:09:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1619723361.63.0.0692332071954.issue37387@roundup.psfhosted.org>
In-reply-to
Content
> I don't know enough about when and why CPython decides to 
> replace .pyc files 

It's straight-forward in the case of py_compile.compile():

    >>> pyc = py_compile.compile('test.py')
    >>> f = open(pyc)
    >>> try: py_compile.compile('test.py')
    ... except OSError as e: print(e)
    ...
    [WinError 5] Access is denied: '__pycache__\\test.cpython-310.pyc.1914201873840' -> '__pycache__\\test.cpython-310.pyc'

Rewriting the file uses `importlib._bootstrap_external._write_atomic()`, which writes the PYC to a temporary name and then tries to replace the existing file via os.replace(). 

Replacing an existing filename will fail with access denied in Windows if the target filename exists and is already open, mapped as image/data, readonly, or a directory. Notably it's not a sharing violation in the case of an open file, which means it doesn't matter whether the open(s) share delete access or not. In Windows 10 the NT API has a new file rename operation that supports a flag to replace an open file, but in this case we're still stuck with the problem that existing opens have to share delete access. Most opens could share delete access without a problem, but most don't.
History
Date User Action Args
2021-04-29 19:09:21eryksunsetrecipients: + eryksun, tim.peters, paul.moore, vstinner, tim.golden, jkloth, zach.ware, steve.dower, Guido.van.Rossum, pablogsal, shreyanavigyan
2021-04-29 19:09:21eryksunsetmessageid: <1619723361.63.0.0692332071954.issue37387@roundup.psfhosted.org>
2021-04-29 19:09:21eryksunlinkissue37387 messages
2021-04-29 19:09:20eryksuncreate