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