Skip to content

Commit

Permalink
BUGFIX: TypeError causes 404, Closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pylover committed Mar 16, 2017
1 parent 200cd01 commit d36bf50
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
16 changes: 8 additions & 8 deletions nanohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import ujson


__version__ = '0.1.10'
__version__ = '0.1.11'

DEFAULT_CONFIG_FILE = 'nanohttp.yml'
DEFAULT_ADDRESS = '8080'
Expand Down Expand Up @@ -354,13 +354,14 @@ def __delattr__(self, key):

def action(*verbs, encoding='utf-8', content_type=None, inner_decorator=None):
def _decorator(func):
argcount = func.__code__.co_argcount

if inner_decorator is not None:
func = inner_decorator(func)

func.__http_methods__ = verbs if verbs else 'any'

func.__response_encoding__ = encoding
func.__argcount__ = argcount

if content_type:
func.__content_type__ = content_type
Expand Down Expand Up @@ -500,10 +501,7 @@ def _serve_handler(self, handler, *remaining_paths):
if hasattr(handler, '__content_type__'):
context.response_content_type = handler.__content_type__

try:
return handler(*remaining_paths)
except TypeError as ex:
raise HttpNotFound(str(ex))
return handler(*remaining_paths)

def _dispatch(self, *remaining_paths):
if not len(remaining_paths):
Expand All @@ -519,8 +517,10 @@ def _dispatch(self, *remaining_paths):
if handler is not None:
remaining_paths = (path, ) + remaining_paths

if handler is None or not hasattr(handler, '__http_methods__') \
or (hasattr(handler, '__annotations__') and len(handler.__annotations__) < len(remaining_paths)):
args_count = len(remaining_paths)
if (handler is None or not hasattr(handler, '__http_methods__')) \
or (hasattr(handler, '__argcount__') and handler.__argcount__ < args_count) \
or (hasattr(handler, '__annotations__') and len(handler.__annotations__) < args_count):
raise HttpNotFound()

if 'any' != handler.__http_methods__ and context.method not in handler.__http_methods__:
Expand Down
29 changes: 29 additions & 0 deletions practice/type_annotation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

import functools


def a(arg1, arg2, kw1=None, kw2=None, *args, kw3=None, **kw):
ttttttt = 66


@functools.wraps(a, )
def w(*args, **kwargs):
return a(*args, **kwargs)


def describe(f):
print(f.__name__)
code = f.__code__
for k in dir(code):
if k.startswith('co_'):
v = getattr(code, k)
print(k, v)


def main():
describe(a)
describe(w)


if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions tests/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def contact(self, contact_id: int=None):
yield "Contact: %s" % contact_id

@html
def users(self, user_id: int, attr: str=None):
def users(self, user_id: int=None, attr: str=None):
yield 'User: %s\n' % user_id
yield 'Attr: %s\n' % attr

Expand All @@ -72,7 +72,7 @@ def test_root(self):
def test_arguments(self):
self.assert_get('/contact/1', expected_response='Contact: 1')
self.assert_post('/contact', expected_response='Contact: None')
self.assert_get('/users', status=404)
self.assert_get('/users', expected_response='User: None\nAttr: None\n')
self.assert_get('/users/10/')
self.assert_get('/users/10/jobs', expected_response='User: 10\nAttr: jobs\n')
self.assert_get('/users/10/11/11', status=404)
Expand Down

0 comments on commit d36bf50

Please sign in to comment.