PyYAML-ft (Free-Threading)
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
- breaking The primary module name has changed from `yaml` to `yaml_ft` starting with v8.0.0. Direct `import yaml` will fail if only `pyyaml-ft` is installed.
- breaking Custom constructors, representers, and resolvers (`add_constructor`, `add_representer`, `add_implicit_resolver`, `add_path_resolver`) now use thread-local registries. These must be explicitly re-registered in each new thread if used in multithreaded applications.
- gotcha PyYAML-ft is primarily designed for Python 3.13 and newer, especially free-threaded builds. While it might install on older Python versions, its free-threading benefits are specific to Python 3.13+ environments.
- gotcha Using `yaml.load()` (or `yaml_ft.load()`) with untrusted input can lead to arbitrary code execution, as it can deserialize arbitrary Python objects. This is a well-known security vulnerability in PyYAML and applies to PyYAML-ft.
Install
-
pip install pyyaml-ft
Imports
- yaml_ft
import yaml_ft
- safe_load, safe_dump
from yaml_ft import safe_load, safe_dump
Quickstart
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.")