Skip to content
This repository has been archived by the owner on Apr 2, 2019. It is now read-only.

YAML trouble #1

Open
olleolleolle opened this issue May 27, 2011 · 5 comments
Open

YAML trouble #1

olleolleolle opened this issue May 27, 2011 · 5 comments

Comments

@olleolleolle
Copy link

Traceback (most recent call last):
File "visu.py", line 17, in
projects = f.get_projects()
File "/usr/local/Cellar/python/2.7.1/lib/python2.7/site-packages/freckle-0.1-py2.7.egg/freckle.py", line 82, in get_projects
return self.request("%s/projects.xml" % self.endpoint)
File "/usr/local/Cellar/python/2.7.1/lib/python2.7/site-packages/freckle-0.1-py2.7.egg/freckle.py", line 41, in request
return self.parse_response(content)
File "/usr/local/Cellar/python/2.7.1/lib/python2.7/site-packages/freckle-0.1-py2.7.egg/freckle.py", line 93, in parse_response
as_python = getattr(self, parser)(item.text)
File "/usr/local/Cellar/python/2.7.1/lib/python2.7/site-packages/freckle-0.1-py2.7.egg/freckle.py", line 127, in yaml_as_python
return yaml.load(val)
File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/yaml/init.py", line 58, in load
return loader.get_single_data()
File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/yaml/constructor.py", line 44, in get_single_data
return self.construct_document(node)
File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/yaml/constructor.py", line 53, in construct_document
for dummy in generator:
File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/yaml/constructor.py", line 403, in construct_yaml_map
value = self.construct_mapping(node)
File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/yaml/constructor.py", line 213, in construct_mapping
return BaseConstructor.construct_mapping(self, node, deep=deep)
File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/yaml/constructor.py", line 138, in construct_mapping
value = self.construct_object(value_node, deep=deep)
File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/yaml/constructor.py", line 93, in construct_object
data = constructor(self, node)
File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/yaml/constructor.py", line 419, in construct_undefined
node.start_mark)
yaml.constructor.ConstructorError: could not determine a constructor for the tag '!binary'
in "", line 12, column 12:
:name: !binary |
^

@olleolleolle
Copy link
Author

In my case, a try/catch around the yaml.load(val) helped me get back results. I return "BROKEN!" in case of broken parsing.

@olleolleolle
Copy link
Author

def yaml_as_python(self, val):
    """Convert YAML to dict"""
    try:
        return yaml.load(val)
    except Exception, e:
        return "BROKEN"

@ipmb
Copy link
Owner

ipmb commented May 27, 2011

What is the actual value YAML is trying to parse? Sounds like this might be an issue somewhere upstream.

@olleolleolle
Copy link
Author

Having gotten feedback that the trouble was bad data upstream, it might still be interesting to see what data can crash that parser:

olle@ollebook:visu $ python visu.py 

--- 
- :tag: 
    :name: support
    :id: 137260
- :tag: 
    :name: svn issues
    :id: 109970
- :tag: 
    :name: diverse.
    :id: 137261
- :tag: 
    :name: !binary |
      TcO4ZGVy

    :id: 137259


Traceback (most recent call last):
  File "visu.py", line 32, in <module>
    projects = xf.get_projects()
  File "visu.py", line 23, in get_projects
    for project in self.f.get_projects():
  File "/usr/local/Cellar/python/2.7.1/lib/python2.7/site-packages/freckle-0.1-py2.7.egg/freckle.py", line 82, in get_projects
    return self.request("%s/projects.xml" % self.endpoint)
  File "/usr/local/Cellar/python/2.7.1/lib/python2.7/site-packages/freckle-0.1-py2.7.egg/freckle.py", line 41, in request
    return self.parse_response(content)
  File "/usr/local/Cellar/python/2.7.1/lib/python2.7/site-packages/freckle-0.1-py2.7.egg/freckle.py", line 93, in parse_response
    as_python = getattr(self, parser)(item.text)
  File "/usr/local/Cellar/python/2.7.1/lib/python2.7/site-packages/freckle-0.1-py2.7.egg/freckle.py", line 133, in yaml_as_python
    raise e
yaml.constructor.ConstructorError: could not determine a constructor for the tag '!binary'
  in "<string>", line 12, column 12:
        :name: !binary |
               ^

@olleolleolle
Copy link
Author

Hmpf, the text content of that "tag" was the UTF-8 word "Møder". That should parse. Well. This is not a problem in "freckle" module, but it will trouble its Euro users.

PyAML documentation has more detailed info.

In Python 2:

str objects are converted into !!str, !!python/str or !binary nodes depending on whether the object is an ASCII, UTF-8 or binary string.
unicode objects are converted into !!python/unicode or !!str nodes depending on whether the object is an ASCII string or not.
yaml.dump(data) produces the document as a UTF-8 encoded str object.
yaml.dump(data, encoding=('utf-8'|'utf-16-be'|'utf-16-le')) produces a str object in the specified encoding.
yaml.dump(data, encoding=None) produces a unicode object.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants