[PATCH STABLE] opener: raise IOError on paths ending in '/'

Martin Geisler mg at lazybytes.net
Sat Oct 30 07:40:43 CDT 2010


Adrian Buehlmann <adrian at cadifra.com> writes:

> # HG changeset patch
> # User Adrian Buehlmann <adrian at cadifra.com>
> # Date 1288433569 -7200
> # Branch stable
> # Node ID 143169d6df8797ea1f4c9df69c3cbf7caf484386
> # Parent  15ca4bfecfe343cbf53210b19c081676b2a35d3f
> opener: raise IOError on paths ending in '/'
>
> This improves 551aa6e27929 and 1634287b6ab1 by making sure we don't
> call any further functions (e.g. atomictempfile) if we already know
> that the path is not a valid file path.

Nice -- I considered doing something like this by raising an exception
at the very beginning of the function if it's asked to open a directory.
That would cover opening for both reading and writing.

In the end I didn't raise the exception there since I was unsure if open
would always raise it with errno 21 so I preferred to let it raise the
exception. But now that I'm reminded of the errno and os.strerror
functions that indeed seems better, especially if we move it up to the
beginning of the function.

> It's also better not to call nlinks() for such malformed paths, so
> let's move the path test out of the OSError exception handler.
>
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -859,14 +859,15 @@ class opener(object):
>  
>          nlink = -1
>          if mode not in ("r", "rb"):
> +            dirname, basename = os.path.split(f)
> +            if not basename:
> +                code = errno.EISDIR
> +                raise IOError(code, os.strerror(code))
>              try:
>                  nlink = nlinks(f)
>              except OSError:
>                  nlink = 0
> -                dirname, basename = os.path.split(f)
> -                # Avoid calling makedirs when the path points to a
> -                # directory -- the open will raise IOError below.
> -                if basename and not os.path.isdir(dirname):
> +                if not os.path.isdir(dirname):
>                      makedirs(dirname, self.createmode)
>              if atomictemp:
>                  return atomictempfile(f, mode, self.createmode)
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel

-- 
Martin Geisler

Mercurial links: http://mercurial.ch/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://selenic.com/pipermail/mercurial-devel/attachments/20101030/ee1e845a/attachment.pgp>


More information about the Mercurial-devel mailing list