|
| 1 | +# Copyright 2020 The Meson development team |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | + |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import sys |
| 16 | +import typing as T |
| 17 | + |
| 18 | +# Python 3.5 does not have the strict kwarg for resolve and always |
| 19 | +# behaves like calling resolve with strict=True in Python 3.6+ |
| 20 | +# |
| 21 | +# This module emulates the behavior of Python 3.6+ by in Python 3.5 by |
| 22 | +# overriding the resolve method with a bit of custom logic |
| 23 | +# |
| 24 | +# TODO: Drop this module as soon as Python 3.5 support is dropped |
| 25 | + |
| 26 | +if T.TYPE_CHECKING: |
| 27 | + from pathlib import Path |
| 28 | +else: |
| 29 | + if sys.version_info.major <= 3 and sys.version_info.minor <= 5: |
| 30 | + |
| 31 | + # Inspired by https://codereview.stackexchange.com/questions/162426/subclassing-pathlib-path |
| 32 | + import pathlib |
| 33 | + import os |
| 34 | + |
| 35 | + # Can not directly inherit from pathlib.Path because the __new__ |
| 36 | + # operator of pathlib.Path() returns a {Posix,Windows}Path object. |
| 37 | + class Path(type(pathlib.Path())): |
| 38 | + def resolve(self, strict: bool = False) -> 'Path': |
| 39 | + try: |
| 40 | + return super().resolve() |
| 41 | + except FileNotFoundError: |
| 42 | + if strict: |
| 43 | + raise |
| 44 | + return Path(os.path.normpath(str(self))) |
| 45 | + |
| 46 | + else: |
| 47 | + from pathlib import Path |
| 48 | + |
| 49 | +from pathlib import PurePath, PureWindowsPath, PurePosixPath |
0 commit comments