[Python-Dev] __del__ and tp_dealloc in the IO lib

Steve Holden steve at holdenweb.com
Fri Jan 23 02:48:11 CET 2009


Giovanni Bajo wrote:
> 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".
> 
Which it does, assuming you are  using (for example) an ancient
DECSystem-10. But you really can't assume in your writings about Python
that all readers will be using CPython, so it seems like a reasonable
point to make.

> 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.
> 
Who gave you this guarantee of CPython's future behavior? Who knows
which implementation of Python will be used to support your code and
mine in five years?

> Probably, library programmers have a different point of view.

As they properly should.

> 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).
> 
Ah, religious beliefs ... ;-)

> 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 :)

And what are the IronPython team, and the Jython team, supposed to do
when they get around to implementing Python 3? Clearly (since both teams
are already committed to implementing it) the more we can do to
accommodate them the better it will be for cross-implementation
compatibility. Or did I miss something?

You are, of course, free to make whatever assumptions you like about the
environment in which your code executes.

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/



More information about the Python-Dev mailing list