Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/airflow/utils/yaml.py: 44%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements. See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership. The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License. You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied. See the License for the
15# specific language governing permissions and limitations
16# under the License.
17"""
18Use libyaml for YAML dump/load operations where possible.
20If libyaml is available we will use it -- it is significantly faster.
22This module delegates all other properties to the yaml module, so it can be used as:
24.. code-block:: python
26 import airflow.utils.yaml as yaml
28And then be used directly in place of the normal python module.
29"""
31from __future__ import annotations
33from typing import TYPE_CHECKING, Any, BinaryIO, TextIO, cast
35if TYPE_CHECKING:
36 from yaml.error import MarkedYAMLError, YAMLError # noqa: F401
39def safe_load(stream: bytes | str | BinaryIO | TextIO) -> Any:
40 """Like yaml.safe_load, but use the C libyaml for speed where we can."""
41 # delay import until use.
42 from yaml import load as orig
44 try:
45 from yaml import CSafeLoader as SafeLoader
46 except ImportError:
47 from yaml import SafeLoader # type: ignore[assignment]
49 return orig(stream, SafeLoader)
52def dump(data: Any, **kwargs) -> str:
53 """Like yaml.safe_dump, but use the C libyaml for speed where we can."""
54 # delay import until use.
55 from yaml import dump as orig
57 try:
58 from yaml import CSafeDumper as SafeDumper
59 except ImportError:
60 from yaml import SafeDumper # type: ignore[assignment]
62 return cast("str", orig(data, Dumper=SafeDumper, **kwargs))
65def __getattr__(name):
66 # Delegate anything else to the yaml module
67 import yaml
69 if name == "FullLoader":
70 # Try to use CFullLoader by default
71 getattr(yaml, "CFullLoader", yaml.FullLoader)
73 return getattr(yaml, name)