[PATCH 3 of 3] util.makedirs: cleanup of termination of recursion

Adrian Buehlmann adrian at cadifra.com
Thu Aug 25 01:08:56 CDT 2011


On 2011-08-24 23:39, Mads Kiilerich wrote:
> 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.

I already pasted my patch inline and was waiting for your opinion on my
arguments.

But I surely can send it as a proper patch (not inline).

> If you don't do it then I will do it my way ;-)

Even if I have a slight preference for my way, your way would already be
better than the status quo.

So whatever Matt takes (or you push), it will be an improvement.


More information about the Mercurial-devel mailing list