Commit 9277f438 by Tom Christie

Fix YAML parser bug

parent ca9465f1
...@@ -92,28 +92,25 @@ class JSONParser(BaseParser): ...@@ -92,28 +92,25 @@ class JSONParser(BaseParser):
{'detail': 'JSON parse error - %s' % unicode(exc)}) {'detail': 'JSON parse error - %s' % unicode(exc)})
if yaml: class YAMLParser(BaseParser):
class YAMLParser(BaseParser): """
""" Parses YAML-serialized data.
Parses YAML-serialized data. """
"""
media_type = 'application/yaml' media_type = 'application/yaml'
def parse(self, stream): def parse(self, stream):
""" """
Returns a 2-tuple of `(data, files)`. Returns a 2-tuple of `(data, files)`.
`data` will be an object which is the parsed content of the response. `data` will be an object which is the parsed content of the response.
`files` will always be `None`. `files` will always be `None`.
""" """
try: try:
return (yaml.safe_load(stream), None) return (yaml.safe_load(stream), None)
except ValueError, exc: except (ValueError, yaml.parser.ParserError), exc:
raise ErrorResponse(status.HTTP_400_BAD_REQUEST, content = {'detail': 'YAML parse error - %s' % unicode(exc)}
{'detail': 'YAML parse error - %s' % unicode(exc)}) raise ErrorResponse(status.HTTP_400_BAD_REQUEST, content)
else:
YAMLParser = None
class PlainTextParser(BaseParser): class PlainTextParser(BaseParser):
...@@ -248,5 +245,8 @@ DEFAULT_PARSERS = ( ...@@ -248,5 +245,8 @@ DEFAULT_PARSERS = (
XMLParser XMLParser
) )
if YAMLParser: if yaml:
DEFAULT_PARSERS += (YAMLParser,) DEFAULT_PARSERS += (YAMLParser, )
else:
YAMLParser = None
...@@ -152,25 +152,22 @@ class XMLRenderer(BaseRenderer): ...@@ -152,25 +152,22 @@ class XMLRenderer(BaseRenderer):
return dict2xml(obj) return dict2xml(obj)
if yaml: class YAMLRenderer(BaseRenderer):
class YAMLRenderer(BaseRenderer): """
""" Renderer which serializes to YAML.
Renderer which serializes to YAML. """
"""
media_type = 'application/yaml' media_type = 'application/yaml'
format = 'yaml' format = 'yaml'
def render(self, obj=None, media_type=None): def render(self, obj=None, media_type=None):
""" """
Renders *obj* into serialized YAML. Renders *obj* into serialized YAML.
""" """
if obj is None: if obj is None:
return '' return ''
return yaml.safe_dump(obj) return yaml.safe_dump(obj)
else:
YAMLRenderer = None
class TemplateRenderer(BaseRenderer): class TemplateRenderer(BaseRenderer):
...@@ -409,5 +406,7 @@ DEFAULT_RENDERERS = ( ...@@ -409,5 +406,7 @@ DEFAULT_RENDERERS = (
XMLRenderer XMLRenderer
) )
if YAMLRenderer: if yaml:
DEFAULT_RENDERERS += (YAMLRenderer,) DEFAULT_RENDERERS += (YAMLRenderer, )
else:
YAMLRenderer = None
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment