[Python-Dev] __del__ and tp_dealloc in the IO lib
Giovanni Bajo
rasky at develer.com
Fri Jan 23 02:22:15 CET 2009
On Mon, 19 Jan 2009 01:38:18 +0000, Gregory P. Smith wrote:
> I regularly point out in code reviews that the very convenient and
> common idiom of open(name, 'w').write(data) doesn't guarantee when the
> file will be closed; its up to the GC implementation details.
Which, to me, sounds like "please, don't assume that bytes are 8-bits
wide; this depends on implementation details of your CPU".
CPython will always use reference counting and thus have a simple and
clear GC criteria that can be exploited to simplify the code. I
personally don't like defensive programming, nor coding for situations
that will never arise . When I write CPython applications (thus, for
instance, using C extensions), I don't see *any* point in trying to
achieve any cross-python-implementation compatibility. I simply don't
need it.
Probably, library programmers have a different point of view. But I
always object when I'm told that I should make my code longer and harder
to read only because CPython might stop using reference counting (...
when hell freezes over).
Back to the topic, please let's keep things as they are now: the file
descriptor is automatically closed as soon as the file object is
destroyed. If you then feel "safer" always using with or try/finally,
nobody is going to complain. And everybody will be happy :)
--
Giovanni Bajo
Develer S.r.l.
http://www.develer.com
More information about the Python-Dev
mailing list