Skip to content

Comments

Fix TypeError in prepare_tag_prefix on non-ASCII tag prefixes (Python 3)#917

Open
bysiber wants to merge 1 commit intoyaml:mainfrom
bysiber:fix/prepare-tag-prefix-ord-py3
Open

Fix TypeError in prepare_tag_prefix on non-ASCII tag prefixes (Python 3)#917
bysiber wants to merge 1 commit intoyaml:mainfrom
bysiber:fix/prepare-tag-prefix-ord-py3

Conversation

@bysiber
Copy link

@bysiber bysiber commented Feb 20, 2026

Summary

prepare_tag_prefix crashes with a TypeError when given a tag prefix containing non-ASCII characters (e.g., Unicode) in Python 3.

Problem

In Python 3, iterating over bytes yields int values, not single-byte bytes objects. The percent-encoding loop in prepare_tag_prefix calls ord(ch) on these integer values, which raises TypeError:

data = ch.encode('utf-8')
for ch in data:
    chunks.append('%%%02X' % ord(ch))  # TypeError: ord() expected string of length 1, but int found

The sibling method prepare_tag (just below on line ~607) handles this correctly with % ch instead of % ord(ch).

Reproduction

import yaml, io
from yaml.emitter import Emitter

stream = io.StringIO()
e = Emitter(stream)
e.prepare_tag_prefix('tag:ü,2002:')
# TypeError: ord() expected string of length 1, but int found

Fix

Drop the redundant ord() call to match the working prepare_tag implementation:

chunks.append('%%%02X' % ch)

In Python 3, iterating over bytes yields int values, not single-byte
bytes objects. Calling ord() on an int raises TypeError.

The sibling method prepare_tag already handles this correctly by using
'% ch' without ord(). Apply the same fix to prepare_tag_prefix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant