Re: when should I explicitly close a file?

2010-04-27 Thread Lawrence D'Oliveiro
In message , Adam 
Tauno Williams wrote:

> On Fri, 2010-04-23 at 16:29 +1200, Lawrence D'Oliveiro wrote:
>
>> Any implementation that doesn’t do reference-counting is brain-damaged.
> 
> Why?

Because a) it uses extra memory needlessly, and b) waiting until an object 
has dropped out of cache before touching it again just slows things down.

> There are much better ways to do memory management / garbage
> collection;  especially when dealing with large applications.

Especially with large applications, the above considerations apply even more 
so.

If you don’t agree, you might as well stick to Java.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-24 Thread Alf P. Steinbach

* Steven D'Aprano:

On Fri, 23 Apr 2010 13:19:41 +0200, Alf P. Steinbach wrote:


But for a literal context-free interpretation e.g. the 'sys.getrefcount'
function is not documented as CPython only and thus an implementation
that didn't do reference counting would not be a conforming Python
implementation.


Since Jython and IronPython are conforming Python implementations, and 
Guido has started making policy decisions specifically to support these 
other implementations (e.g. the language feature moratorium, PEP 3003), I 
think we can assume that this is a documentation bug.


The documentation for Jython specifies the same for 'sys.getrefcount'.

However, testing:


*sys-package-mgr*: processing new jar, 'C:\Program Files\jython2.5.1\jython.jar'
*sys-package-mgr*: processing new jar, 'C:\Program 
Files\Java\jre6\lib\resources.jar'

*sys-package-mgr*: processing new jar, 'C:\Program Files\Java\jre6\lib\rt.jar'
*sys-package-mgr*: processing new jar, 'C:\Program Files\Java\jre6\lib\jsse.jar'
*sys-package-mgr*: processing new jar, 'C:\Program Files\Java\jre6\lib\jce.jar'
*sys-package-mgr*: processing new jar, 'C:\Program 
Files\Java\jre6\lib\charsets.jar'
*sys-package-mgr*: processing new jar, 'C:\Program 
Files\Java\jre6\lib\ext\dnsns.jar'
*sys-package-mgr*: processing new jar, 'C:\Program 
Files\Java\jre6\lib\ext\localedata.jar'
*sys-package-mgr*: processing new jar, 'C:\Program 
Files\Java\jre6\lib\ext\sunjce_provider.jar'
*sys-package-mgr*: processing new jar, 'C:\Program 
Files\Java\jre6\lib\ext\sunmscapi.jar'
*sys-package-mgr*: processing new jar, 'C:\Program 
Files\Java\jre6\lib\ext\sunpkcs11.jar'

A created
Traceback (most recent call last):
  File "c:\test\refcount.py", line 17, in 
writeln( str( sys.getrefcount( a ) - 1 ) )
AttributeError: 'systemstate' object has no attribute 'getrefcount'



However, a Python implementation that always returned 0 for 
sys.getrefcount would technically satisfy the word of the documentation, 
if not the spirit.


Yes.

OK, learned something new: I though Jython actually implemented getrefcount.

The Jython docs says it does...


Cheers,

- Alf

--
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-23 Thread Steven D'Aprano
On Fri, 23 Apr 2010 13:19:41 +0200, Alf P. Steinbach wrote:

> But for a literal context-free interpretation e.g. the 'sys.getrefcount'
> function is not documented as CPython only and thus an implementation
> that didn't do reference counting would not be a conforming Python
> implementation.

Since Jython and IronPython are conforming Python implementations, and 
Guido has started making policy decisions specifically to support these 
other implementations (e.g. the language feature moratorium, PEP 3003), I 
think we can assume that this is a documentation bug.

However, a Python implementation that always returned 0 for 
sys.getrefcount would technically satisfy the word of the documentation, 
if not the spirit.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-23 Thread Alf P. Steinbach

* Adam Tauno Williams:

On Fri, 2010-04-23 at 16:29 +1200, Lawrence D'Oliveiro wrote:
In message , Chris 
Rebert wrote:

