From fee37747c51b7f6da22aab942a5052f20dcaf044 Mon Sep 17 00:00:00 2001 From: ShipItAndPray Date: Sun, 26 Apr 2026 21:53:37 -0500 Subject: [PATCH] Fix percent-escaped tag prefixes in yaml.emit --- lib/yaml/emitter.py | 4 ++-- tests/test_emit.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 tests/test_emit.py diff --git a/lib/yaml/emitter.py b/lib/yaml/emitter.py index a664d0111..79c13d925 100644 --- a/lib/yaml/emitter.py +++ b/lib/yaml/emitter.py @@ -571,8 +571,8 @@ def prepare_tag_prefix(self, prefix): chunks.append(prefix[start:end]) start = end = end+1 data = ch.encode('utf-8') - for ch in data: - chunks.append('%%%02X' % ord(ch)) + for byte in data: + chunks.append('%%%02X' % byte) if start < end: chunks.append(prefix[start:end]) return ''.join(chunks) diff --git a/tests/test_emit.py b/tests/test_emit.py new file mode 100644 index 000000000..fe3016989 --- /dev/null +++ b/tests/test_emit.py @@ -0,0 +1,12 @@ +import io + +import yaml + + +def test_emit_tag_directive_with_percent_encoded_prefix_from_bytes(): + events = list(yaml.parse(io.BytesIO(b"%TAG ! tag:%002:\n---"))) + + output = yaml.emit(events) + + assert output == "%TAG ! tag:%002:\n---\n...\n" + assert list(yaml.parse(output))[1].tags == events[1].tags