[Python-Dev] The desired behaviour for resolve() when the path doesn't exist

Serhiy Storchaka storchaka at gmail.com
Tue Jan 7 16:26:20 CET 2014


06.01.14 12:38, Vajrasky Kok написав(ла):
> This is related with ticket 19717: "resolve() fails when the path
> doesn't exist".
>
> Assuming /home/cutecat exists but not /home/cutecat/aa,
>
> what is the desired output of
> Path('/home/cutecat/aa/bb/cc').resolve(strict=False)?
>
> Should it be:
>
> "/home/cutecat" (the existed path only),
> "/home/cutecat/aa" (the first non-existed path; my current strategy), or
> "/home/cutecat/aa/bb/cc" (the default behaviour of os.path.realpath)?

The readlink command has three canonicalize modes

`-f'
`--canonicalize'
      Activate canonicalize mode.  If any component of the file name
      except the last one is missing or unavailable, `readlink' produces
      no output and exits with a nonzero exit code.  A trailing slash is
      ignored.

`-e'
`--canonicalize-existing'
      Activate canonicalize mode.  If any component is missing or
      unavailable, `readlink' produces no output and exits with a
      nonzero exit code.  A trailing slash requires that the name
      resolve to a directory.

`-m'
`--canonicalize-missing'
      Activate canonicalize mode.  If any component is missing or
      unavailable, `readlink' treats it as a directory.

Behavior of os.path.realpath() is equivalent to --canonicalize-missing. 
Current behavior of pathlib.Path.resolve() is equivalent to 
--canonicalize-existing.

Behavior of --canonicalize-existing can be derived from --canonicalize, 
just check that resulting patch exists. But other modes can't be derived 
from --canonicalize-existing.

def resolve_existing(path):
     path = path.resolve()
     if not path.exists():
         raise FileNotFoundError(errno.ENOENT, 'No such file or 
directory: %r' % str(path))
     return path

So perhaps two main modes should be --canonicalize (default) and 
--canonicalize-missing (with missing=True)?



More information about the Python-Dev mailing list