Fix YAML parser bug

This commit is contained in:
Tom Christie 2012-02-21 22:09:05 +00:00
parent ca9465f11e
commit 9277f438cb
2 changed files with 39 additions and 40 deletions

View File

@ -92,28 +92,25 @@ class JSONParser(BaseParser):
{'detail': 'JSON parse error - %s' % unicode(exc)})
if yaml:
class YAMLParser(BaseParser):
class YAMLParser(BaseParser):
"""
Parses YAML-serialized data.
"""
media_type = 'application/yaml'
def parse(self, stream):
"""
Parses YAML-serialized data.
Returns a 2-tuple of `(data, files)`.
`data` will be an object which is the parsed content of the response.
`files` will always be `None`.
"""
media_type = 'application/yaml'
def parse(self, stream):
"""
Returns a 2-tuple of `(data, files)`.
`data` will be an object which is the parsed content of the response.
`files` will always be `None`.
"""
try:
return (yaml.safe_load(stream), None)
except ValueError, exc:
raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
{'detail': 'YAML parse error - %s' % unicode(exc)})
else:
YAMLParser = None
try:
return (yaml.safe_load(stream), None)
except (ValueError, yaml.parser.ParserError), exc:
content = {'detail': 'YAML parse error - %s' % unicode(exc)}
raise ErrorResponse(status.HTTP_400_BAD_REQUEST, content)
class PlainTextParser(BaseParser):
@ -248,5 +245,8 @@ DEFAULT_PARSERS = (
XMLParser
)
if YAMLParser:
DEFAULT_PARSERS += (YAMLParser,)
if yaml:
DEFAULT_PARSERS += (YAMLParser, )
else:
YAMLParser = None

View File

@ -152,25 +152,22 @@ class XMLRenderer(BaseRenderer):
return dict2xml(obj)
if yaml:
class YAMLRenderer(BaseRenderer):
class YAMLRenderer(BaseRenderer):
"""
Renderer which serializes to YAML.
"""
media_type = 'application/yaml'
format = 'yaml'
def render(self, obj=None, media_type=None):
"""
Renderer which serializes to YAML.
Renders *obj* into serialized YAML.
"""
if obj is None:
return ''
media_type = 'application/yaml'
format = 'yaml'
def render(self, obj=None, media_type=None):
"""
Renders *obj* into serialized YAML.
"""
if obj is None:
return ''
return yaml.safe_dump(obj)
else:
YAMLRenderer = None
return yaml.safe_dump(obj)
class TemplateRenderer(BaseRenderer):
@ -409,5 +406,7 @@ DEFAULT_RENDERERS = (
XMLRenderer
)
if YAMLRenderer:
DEFAULT_RENDERERS += (YAMLRenderer,)
if yaml:
DEFAULT_RENDERERS += (YAMLRenderer, )
else:
YAMLRenderer = None