Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:

Problems:

1. In general, it is hard to avoid leaks because an exception like 
KeyboardInterrupt or MemoryError can be raised virtually anywhere, even before 
we save a file descriptor. We may rewrite the code so that it will use few 
simple bytecode instructions and disable interruption before these 
instructions. This may solve the problem in CPython, but alternate 
implementation will need to handle this theirself.

2. It is not safe to close a file descriptor if io.open() is failed, because it 
can already be closed in io.open(), and we do not know where it was closed or 
no. It can be solved by using the opener argument, but we will need to patch 
the name attribute of the file returned by io.open(), and it is not easy, 
because the type of the result of io.open() depends on the mode and buffering 
arguments, and only FileIO has writable name attribute. This will need 
additional complex code. We can also change the behavior of io.open(), make it 
either always closing fd or never closing it in case of error. But this will 
break working code, or introduce leaks in working code, so such change cannot 
be backported. In any case we should revisit all other cases of using io.open() 
with an open file descriptor in the stdlib (including TemporaryFile).

3. I afraid that removing a file while the file descriptor is open may not work 
on Windows. Seems this case is not well tested.

As for using `except Exception` instead of `except BaseException`, it is better 
to use the later, or even the bare `except`.

I worked on this issue yesterday, but then found new problems, it will take 
several days or weeks to solve them. As a workaround I suggest to skip 
temporary the test which monkeypatches _TemporaryFileWrapper.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39318>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to