Skip to content

Commit

Permalink
pkgconfig: Use ImmutableListProtocol[str] for cached lists
Browse files Browse the repository at this point in the history
This ensures that we are not modifying lists from lru cache.
  • Loading branch information
xclaesse committed Sep 18, 2023
1 parent f1c35b5 commit 8cb0217
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions mesonbuild/dependencies/pkgconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

if T.TYPE_CHECKING:
from typing_extensions import Literal
from .._typing import ImmutableListProtocol

from ..environment import Environment
from ..utils.core import EnvironOrDict
Expand Down Expand Up @@ -95,14 +96,14 @@ def version(self, name: str) -> T.Optional[str]:
raise NotImplementedError

def cflags(self, name: str, allow_system: bool = False,
define_variable: PkgConfigDefineType = None) -> T.List[str]:
define_variable: PkgConfigDefineType = None) -> ImmutableListProtocol[str]:
'''Return module cflags
@allow_system: If False, remove default system include paths
'''
raise NotImplementedError

def libs(self, name: str, static: bool = False, allow_system: bool = False,
define_variable: PkgConfigDefineType = None) -> T.List[str]:
define_variable: PkgConfigDefineType = None) -> ImmutableListProtocol[str]:
'''Return module libs
@static: If True, also include private libraries
@allow_system: If False, remove default system libraries search paths
Expand All @@ -114,7 +115,7 @@ def variable(self, name: str, variable_name: str,
'''Return module variable or None if variable is not defined'''
raise NotImplementedError

def list_all(self) -> T.List[str]:
def list_all(self) -> ImmutableListProtocol[str]:
'''Return all available pkg-config modules'''
raise NotImplementedError

Expand Down Expand Up @@ -144,7 +145,7 @@ def _define_variable_args(define_variable: PkgConfigDefineType) -> T.List[str]:

@lru_cache(maxsize=None)
def cflags(self, name: str, allow_system: bool = False,
define_variable: PkgConfigDefineType = None) -> T.List[str]:
define_variable: PkgConfigDefineType = None) -> ImmutableListProtocol[str]:
env = None
if allow_system:
env = os.environ.copy()
Expand All @@ -159,7 +160,7 @@ def cflags(self, name: str, allow_system: bool = False,

@lru_cache(maxsize=None)
def libs(self, name: str, static: bool = False, allow_system: bool = False,
define_variable: PkgConfigDefineType = None) -> T.List[str]:
define_variable: PkgConfigDefineType = None) -> ImmutableListProtocol[str]:
env = None
if allow_system:
env = os.environ.copy()
Expand Down Expand Up @@ -194,7 +195,7 @@ def variable(self, name: str, variable_name: str,
return variable

@lru_cache(maxsize=None)
def list_all(self) -> T.List[str]:
def list_all(self) -> ImmutableListProtocol[str]:
ret, out, err = self._call_pkgbin(['--list-all'])
if ret != 0:
raise DependencyException(f'could not list modules:\n{err}\n')
Expand Down Expand Up @@ -319,15 +320,15 @@ def __repr__(self) -> str:
return s.format(self.__class__.__name__, self.name, self.is_found,
self.version_reqs)

def _convert_mingw_paths(self, args: T.List[str]) -> T.List[str]:
def _convert_mingw_paths(self, args: ImmutableListProtocol[str]) -> T.List[str]:
'''
Both MSVC and native Python on Windows cannot handle MinGW-esque /c/foo
paths so convert them to C:/foo. We cannot resolve other paths starting
with / like /home/foo so leave them as-is so that the user gets an
error/warning from the compiler/linker.
'''
if not self.env.machines.build.is_windows():
return args
return args.copy()
converted = []
for arg in args:
pargs: T.Tuple[str, ...] = tuple()
Expand Down Expand Up @@ -359,7 +360,7 @@ def _set_cargs(self) -> None:
cflags = self.pkgconfig.cflags(self.name, allow_system)
self.compile_args = self._convert_mingw_paths(cflags)

def _search_libs(self, libs_in: T.List[str], raw_libs_in: T.List[str]) -> T.Tuple[T.List[str], T.List[str]]:
def _search_libs(self, libs_in: ImmutableListProtocol[str], raw_libs_in: ImmutableListProtocol[str]) -> T.Tuple[T.List[str], T.List[str]]:
'''
@libs_in: PKG_CONFIG_ALLOW_SYSTEM_LIBS=1 pkg-config --libs
@raw_libs_in: pkg-config --libs
Expand Down

0 comments on commit 8cb0217

Please sign in to comment.