Skip to content

Commit fc9fd42

Browse files
bonzinijpakkane
authored andcommitted
interpreter: do not use pathlib for DependencyVariableString creation
Path.is_dir() can raise a PermissionError if a parent does not have the executable permission set; plus the "in p.parents" tests are very expensive. Do not use Path at all. Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 475bfba commit fc9fd42

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

mesonbuild/interpreter/interpreter.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -704,20 +704,18 @@ def func_declare_dependency(self, node: mparser.BaseNode, args: T.List[TYPE_var]
704704
version = self.project_version
705705
d_module_versions = kwargs['d_module_versions']
706706
d_import_dirs = self.extract_incdirs(kwargs, 'd_import_dirs')
707-
srcdir = Path(self.environment.source_dir)
707+
srcdir = self.environment.source_dir
708+
subproject_dir = os.path.abspath(os.path.join(srcdir, self.subproject_dir))
709+
project_root = os.path.abspath(os.path.join(srcdir, self.root_subdir))
708710
# convert variables which refer to an -uninstalled.pc style datadir
709711
for k, v in variables.items():
710712
if not v:
711713
FeatureNew.single_use('empty variable value in declare_dependency', '1.4.0', self.subproject, location=node)
712-
try:
713-
p = Path(v)
714-
except ValueError:
715-
continue
716-
else:
717-
if not self.is_subproject() and srcdir / self.subproject_dir in p.parents:
718-
continue
719-
if p.is_absolute() and p.is_dir() and srcdir / self.root_subdir in [p] + list(Path(os.path.abspath(p)).parents):
720-
variables[k] = P_OBJ.DependencyVariableString(v)
714+
if os.path.isabs(v) \
715+
and (self.is_subproject() or not is_parent_path(subproject_dir, v)) \
716+
and is_parent_path(project_root, v) \
717+
and os.path.isdir(v):
718+
variables[k] = P_OBJ.DependencyVariableString(v)
721719

722720
dep = dependencies.InternalDependency(version, incs, compile_args,
723721
link_args, libs, libs_whole, sources, extra_files,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
project('foo')
2+
3+
declare_dependency(
4+
variables: {
5+
'dir': get_option('dir')
6+
}
7+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
option('dir', type: 'string')

unittests/linuxliketests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,18 @@ def test_top_options_in_sp(self):
18621862
testdir = os.path.join(self.unit_test_dir, '125 pkgsubproj')
18631863
self.init(testdir)
18641864

1865+
def test_unreadable_dir_in_declare_dep(self):
1866+
testdir = os.path.join(self.unit_test_dir, '125 declare_dep var')
1867+
tmpdir = Path(tempfile.mkdtemp())
1868+
self.addCleanup(windows_proof_rmtree, tmpdir)
1869+
declaredepdir = tmpdir / 'test'
1870+
declaredepdir.mkdir()
1871+
try:
1872+
tmpdir.chmod(0o444)
1873+
self.init(testdir, extra_args=f'-Ddir={declaredepdir}')
1874+
finally:
1875+
tmpdir.chmod(0o755)
1876+
18651877
def check_has_flag(self, compdb, src, argument):
18661878
for i in compdb:
18671879
if src in i['file']:

0 commit comments

Comments
 (0)