[Sugar-devel] [PATCH 3/7] metadatastore: store/change files on disk defensively #2317

Martin Langhoff martin.langhoff at gmail.com
Fri Sep 21 11:43:48 EDT 2012

On Thu, Sep 20, 2012 at 11:55 PM, Martin Langhoff <martin at laptop.org> wrote:
> -            f = open(os.path.join(metadata_path, key), 'w')
> -            try:
> -                if isinstance(value, unicode):
> -                    value = value.encode('utf-8')
> -                elif not isinstance(value, basestring):
> -                    value = str(value)
> -                f.write(value)
> -            finally:
> -                f.close()

BTW, I simplified the structure of

  f = open(path, 'w')

because in practice, f.write() writes into a buffer, so it never
fails. It is open() and close() that throw exceptions. In fact, at
ENOSPC, close() was failing.

And the use of finally is misguided. In my code, which wraps
open/write/close in a single try, if f.write() does throw an
exception, Python will close the FH when it goes out of scope.
Simpler, saner.

Python won't leak FHs on you. (IOWs, it takes some work to leak FHs on
any modern scripting language).


 martin.langhoff at gmail.com
 martin at laptop.org -- Software Architect - OLPC
 - ask interesting questions
 - don't get distracted with shiny stuff  - working code first
 - http://wiki.laptop.org/go/User:Martinlanghoff

More information about the Sugar-devel mailing list