[pypy-dev] What's wrong with >>> open(?xxx?, ?w?).write(?stuff?) ?

Paolo Giarrusso p.giarrusso at gmail.com
Fri Aug 20 15:39:22 CEST 2010


On Fri, Aug 20, 2010 at 12:23, Donny Viszneki <donny.viszneki at gmail.com> wrote:
> Armin: Sakesun used "del f" and it appears you did not.
Actually, he didn't either. He said "I think that open(’xxx’,
’w’).write(’stuff’)" is equivalent to using del (which he thought
would work), and the equivalence was correct.

Anyway, in the _first reply_ message, he realized that using:

ipy -c "open(’xxx’, ’w’).write(’stuff’)"
jython -c "open(’xxx’, ’w’).write(’stuff’)"

made a difference (because the interpreter exited), so that problem
was solved. His mail implies that on PyPy he typed the code at the
prompt, rather than at -c.

> In Python
> IIRC, an explicit call to del should kick off the finalizer to flush
> and close the file!

No, as shown by Armin. del will just clear the reference, which on
CPython means decreasing the refcount. Refcounting will then finalize
the object immediately, a GC at some later point, if it runs at all -
there's no such guarantee on Java and .NET. For Java, that's unless
you do special unsafe setup (System.runFinalizersOnExit(), it's
discouraged for a number of reasons, see docs). On .NET, I expect a
such method to exist, too, since they were so unaware of problems
wiith finalizers in .NET 1.0 to give them the syntax of destructors.
But .NET 2.0 has SafeHandles, which guarantee release of critical
resources if the "finalization" code follows some restriction, using
_reference counting_:

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.safehandle.aspx
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.safehandle.dangerousaddref.aspx

> open('x', 'w').write('hello') alone does not imply the file instance
> (return value of open()) has been finalized because the garbage
> collector may not have hit it yet.

On CPython, you have such an implication, because of refcounting semantics.

> On Fri, Aug 20, 2010 at 5:57 AM, Armin Rigo <arigo at tunes.org> wrote:
>> Hi Sakesun,
>>
>> On Thu, Aug 19, 2010 at 11:25:42AM +0700, sakesun roykiatisak wrote:
>>> >>> f = open('xxx', 'w')
>>> >>> f.write('stuff')
>>> >>> del f
>>>
>>> Also, I've tried that with both Jython and IronPython and they all work
>>> fine.
>>
>> I guess that you didn't try exactly the same thing.  If I do:
>>
>>    arigo at tannit ~ $ jython
>>    Jython 2.2.1 on java1.6.0_20
>>    Type "copyright", "credits" or "license" for more information.
>>    >>> open('x', 'w').write('hello')
>>    >>>
>>
>> Then "cat x" in another terminal shows an empty file.  The file "x" is
>> only filled when I exit Jython.  It is exactly the same behavior as I
>> get on PyPy.  Maybe I missed something, and there is a different way to
>> do things such that it works on Jython but not on PyPy; if so, can you
>> describe it more precisely?  Thanks!

-- 
Paolo Giarrusso - Ph.D. Student
http://www.informatik.uni-marburg.de/~pgiarrusso/



More information about the Pypy-dev mailing list