Closed rec closed 3 days ago
This pull request reduces the use of temporary files and modifies the behavior of file operations in the 'safer' library. The changes primarily affect the test suite and the initialization of the library's main class.
Change | Details | Files |
---|---|---|
Modify file count assertions in test cases |
|
test/test_open.py |
Add new test case for temporary file permissions |
|
test/test_open.py |
Modify temporary file naming in the main class |
|
safer/__init__.py |
Remove explicit chmod operation on temporary files |
|
safer/__init__.py |
The AI schtick wasn't bad and I pushed some consequent changes to clean things up.
This looks reasonable to me, but your change on line 660:
if temp_file is True:
parent, file = os.path.split(target_file)
temp_file = os.path.join(parent, f'.{file}.tmp-safer')
super().__init__(temp_file, delete_failures, parent)
Looks like it will mean that 607 will never be called:
if temp_file is True:
fd, temp_file = tempfile.mkstemp(dir=parent)
os.close(fd)
That might be a reasonable thing to do. Also, this isn't necessarily a problem, but it is a behavior change: by using {file}.tmp-safer
, you lose the randomized names that could prevent (or perhaps delay) issues with attempts to write to the same file. It looks like mkstemp is doing some complex magic to ensure names are randomized in a thread safe way. I'm not sure if that's worth using here as well or not. However, I do like that the temporary file now gives a much better indication of what the file being written will be called. That is a feature I was interested in.
Ah, sorry, I should have written up the change like I said I was going to, but I got distracted by the AI code review! :-D
Two very perceptive comments, thanks!
Line 607 does still get called, exactly in the case where you have a stream which is not a file or a file handle (for example, a socket connection). EDIT: I just checked it by assert False
in that clause and 17 tests fail, exactly those cases.
Indeed, I thought for a while about replicating the magic that mkstemp does! But then I realized the following: if you call safer.open
on the same file twice at the same time, you're going to get unpredictable results anyway.
I will emphasize this in the documentation (EDIT: I just did) but I'm just going to have a blanket prohibition on trying to write to the same file twice at the same time with safer
, because there's no way to make it work.
In that case, the "fixed" temp file name is perfectly OK as there will never be collisions.
Summary by Sourcery
Reduce the use of temporary files by refining the logic for handling temporary file creation and permissions. Fix test assertions to accurately reflect the number of temporary files created. Add a test to ensure consistent permissions for temporary files.
Bug Fixes:
Enhancements:
Tests: