Skip to content

Commit 1dfaccf

Browse files
committed
pathlib: Fix resolve() by overriding it in Python 3.5
1 parent 77b5c82 commit 1dfaccf

40 files changed

+91
-40
lines changed

meson.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616

1717
import sys
18-
from pathlib import Path
18+
from mesonbuild._pathlib import Path
1919

2020
# If we're run uninstalled, add the script directory to sys.path to ensure that
2121
# we always import the correct mesonbuild modules even if PYTHONPATH is mangled

mesonbuild/_pathlib.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

mesonbuild/backend/backends.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from collections import OrderedDict
1616
from functools import lru_cache
17-
from pathlib import Path
17+
from .._pathlib import Path
1818
import enum
1919
import json
2020
import os
@@ -917,7 +917,7 @@ def generate_depmf_install(self, d):
917917
def get_regen_filelist(self):
918918
'''List of all files whose alteration means that the build
919919
definition needs to be regenerated.'''
920-
deps = [os.path.join(self.build_to_src, df)
920+
deps = [str(Path(self.build_to_src) / df)
921921
for df in self.interpreter.get_build_def_files()]
922922
if self.environment.is_cross_build():
923923
deps.extend(self.environment.coredata.cross_files)

mesonbuild/backend/ninjabackend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from collections import OrderedDict
2121
from enum import Enum, unique
2222
import itertools
23-
from pathlib import PurePath, Path
23+
from .._pathlib import PurePath, Path
2424
from functools import lru_cache
2525

2626
from . import backends

mesonbuild/backend/vs2010backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import xml.etree.ElementTree as ET
2020
import uuid
2121
import typing as T
22-
from pathlib import Path, PurePath
22+
from .._pathlib import Path, PurePath
2323

2424
from . import backends
2525
from .. import build
@@ -1000,7 +1000,7 @@ def gen_vcxproj(self, target, ofname, guid):
10001000
if inc_dir not in file_inc_dirs[l]:
10011001
file_inc_dirs[l].append(inc_dir)
10021002
# Add include dirs to target as well so that "Go to Document" works in headers
1003-
if inc_dir not in target_inc_dirs:
1003+
if inc_dir not in target_inc_dirs:
10041004
target_inc_dirs.append(inc_dir)
10051005

10061006
# Split compile args needed to find external dependencies

mesonbuild/cmake/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from .. import mlog
2222
from contextlib import contextmanager
2323
from subprocess import Popen, PIPE, TimeoutExpired
24-
from pathlib import Path
24+
from .._pathlib import Path
2525
import typing as T
2626
import json
2727

mesonbuild/cmake/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from ..mesonlib import MesonException
1919
from .. import mlog
20-
from pathlib import Path
20+
from .._pathlib import Path
2121
import typing as T
2222

2323
class CMakeException(MesonException):

mesonbuild/cmake/executor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# or an interpreter-based tool.
1717

1818
import subprocess as S
19-
from pathlib import Path
19+
from .._pathlib import Path
2020
from threading import Thread
2121
import typing as T
2222
import re

mesonbuild/cmake/fileapi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .common import CMakeException, CMakeBuildFile, CMakeConfiguration
1616
import typing as T
1717
from .. import mlog
18-
from pathlib import Path
18+
from .._pathlib import Path
1919
import json
2020
import re
2121

@@ -74,6 +74,7 @@ def load_reply(self) -> None:
7474

7575
# Debug output
7676
debug_json = self.build_dir / '..' / 'fileAPI.json'
77+
debug_json = debug_json.resolve()
7778
debug_json.write_text(json.dumps(index, indent=2))
7879
mlog.cmd_ci_include(debug_json.as_posix())
7980

mesonbuild/cmake/interpreter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from ..compilers.compilers import lang_suffixes, header_suffixes, obj_suffixes, lib_suffixes, is_header
2727
from enum import Enum
2828
from functools import lru_cache
29-
from pathlib import Path
29+
from .._pathlib import Path
3030
import typing as T
3131
import re
3232
from os import environ

0 commit comments

Comments
 (0)