On Wed, Apr 21, 2010 at 5:53 PM, Lawrence D'Oliveiro wrote:

In message <4bc9aad...@dnews.tpgi.com.au>, Lie Ryan wrote:

Since in python nothing is guaranteed about implicit file close ...

It is guaranteed that objects with a reference count of zero will be
disposed.
In my experiments, this happens immediately.

Experiment with an implementation other than CPython and prepare to be
surprised.

Any implementation that doesn’t do reference-counting is brain-damaged.


Why?


Depends on what the statement was meant to mean.

But for a literal context-free interpretation e.g. the 'sys.getrefcount' 
function is not documented as CPython only and thus an implementation that 
didn't do reference counting would not be a conforming Python implementation.


Whether it uses reference counting to destroy objects at earliest opportunity is 
another matter.




 There are much better ways to do memory management / garbage
collection;  especially when dealing with large applications.


Depends on whether you're talking about Python implementations or as a matter of 
general principle, and depends on how you define "better", "large" and so on.


On its own it's a pretty meaningless statement.

But although a small flame war erupted the last time I mentioned this, I think a 
case can be made that Python is not designed for programming-in-the-large. And 
that the current CPython scheme is eminently suitable for small scripts. But it 
has its drawbacks, especially considering the various ways that stack frames can 
be retained, and considering the documentation of 'gc.garbage', ...


  "Objects that have __del__() methods and are part of a reference cycle cause
  the entire reference cycle to be uncollectable, including objects not
  necessarily in the cycle but reachable only from it."

... which means that a programming style assuming current CPython semantics and 
employing RAII can be detrimental in a sufficiently large system.



Cheers & hth.,

- Alf
--
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-23 Thread Adam Tauno Williams
On Fri, 2010-04-23 at 16:29 +1200, Lawrence D'Oliveiro wrote:
> In message , Chris 
> Rebert wrote:
> > On Wed, Apr 21, 2010 at 5:53 PM, Lawrence D'Oliveiro wrote:
> >> In message <4bc9aad...@dnews.tpgi.com.au>, Lie Ryan wrote:
> >>> Since in python nothing is guaranteed about implicit file close ...
> >> It is guaranteed that objects with a reference count of zero will be
> >> disposed.
> >> In my experiments, this happens immediately.
> > Experiment with an implementation other than CPython and prepare to be
> > surprised.
> Any implementation that doesn’t do reference-counting is brain-damaged.

Why?  There are much better ways to do memory management / garbage
collection;  especially when dealing with large applications.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-22 Thread Benjamin Kaplan
On Fri, Apr 23, 2010 at 12:29 AM, Lawrence D'Oliveiro
 wrote:

> In message , Chris
> Rebert wrote:
>
> > On Wed, Apr 21, 2010 at 5:53 PM, Lawrence D'Oliveiro wrote:
> >
> >> In message <4bc9aad...@dnews.tpgi.com.au>, Lie Ryan wrote:
> >>
> >>> Since in python nothing is guaranteed about implicit file close ...
> >>
> >> It is guaranteed that objects with a reference count of zero will be
> >> disposed.
> >
> >> In my experiments, this happens immediately.
> >
> > Experiment with an implementation other than CPython and prepare to be
> > surprised.
>
> Any implementation that doesn’t do reference-counting is brain-damaged.
> --
>

Why? Nothing in the Python spec calls for reference-counting. And (AFAIK)
everything that runs on the JVM or CLR uses garabage collection and not
reference counting. Have you heard of those things before? They're rather
popular.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-22 Thread Steven D'Aprano
On Fri, 23 Apr 2010 16:29:46 +1200, Lawrence D'Oliveiro wrote:

> Any implementation that doesn’t do reference-counting is brain-damaged.

Funny, that's exactly what other people say about implementations that 
*do* use reference counting.

-- 
Steven

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-22 Thread Lawrence D'Oliveiro
In message , Chris 
Rebert wrote:

> On Wed, Apr 21, 2010 at 5:53 PM, Lawrence D'Oliveiro wrote:
>
>> In message <4bc9aad...@dnews.tpgi.com.au>, Lie Ryan wrote:
>>
>>> Since in python nothing is guaranteed about implicit file close ...
>>
>> It is guaranteed that objects with a reference count of zero will be
>> disposed.
> 
>> In my experiments, this happens immediately.
> 
> Experiment with an implementation other than CPython and prepare to be
> surprised.

Any implementation that doesn’t do reference-counting is brain-damaged.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-22 Thread Adam Tauno Williams
On Thu, 2010-04-22 at 12:53 +1200, Lawrence D'Oliveiro wrote:
> In message <4bc9aad...@dnews.tpgi.com.au>, Lie Ryan wrote:
> > Since in python nothing is guaranteed about implicit file close ...
> It is guaranteed that objects with a reference count of zero will be 
> disposed. In my experiments, this happens immediately.

A current implementation specific detail.  Always close files.
Otherwise, in the future, or on a different run-time, your code will
break.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-21 Thread Alf P. Steinbach

* Lawrence D'Oliveiro:

In message <4bc9aad...@dnews.tpgi.com.au>, Lie Ryan wrote:


Since in python nothing is guaranteed about implicit file close ...


It is guaranteed that objects with a reference count of zero will be 
disposed.


Only in current CPython.



In my experiments, this happens immediately.


Depends what you mean, but even in current CPython destruction of a local can be 
postponed indefinitely if a reference to the stack frame is kept somewhere.


And that happens, for example, when an exception is raised (until the handler 
completes, but it doesn't necessarily complete for a Very Long Time).



Cheers & hth.,

- Alf
--
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-21 Thread Steven D'Aprano
On Thu, 22 Apr 2010 12:53:51 +1200, Lawrence D'Oliveiro wrote:

> In message <4bc9aad...@dnews.tpgi.com.au>, Lie Ryan wrote:
> 
>> Since in python nothing is guaranteed about implicit file close ...
> 
> It is guaranteed that objects with a reference count of zero will be
> disposed. 

Not all Python implementations have reference counts at all, e.g. Jython 
and IronPython. Neither of those close files immediately.


> In my experiments, this happens immediately.

Are your experiments done under PyPy, CLPython, or Pynie?



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-21 Thread Chris Rebert
On Wed, Apr 21, 2010 at 5:53 PM, Lawrence D'Oliveiro wrote:
> In message <4bc9aad...@dnews.tpgi.com.au>, Lie Ryan wrote:
>
>> Since in python nothing is guaranteed about implicit file close ...
>
> It is guaranteed that objects with a reference count of zero will be
> disposed.

> In my experiments, this happens immediately.

Experiment with an implementation other than CPython and prepare to be
surprised.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-21 Thread Lawrence D'Oliveiro
In message <4bc9aad...@dnews.tpgi.com.au>, Lie Ryan wrote:

> Since in python nothing is guaranteed about implicit file close ...

It is guaranteed that objects with a reference count of zero will be 
disposed. In my experiments, this happens immediately.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-17 Thread Lie Ryan
On 04/17/10 21:23, Lawrence D'Oliveiro wrote:
> In message 
> , gelonida 
> wrote:
> 
>> I've been told, that following code snippet is not good.
>>
>> open("myfile","w").write(astring) ...
> 
> I do that for reads, but never for writes.
> 
> For writes, you want to give a chance for write errors to raise an exception 
> and alert the user, instead of failing silently, to avoid inadvertent data 
> loss. Hence the explicit close.

In short, in case of doubt, just be explicit.

Since in python nothing is guaranteed about implicit file close, you
must always explicitly close it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-17 Thread Lawrence D'Oliveiro
In message 
, gelonida 
wrote:

> I've been told, that following code snippet is not good.
> 
> open("myfile","w").write(astring) ...

I do that for reads, but never for writes.

For writes, you want to give a chance for write errors to raise an exception 
and alert the user, instead of failing silently, to avoid inadvertent data 
loss. Hence the explicit close.
-- 
http://mail.python.org/mailman/listinfo/python-list