diff options
author | Daniel Mensinger <daniel@mensinger-ka.de> | 2020-09-29 17:36:00 +0200 |
---|---|---|
committer | Daniel Mensinger <daniel@mensinger-ka.de> | 2020-10-04 10:45:48 +0200 |
commit | 1dfaccfd9129aa4ac24b46f0a9490b19ae59adc0 (patch) | |
tree | 466311902843db8865967e23bfbcbd5c1859cb04 /mesonbuild/_pathlib.py | |
parent | 77b5c82d0725eb914aa341a617dd9f418de60910 (diff) | |
download | meson-1dfaccfd9129aa4ac24b46f0a9490b19ae59adc0.zip meson-1dfaccfd9129aa4ac24b46f0a9490b19ae59adc0.tar.gz meson-1dfaccfd9129aa4ac24b46f0a9490b19ae59adc0.tar.bz2 |
pathlib: Fix resolve() by overriding it in Python 3.5
Diffstat (limited to 'mesonbuild/_pathlib.py')
-rw-r--r-- | mesonbuild/_pathlib.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/mesonbuild/_pathlib.py b/mesonbuild/_pathlib.py new file mode 100644 index 0000000..aac2a1b --- /dev/null +++ b/mesonbuild/_pathlib.py @@ -0,0 +1,49 @@ +# Copyright 2020 The Meson development team + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys +import typing as T + +# Python 3.5 does not have the strict kwarg for resolve and always +# behaves like calling resolve with strict=True in Python 3.6+ +# +# This module emulates the behavior of Python 3.6+ by in Python 3.5 by +# overriding the resolve method with a bit of custom logic +# +# TODO: Drop this module as soon as Python 3.5 support is dropped + +if T.TYPE_CHECKING: + from pathlib import Path +else: + if sys.version_info.major <= 3 and sys.version_info.minor <= 5: + + # Inspired by https://codereview.stackexchange.com/questions/162426/subclassing-pathlib-path + import pathlib + import os + + # Can not directly inherit from pathlib.Path because the __new__ + # operator of pathlib.Path() returns a {Posix,Windows}Path object. + class Path(type(pathlib.Path())): + def resolve(self, strict: bool = False) -> 'Path': + try: + return super().resolve() + except FileNotFoundError: + if strict: + raise + return Path(os.path.normpath(str(self))) + + else: + from pathlib import Path + +from pathlib import PurePath, PureWindowsPath, PurePosixPath |