PyYML (Python in YAML)

0.0.2 · abandoned · verified Wed Apr 15

This library, `pyyml` (version 0.0.2), aims to integrate Python code execution directly within YAML documents. Released in 2019, it appears to be an unmaintained project with its last release several years ago, focusing on enabling Python names and expressions to be evaluated during YAML loading. This functionality, while seemingly powerful, introduces significant security vulnerabilities, as arbitrary Python code can be executed from untrusted YAML sources. It is distinct from the widely used and actively maintained `PyYAML` library.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load a YAML string containing Python evaluation tags (`!!python/eval`) and name references (`!!python/name`) using `pyyml`'s `PythonLoader`. It also shows a basic example of dumping Python data using `PythonDumper`.

import yaml
from pyyml.pyyml import PythonLoader, PythonDumper

# Example YAML with Python code (!!python/eval and !!python/name)
yaml_string = """
message: !!python/eval "'Hello, ' + 'World!'"
version_info: !!python/name 'sys.version_info'
calculate: !!python/eval "lambda x, y: x + y"
"""

# Load the YAML using PythonLoader
data = yaml.load(yaml_string, Loader=PythonLoader)

print(f"Message: {data['message']}")
print(f"Python Version Info: {data['version_info']}")
print(f"Calculation (5 + 3): {data['calculate'](5, 3)}")

# Example of dumping (if PythonDumper is used for custom types)
python_data = {
    'my_list': [1, 2, 3],
    'my_tuple': (4, 5),
    'my_set': {6, 7}
}
# Note: PythonDumper may not handle all arbitrary Python objects without custom constructors/representers.
# For simple types, it behaves like SafeDumper.
dumped_yaml = yaml.dump(python_data, Dumper=PythonDumper, default_flow_style=False)
print("\nDumped YAML:")
print(dumped_yaml)

view raw JSON →