Modify

Ticket #136 (closed defect: invalid)

Opened 5 years ago

Last modified 5 years ago

Pure-python emitter fails emitting tag represented as a mapping

Reported by: julians@… Owned by: xi
Priority: normal Component: pyyaml
Severity: normal Keywords:
Cc:

Description

Hi,

the following snippet will fail with PyYAML 3.08:

#!/usr/bin/env python2.5

import yaml

Dumper = yaml.Dumper

class FooTag(yaml.YAMLObject):

    yaml_dumper = Dumper

    yaml_tag = u'!foo'

    @classmethod
    def to_yaml(cls, dumper, foo):
        return dumper.represent_mapping(None, 
                                        {"foo": "bar"} )

print(yaml.dump(FooTag(), Dumper=Dumper))

The error message is:

Traceback (most recent call last):
  File "tag_not_specified.py", line 20, in <module>
    print(yaml.dump(FooTag(), Dumper=Dumper))
  File "(bogus file name)", line 175, in dump
  File "(bogus file name)", line 165, in dump_all
  File "yaml/representer.py", line 34, in represent
  File "yaml/serializer.py", line 54, in serialize
  File "yaml/serializer.py", line 109, in serialize_node
  File "yaml/emitter.py", line 110, in emit
  File "yaml/emitter.py", line 224, in expect_document_root
  File "yaml/emitter.py", line 238, in expect_node
  File "yaml/emitter.py", line 483, in process_tag
yaml.emitter.EmitterError: tag is not specified

(For some reason the file name in lines two and three of the trace is wrong, I've removed it here.)

The above does work when using libyaml (try substituting CDumper). For what it's worth it will also work when to_yaml emits a scalar instead of a mapping.

I've found that the following patch fixes the issue for me, but I don't understand the code well enough to know all implications of doing so.

diff -ru PyYAML-3.08/lib/yaml/emitter.py PyYAML-3.08-patched/lib/yaml/emitter.py
--- PyYAML-3.08/lib/yaml/emitter.py	2008-12-31 05:24:15.000000000 +1030
+++ PyYAML-3.08-patched/lib/yaml/emitter.py	2009-07-03 13:58:11.000000000 +0930
@@ -480,7 +480,7 @@

                 self.prepared_tag = None
                 return
         if tag is None:
-            raise EmitterError(&quot;tag is not specified&quot;)
+            return
         if self.prepared_tag is None:
             self.prepared_tag = self.prepare_tag(tag)
         if self.prepared_tag:

Attachments

Change History

comment:1 Changed 5 years ago by xi

  • Status changed from new to closed
  • Resolution set to invalid

Replace dumper.represent_mapping(None, {"foo": "bar"} ) with dumper.represent_mapping(cls.yaml_tag, {"foo": "bar"} ).

The tag should always be specified. If there is a bug, then it is the fact that None tag is sometimes accepted without an error.

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.