PyYAML Documentation

repository·main·Indexed 7 days ago

https://github.com/yaml/pyyaml

A full-featured YAML processing framework for Python. It supports pure Python implementations and high-performance LibYAML bindings via CLoader and CDumper. The library provides various loading methods including safe_load(), full_load(), and unsafe_load() to handle different trust levels of input, as well as serialization tools like dump() and dump_all(). It allows for extensibility through custom constructors and representers, and provides a YAMLObject class for automated serialization.

Tokens
5.1K
Snippets
1
Records
47
Agent score
53%

What's inside PyYAML

  1. Run PyYAML tests

    main

    PyYAML provides a Makefile to run various test suites.

    • Run the complete suite (pure Python + LibYAML extension): make test
    • Run only pure Python tests: make test-python
    • Run only LibYAML extension tests: make test-libyaml
    • Test with a specific Python version: make test PYTHON-VERSION=<version>
  2. Install PyYAML

    main

    Install PyYAML using the setup.py script. By default, the script checks for LibYAML and installs bindings if found. You can explicitly control LibYAML bindings using the following flags:

    • Force installation of LibYAML bindings: --with-libyaml
    • Skip building/installing LibYAML bindings: --without-libyaml
    python setup.py install
    # To force LibYAML bindings:
    python setup.py --with-libyaml install
    # To skip LibYAML bindings:
    python setup.py --without-libyaml install
  3. Choose the appropriate Constructor class for security

    main

    PyYAML provides different constructor classes with varying levels of security and capability when converting YAML nodes into Python objects:

    1. SafeConstructor: The recommended choice for untrusted input. It only supports standard YAML tags (integers, floats, strings, sequences, maps, etc.) and does not allow the instantiation of arbitrary Python objects.
    2. FullConstructor: Inherits from SafeConstructor but adds support for some Python-specific types. It includes a blacklist to prevent certain attributes/methods from being set to mitigate security risks.
    3. UnsafeConstructor / Constructor: These allow full deserialization of arbitrary Python objects using tags like !!python/object/apply. Warning: Use these only with trusted data, as they can execute arbitrary code during deserialization.
  4. Troubleshoot ScannerError during quoted scalar scanning

    main

    A ScannerError may be raised when scanning quoted scalars (flow scalars) in the following scenarios:

    • Unexpected End of Stream: The stream ends before the closing quote is found.
    • Unexpected Document Separator: A document separator (--- or ...) is found within a quoted scalar.
    • Invalid Escape Sequence: In double-quoted scalars, an invalid hexadecimal escape sequence is encountered (expected 2, 4, or 8 hex digits depending on the prefix x, u, or U).
    • Unknown Escape Character: An unrecognized escape character is used in a double-quoted scalar.
  5. Troubleshoot ScannerError during block scalar scanning

    main

    When parsing YAML, a ScannerError may be raised during the scanning of block scalars. Common causes include:

    • Invalid Indentation Indicator: An indentation indicator of 0 is found (must be in the range 1-9).
    • Missing Indicators: Expected chomping (+ or -) or indentation indicators, but found an unexpected character.
    • Invalid Line Breaks: Expected a comment or a line break, but found an unexpected character.

    Errors include a start_mark and the current mark to help locate the problematic position in the YAML stream.

  6. Handle ConstructorError during deserialization

    main
    When YAML deserialization fails due to invalid tags, unhashable keys in mappings, or recursive nodes, PyYAML raises a ConstructorError. This error typically includes information about the location of the error in the YAML stream via a mark attribute.
  7. Troubleshoot ScannerError during URI scanning

    main

    When scanning a URI (e.g., in a tag), a ScannerError is raised if:

    • Missing URI: No valid URI characters are found where one was expected.
    • Invalid URI Escape Sequence: A % character is not followed by exactly 2 hexadecimal numbers.
    • Unicode Decode Error: A URI escape sequence results in an invalid UTF-8 byte sequence.
  8. Use SafeRepresenter for secure YAML serialization

    main
    The SafeRepresenter class provides a subset of YAML tags that are safe for serialization, avoiding the execution of arbitrary Python code. It handles standard Python types like None, str, bytes, bool, int, float, list, tuple, dict, set, datetime.date, and datetime.datetime using standard YAML tags.