[Python-Dev] del and tp_dealloc in the IO lib (original) (raw)
Brett Cannon brett at python.org
Mon Jan 19 00:19:39 CET 2009
- Previous message: [Python-Dev] __del__ and tp_dealloc in the IO lib
- Next message: [Python-Dev] __del__ and tp_dealloc in the IO lib
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Sun, Jan 18, 2009 at 15:03, Antoine Pitrou <solipsis at pitrou.net> wrote:
Dear python-dev,
The Python implementation of IOBase, the base class for everything IO, has the (strange) idea to define a del method. It is probably meant to avoid code duplication, so that users subclassing IOBase automatically get the close-on-destruct behaviour. (there is an even stranger test in testio which involves overriding the del method in a class derived from FileIO...) However, it has the drawback that all IO objects inherit a del method, meaning problems when collecting reference cycles (the del may not get called if caught in a reference cycle, defeating the whole point). While rewriting the IO stack in C, we have tried to keep this behaviour, but it seems better to just do it in the tpdealloc function, and kill the del (actually, we already do it in tpdealloc, because del / tpdel behaviour for C types is shady). Subclassing IOBase in Python would keep the tpdealloc and therefore the close-on-destruct behaviour, without the problems of a del method. (the implementation has to take a few precautions, first revive the object, then check its "closed" attribute/property - ignoring errors -, and if "closed" ended False then call the close() method) What do you think?
Fine by me. People should be using the context manager for guaranteed file closure anyway IMO.
-Brett
- Previous message: [Python-Dev] __del__ and tp_dealloc in the IO lib
- Next message: [Python-Dev] __del__ and tp_dealloc in the IO lib
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]