PyYAML-ft (Free-Threading)

8.0.0 · active · verified Thu Apr 09

PyYAML-ft is a fork of the popular PyYAML library, providing a full-featured YAML parser and emitter for Python. Its primary purpose is to add support for free-threading builds of CPython, specifically targeting Python 3.13 and newer, where the Global Interpreter Lock (GIL) is absent. This addresses thread-safety issues that are more easily triggered in free-threaded environments and ensures ABI compatibility. The library is currently at version 8.0.0 and maintains an active release cadence in sync with free-threading Python developments.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load and dump YAML data using `pyyaml-ft`. It includes a conditional import for compatibility and uses `safe_load`/`safe_dump` for secure processing of YAML data.

import io

try:
    import yaml_ft as yaml
except ModuleNotFoundError:
    # Fallback or for environments where pyyaml-ft isn't the primary YAML lib
    import yaml 

# Sample YAML data
yaml_data = """
name: Alice
age: 30
occupations:
  - Software Engineer
  - Technical Writer
is_active: true
"""

# Load YAML data
data = yaml.safe_load(yaml_data)
print("Loaded Data:", data)

# Modify data
data['age'] = 31
data['occupations'].append('Open Source Contributor')

# Dump data to YAML string
output_stream = io.StringIO()
yaml.safe_dump(data, output_stream, default_flow_style=False)
print("\nDumped YAML:")
print(output_stream.getvalue())

print(f"\nSuccessfully used {yaml.__name__} for YAML operations.")

view raw JSON →