[PATCH 3 of 3] util.makedirs: cleanup of termination of recursion
Mads Kiilerich
mads at kiilerich.com
Wed Aug 24 16:39:39 CDT 2011
Adrian Buehlmann wrote, On 08/22/2011 02:55 PM:
> I have a slight preference for my latest version, on the grounds that
> it's best to check and raise for as much as we can before reaching for
> more library calls (os.*).
>
> That is, I prefer
>
> def makedirs(name, mode=None):
> """recursive directory creation with parent mode inheritance"""
> try:
> os.mkdir(name)
> except OSError, err:
> if err.errno == errno.EEXIST:
> return
> if err.errno != errno.ENOENT or not name:
> raise
> parent = os.path.dirname(os.path.abspath(name))
> if parent == name:
> raise
> makedirs(parent, mode)
> os.mkdir(name)
> if mode is not None:
> os.chmod(name, mode)
>
> because it's better to have the
>
> if err.errno != errno.ENOENT or not name:
> raise
>
> check and raise done before reaching for more troubles by calling
> into the platform again with
>
> parent = os.path.dirname(os.path.abspath(name))
>
> It's not about speed, but more about correctness. Consider what happens
> if the abspath call would throw.
If you prefer it that way then please send a patch so you get proper
attribution.
If you don't do it then I will do it my way ;-)
/Mads
More information about the Mercurial-devel
mailing list