Custom constructor should not clobber wrapped output

Issue #62 invalid
Anonymous created an issue

Here's a minimal example to reproduce my bug:

#!/usr/bin/env python3

import io, json, sys, yaml

# Comment this out to compare with the non-buggy case
yaml.add_constructor('!baz', lambda loader, node: node.value)

# Comment this out and uncomment the line after it to "fix" the bug
default_out = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
# default_out = sys.stdout

parsed = yaml.load('''
---
foo:
- bar''')
json.dump(parsed, default_out)
default_out.write('\n')

This program prints nothing. Commenting out the add_constructor line, or printing directly to sys.stdout, prints {"foo": ["bar"]} as it should.

Somehow, adding a custom constructor clobbers the I/O wrapper around stdout.

Comments (4)

  1. Philip Chimento

    But... I don't want to dump it as YAML. Adding the custom constructor just breaks that I/O wrapper even if I dump nothing. How about this minimal example?

    #!/usr/bin/env python3
    
    import io, sys, yaml
    
    # Comment this out to compare with the non-buggy case
    yaml.add_constructor('!baz', lambda loader, node: node.value)
    
    # Comment this out and uncomment the line after it to "fix" the bug
    default_out = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
    # default_out = sys.stdout
    
    default_out.write('Print me\n')
    

    Surely the above is a bug with yaml.add_constructor()?

  2. Log in to comment