"file" does not work with the "with" statement!

2009-12-10 Thread Michele Simionato
I have just discovered that the syntax with file(name, 'w') as f: do_something(f) does not close the file at the end of the with statement! On the contrary with open(name, 'w') as f: do_something(f) works fine. The docs say "When opening a file, it’s preferable to use open() instead of in

Re: "file" does not work with the "with" statement!

2009-12-10 Thread Michele Simionato
On Dec 10, 11:59 am, Christian Heimes wrote: > In Python 2.5 you have to implement your own __enter__ and __exit__ > methods if you subclass from file. The file.__exit__ function doesn't > call f.close(). Yes, that was my problem. -- http://mail.python.org/mailman/listinfo/python-list

Re: "file" does not work with the "with" statement!

2009-12-10 Thread Peter Otten
Michele Simionato wrote: > On Dec 10, 11:04 am, "Diez B. Roggisch" wrote: >> Are you sure? For me on python2.5, it works as advertised: >> >> from __future__ import with_statement >> >> def test(outf): >> with outf: >> outf.write("test\n") >> try: >> outf.write("test\n") >> assert False, "Not clo

Re: "file" does not work with the "with" statement!

2009-12-10 Thread Christian Heimes
Michele Simionato wrote: > Python 2.5, but it could be an artifact of the way I am looking if a > file is closed. > I have subclassed the file builtin, added a .close method and it was > not called by the > "with" statement. This during debugging, but now I have found another > reason to explain wh

Re: "file" does not work with the "with" statement!

2009-12-10 Thread Diez B. Roggisch
Michele Simionato wrote: > On Dec 10, 11:04 am, "Diez B. Roggisch" wrote: >> Are you sure? For me on python2.5, it works as advertised: >> >> from __future__ import with_statement >> >> def test(outf): >> with outf: >> outf.write("test\n") >> try: >> outf.write("test\n") >> assert False, "Not clo

Re: "file" does not work with the "with" statement!

2009-12-10 Thread Michele Simionato
On Dec 10, 11:04 am, "Diez B. Roggisch" wrote: > Are you sure? For me on python2.5, it works as advertised: > > from __future__ import with_statement > > def test(outf): >     with outf: >         outf.write("test\n") >     try: >         outf.write("test\n") >         assert False, "Not closed" >

Re: "file" does not work with the "with" statement!

2009-12-10 Thread Diez B. Roggisch
Michele Simionato wrote: > I have just discovered that the syntax > > with file(name, 'w') as f: >do_something(f) > > does not close the file at the end of the with statement! On the > contrary > > with open(name, 'w') as f: >do_something(f) > > works fine. The docs say "When opening a

Re: "file" does not work with the "with" statement!

2009-12-10 Thread Peter Otten
Michele Simionato wrote: > I have just discovered that the syntax > > with file(name, 'w') as f: >do_something(f) > > does not close the file at the end of the with statement! On the > contrary > > with open(name, 'w') as f: >do_something(f) > > works fine. The docs say "When opening a