[PATCH 4 of 8] opener: add read & write utility methods

Augie Fackler durin42 at gmail.com
Fri Dec 24 22:21:53 CST 2010


On Dec 24, 2010, at 7:35 PM, Dan Villiom Podlaski Christiansen wrote:
> 
> # HG changeset patch
> # User Dan Villiom Podlaski Christiansen <danchr at gmail.com>
> # Date 1293200581 -3600
> # Node ID 12f7ef8071592cb1e071ea1c8d2dfa801540a578
> # Parent  39338cacd4f04935cd7f2d1b5639fd7ce8ca71a6
> opener: add read & write utility methods
> 
> The two new methods are useful for quickly opening a file for reading
> or writing. Unlike 'opener(...).read()', they ensure they the file is
> immediately closed without relying on CPython reference counting.
> 
> Similar methods are added to custom openers.
> 
> diff --git a/mercurial/statichttprepo.py b/mercurial/statichttprepo.py
> --- a/mercurial/statichttprepo.py
> +++ b/mercurial/statichttprepo.py
> @@ -75,6 +75,7 @@ def build_opener(ui, authinfo):
>                 raise IOError('Permission denied')
>             f = "/".join((p, urllib.quote(path)))
>             return httprangereader(f, urlopener)
> +        o.read = lambda *args, **kwargs: o(*args, **kwargs).read()
>         return o
> 
>     opener.options = {'nonlazy': 1}
> diff --git a/mercurial/store.py b/mercurial/store.py
> --- a/mercurial/store.py
> +++ b/mercurial/store.py
> @@ -176,6 +176,8 @@ class basicstore(object):
>         op = opener(self.path)
>         op.createmode = self.createmode
>         self.opener = lambda f, *args, **kw: op(encodedir(f), *args, **kw)
> +        self.opener.read = (lambda f, *args, **kw:
> +                            op(encodedir(f), *args, **kw).read())

How does this ensure the file is closed properly? Isn't this identical to doing self.opener().read()? (Note: it's a bit late and it's probable that I'm missing something obvious, but I feel like a comment might be warranted anyway for clarity.

> 
>     def join(self, f):
>         return self.pathjoiner(self.path, encodedir(f))
> @@ -221,6 +223,8 @@ class encodedstore(basicstore):
>         op = opener(self.path)
>         op.createmode = self.createmode
>         self.opener = lambda f, *args, **kw: op(encodefilename(f), *args, **kw)
> +        self.opener.read = (lambda f, *args, **kw:
> +                            op(encodefilename(f), *args, **kw).read())

Same here.

> 
>     def datafiles(self):
>         for a, b, size in self._walk('data', True):
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -879,6 +879,20 @@ class opener(object):
>             return
>         os.chmod(name, self.createmode & 0666)
> 
> +    def read(self, *args, **kwargs):
> +        fp = self(*args, **kwargs)
> +        try:
> +            return fp.read()
> +        finally:
> +            fp.close()
> +
> +    def write(self, data, *args, **kwargs):
> +        fp = self(*args, **kwargs)
> +        try:
> +            return fp.write(data)
> +        finally:
> +            fp.close()
> +
>     def __call__(self, path, mode="r", text=False, atomictemp=False):
>         self.auditor(path)
>         f = os.path.join(self.base, path)
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel




More information about the Mercurial-devel mailing list