Skip to content

[pyyaml 3.13] TypeError while dumping lambda in Python 3 #222

@NiklasRosenstein

Description

@NiklasRosenstein
import yaml
print(yaml.dump(lambda: None))

This fails with a TypeError in Python 3 (not in Python 2).

[...]
  File "/Users/nrosenstein/Projects/.venv/lib/python3.7/site-packages/yaml/emitter.py", line 228, in expect_document_root
    self.expect_node(root=True)
  File "/Users/nrosenstein/Projects/.venv/lib/python3.7/site-packages/yaml/emitter.py", line 242, in expect_node
    self.process_tag()
  File "/Users/nrosenstein/Projects/.venv/lib/python3.7/site-packages/yaml/emitter.py", line 489, in process_tag
    self.prepared_tag = self.prepare_tag(tag)
  File "/Users/nrosenstein/Projects/.venv/lib/python3.7/site-packages/yaml/emitter.py", line 607, in prepare_tag
    for ch in data:
TypeError: ord() expected string of length 1, but int found

For reference:

pyyaml/lib3/yaml/emitter.py

Lines 569 to 575 in a2d481b

else:
if start < end:
chunks.append(prefix[start:end])
start = end = end+1
data = ch.encode('utf-8')
for ch in data:
chunks.append('%%%02X' % ord(ch))

On a side note, I don't know how the line numbers don't match. I have PyYAML 3.13 installed and this references the commit on the 3.13 tag.

str.encode() returns a bytes object in Python 3, which in turn yields integers and no substrings when iterated over, leading ord() to fail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions