Avoid effects of YAML tags in get_deps

This commit is contained in:
Oleh Prypin
2023-06-06 23:13:21 +02:00
parent e175070243
commit 5de1273259
2 changed files with 22 additions and 3 deletions

View File

@@ -4,19 +4,38 @@ import dataclasses
import datetime
import functools
import logging
import sys
from typing import Mapping, Sequence
if sys.version_info >= (3, 10):
from importlib.metadata import EntryPoint, entry_points
else:
from importlib_metadata import EntryPoint, entry_points
import yaml
from mkdocs import utils
from mkdocs.config.base import _open_config_file
from mkdocs.plugins import EntryPoint, entry_points
from mkdocs.utils.cache import download_and_cache_url
log = logging.getLogger(__name__)
# Note: do not rely on functions in this module, it is not public API.
class YamlLoader(yaml.SafeLoader):
pass
# Prevent errors from trying to access external modules which may not be installed yet.
YamlLoader.add_constructor("!ENV", lambda loader, node: None) # type: ignore
YamlLoader.add_multi_constructor(
"tag:yaml.org,2002:python/name:", lambda loader, suffix, node: None
)
YamlLoader.add_multi_constructor(
"tag:yaml.org,2002:python/object/apply:", lambda loader, suffix, node: None
)
NotFound = ()
@@ -76,7 +95,7 @@ def get_deps(projects_file_url: str, config_file_path: str | None = None) -> Non
config_file_path: Non-default path to mkdocs.yml.
"""
with _open_config_file(config_file_path) as f:
cfg = utils.yaml_load(f)
cfg = utils.yaml_load(f, loader=YamlLoader) # type: ignore
if all(c not in cfg for c in ('site_name', 'theme', 'plugins', 'markdown_extensions')):
log.warning("The passed config file doesn't seem to be a mkdocs.yml config file")

View File

@@ -63,7 +63,7 @@ def get_yaml_loader(loader=yaml.Loader):
return Loader
def yaml_load(source: IO | str, loader: type[yaml.Loader] | None = None) -> dict[str, Any]:
def yaml_load(source: IO | str, loader: type[yaml.BaseLoader] | None = None) -> dict[str, Any]:
"""Return dict of source YAML file using loader, recursively deep merging inherited parent."""
Loader = loader or get_yaml_loader()
result = yaml.load(source, Loader=Loader)