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

Alex Gaynor alex.gaynor at gmail.com
Thu Aug 19 07:09:25 CEST 2010


On Thu, Aug 19, 2010 at 12:07 AM, sakesun roykiatisak <sakesun at gmail.com> wrote:
>
> A little problem is that, "with" statement is yet to work in pypy.
> :)
>
> On Thu, Aug 19, 2010 at 11:49 AM, sakesun roykiatisak <sakesun at gmail.com>
> wrote:
>>
>> That's make sense.  I've tried on both IronPython and Jython with:
>> ipy -c "open(’xxx’, ’w’).write(’stuff’)"
>> jython -c "open(’xxx’, ’w’).write(’stuff’)"
>> When the interpreter terminate the file is closed. That's why it didn't
>> cause any problem.
>> Perhaps, I should always use "with" statement from now on.
>> >>> with open('xxx', 'w') as f: f.write('stuff')
>> Thanks
>>
>> On Thu, Aug 19, 2010 at 11:40 AM, Aaron DeVore <aaron.devore at gmail.com>
>> wrote:
>>>
>>> If I understand correctly, PyPy will garbage collect (and close) the
>>> file object at an indeterminate time. That time could be as long as
>>> until the program exits. Because CPython uses reference counting, it
>>> closes the file immediately after the file object goes out of scope.
>>>
>>> Of course, I may be entirely wrong.
>>>
>>> -Aaron DeVore
>>>
>>> On Wed, Aug 18, 2010 at 9:25 PM, sakesun roykiatisak <sakesun at gmail.com>
>>> wrote:
>>> > Hi,
>>> >  I encountered this quite a few times when learning pypy from internet
>>> > resources:
>>> >   the code like this
>>> >>>> open(’xxx’, ’w’).write(’stuff’)
>>> > This code is not working on pypy because it rely on CPython refcounting
>>> > behaviour.
>>> > I don't get it. Why ?  I thought the code should be similar to storing
>>> > the
>>> > file object in temporary variable like this
>>> >>>> f = open('xxx', 'w')
>>> >>>> f.write('stuff')
>>> >>>> del f
>>> > Also, I've tried that with both Jython and IronPython and they all work
>>> > fine.
>>> > Why does this cause problem to pypy ?  Do I have to avoid writing code
>>> > like
>>> > this in the future ?
>>> > _______________________________________________
>>> > pypy-dev at codespeak.net
>>> > http://codespeak.net/mailman/listinfo/pypy-dev
>>> >
>>
>
>
> _______________________________________________
> pypy-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/pypy-dev
>

Since PyPy implements Python 2.5 at present you'll need to use `from
__future__ import with_statement` to ues it.

Alex

-- 
"I disapprove of what you say, but I will defend to the death your
right to say it." -- Voltaire
"The people's good is the highest law." -- Cicero
"Code can always be simpler than you think, but never as simple as you
want" -- Me



More information about the Pypy-dev mailing list