Re: [pypy-dev] What's wrong with >>> open(’xxx’, ’w’).write(’stuff’) ?
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@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
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
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
On Wed, Aug 18, 2010 at 9:25 PM, sakesun roykiatisak <sakesun@gmail.com> wrote: the like
this in the future ? _______________________________________________ pypy-dev@codespeak.net http://codespeak.net/mailman/listinfo/pypy-dev
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@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@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
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
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
On Wed, Aug 18, 2010 at 9:25 PM, sakesun roykiatisak <sakesun@gmail.com> wrote: the like
this in the future ? _______________________________________________ pypy-dev@codespeak.net http://codespeak.net/mailman/listinfo/pypy-dev
On Thu, Aug 19, 2010 at 12:07 AM, sakesun roykiatisak <sakesun@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@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@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@gmail.com> wrote:
> 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
> 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
Hi, I encountered this quite a few times when learning pypy from internet resources: the code like this the file object in temporary variable like this like this in the future ? _______________________________________________ pypy-dev@codespeak.net http://codespeak.net/mailman/listinfo/pypy-dev
_______________________________________________ pypy-dev@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
A good resource I recently read on this is this entry in Raymond Chen's blog: http://blogs.msdn.com/b/oldnewthing/archive/2010/08/09/10047586.aspx Together with the following entry, which explains why the lifetime of the variable has nothing to do with the lifetime of the object, this should help you understand. You should consider automatically closing a file to be an implementation detail, even cpython may not respect such semantics in future. That is why the with statement was created. -- William Leslie
Thanks. Interestingly, this is not the first time I was suggested to pursue further reading with Raymond Chen's blog. http://www.mail-archive.com/users@lists.ironpython.com/msg05792.html <http://www.mail-archive.com/users@lists.ironpython.com/msg05792.html> :) On Thu, Aug 19, 2010 at 12:13 PM, William Leslie < william.leslie.ttg@gmail.com> wrote:
A good resource I recently read on this is this entry in Raymond Chen's blog:
http://blogs.msdn.com/b/oldnewthing/archive/2010/08/09/10047586.aspx
Together with the following entry, which explains why the lifetime of the variable has nothing to do with the lifetime of the object, this should help you understand.
You should consider automatically closing a file to be an implementation detail, even cpython may not respect such semantics in future. That is why the with statement was created.
-- William Leslie
participants (3)
-
Alex Gaynor
-
sakesun roykiatisak
-
William Leslie