Skip to content

Commit

Permalink
Raising ValueError when yielded response is not iterable, closes #114
Browse files Browse the repository at this point in the history
  • Loading branch information
pylover committed Sep 10, 2018
1 parent c2cd234 commit 842718b
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions nanohttp/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import types
import logging
from collections import Iterable

from nanohttp.contexts import Context, context
from nanohttp.exceptions import HTTPStatus
Expand Down Expand Up @@ -88,7 +89,15 @@ def __call__(self, environ, start_response):
if response_body:
# The goal is to yield an iterable, to encode and iter over it
# at the end of this method.
if isinstance(response_body, types.GeneratorType):

if isinstance(response_body, (str, bytes)):
# Mocking the body inside an iterable to prevent
# the iteration over the str character by character
# For more info check the pull-request
# #34, https://github.com/Carrene/nanohttp/pull/34
response_iterable = (response_body, )

elif isinstance(response_body, types.GeneratorType):
# Generators are iterable !
response_iterable = response_body

Expand All @@ -97,14 +106,15 @@ def __call__(self, environ, start_response):
# `yield` statement
buffer = next(response_iterable)

elif isinstance(response_body, (str, bytes)):
# Mocking the body inside an iterable to prevent
# the iteration over the str character by character
# For more info check the pull-request
# #34, https://github.com/Carrene/nanohttp/pull/34
response_iterable = (response_body, )
elif isinstance(response_body, Iterable):
# Creating an iterator from iterable!
response_iterable = iter(response_body)

else:
raise ValueError(
'Controller\'s action/handler response must be '
'generator and or iterable'
)
# Assuming the body is an iterable.
response_iterable = response_body

Expand Down

0 comments on commit 842718b

Please sign in to comment.