From 7c09ec29f716670d16429af71d5984bdc0bf9892 Mon Sep 17 00:00:00 2001 From: Suby Raman Date: Thu, 2 Feb 2017 12:21:14 -0500 Subject: [PATCH 1/6] rebase --- sanic/blueprints.py | 3 + sanic/exceptions.py | 4 + sanic/router.py | 51 ++++++-- sanic/sanic.py | 61 ++++++++- sanic/views.py | 1 + tests/test_url_building.py | 261 +++++++++++++++++++++++++++++++++++++ 6 files changed, 369 insertions(+), 12 deletions(-) create mode 100644 tests/test_url_building.py diff --git a/sanic/blueprints.py b/sanic/blueprints.py index c9a4b8ac..0c14f4bc 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -35,6 +35,9 @@ class Blueprint: # Routes for future in self.routes: + # attach the blueprint name to the handler so that it can be + # prefixed properly in the router + future.handler.__blueprintname__ = self.name # Prepend the blueprint URI prefix if available uri = url_prefix + future.uri if url_prefix else future.uri app.route( diff --git a/sanic/exceptions.py b/sanic/exceptions.py index d986cd08..201edbf6 100644 --- a/sanic/exceptions.py +++ b/sanic/exceptions.py @@ -120,6 +120,10 @@ class ServerError(SanicException): status_code = 500 +class URLBuildError(SanicException): + status_code = 500 + + class FileNotFound(NotFound): status_code = 404 diff --git a/sanic/router.py b/sanic/router.py index 5ed21136..25b1b9ee 100644 --- a/sanic/router.py +++ b/sanic/router.py @@ -4,8 +4,8 @@ from functools import lru_cache from .exceptions import NotFound, InvalidUsage from .views import CompositionView -Route = namedtuple('Route', ['handler', 'methods', 'pattern', 'parameters']) -Parameter = namedtuple('Parameter', ['name', 'cast']) +Route = namedtuple('Route', ['handler', 'methods', 'pattern', 'parameters', 'name']) +Parameter = namedtuple('Parameter', ['name', 'cast', 'pattern']) REGEX_TYPES = { 'string': (str, r'[^/]+'), @@ -59,6 +59,7 @@ class Router: routes_static = None routes_dynamic = None routes_always_check = None + parameter_pattern = re.compile(r'<(.+?)>') def __init__(self): self.routes_all = {} @@ -67,6 +68,19 @@ class Router: self.routes_always_check = [] self.hosts = None + def parse_parameter_string(self, parameter_string): + # We could receive NAME or NAME:PATTERN + name = parameter_string + pattern = 'string' + if ':' in parameter_string: + name, pattern = parameter_string.split(':', 1) + + default = (str, pattern) + # Pull from pre-configured types + _type, pattern = REGEX_TYPES.get(pattern, default) + + return name, _type, pattern + def add(self, uri, methods, handler, host=None): """ Adds a handler to the route list @@ -106,14 +120,13 @@ class Router: def add_parameter(match): # We could receive NAME or NAME:PATTERN name = match.group(1) - pattern = 'string' - if ':' in name: - name, pattern = name.split(':', 1) + name, _type, pattern = self.parse_parameter_string(name) - default = (str, pattern) - # Pull from pre-configured types - _type, pattern = REGEX_TYPES.get(pattern, default) - parameter = Parameter(name=name, cast=_type) + # store a regex for matching on a specific parameter + # this will be useful for URL building + specific_parameter_pattern = '^{}$'.format(pattern) + parameter = Parameter( + name=name, cast=_type, pattern=specific_parameter_pattern) parameters.append(parameter) # Mark the whole route as unhashable if it has the hash key in it @@ -125,7 +138,7 @@ class Router: return '({})'.format(pattern) - pattern_string = re.sub(r'<(.+?)>', add_parameter, uri) + pattern_string = re.sub(self.parameter_pattern, add_parameter, uri) pattern = re.compile(r'^{}$'.format(pattern_string)) def merge_route(route, methods, handler): @@ -169,9 +182,17 @@ class Router: if route: route = merge_route(route, methods, handler) else: + # prefix the handler name with the blueprint name + # if available + if hasattr(handler, '__blueprintname__'): + handler_name = '{}.{}'.format( + handler.__blueprintname__, handler.__name__) + else: + handler_name = handler.__name__ + route = Route( handler=handler, methods=methods, pattern=pattern, - parameters=parameters) + parameters=parameters, name=handler_name) self.routes_all[uri] = route if properties['unhashable']: @@ -208,6 +229,14 @@ class Router: if clean_cache: self._get.cache_clear() + @lru_cache(maxsize=ROUTER_CACHE_SIZE) + def find_route_by_view_name(self, view_name): + for uri, route in self.routes_all.items(): + if route.name == view_name: + return uri, route + + return (None, None) + def get(self, request): """ Gets a request handler based on the URL of the request, or raises an diff --git a/sanic/sanic.py b/sanic/sanic.py index 8cfe08dd..11b104ac 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -3,13 +3,15 @@ from asyncio import get_event_loop from collections import deque from functools import partial from inspect import isawaitable, stack, getmodulename +import re from traceback import format_exc +from urllib.parse import urlencode, urlunparse import warnings from .config import Config from .constants import HTTP_METHODS from .exceptions import Handler -from .exceptions import ServerError +from .exceptions import ServerError, URLBuildError from .log import log from .response import HTTPResponse from .router import Router @@ -192,6 +194,63 @@ class Sanic: DeprecationWarning) return self.blueprint(*args, **kwargs) + def url_for(self, view_name: str, **kwargs): + uri, route = self.router.find_route_by_view_name(view_name) + + if not uri or not route: + raise URLBuildError( + 'Endpoint with name `{}` was not found'.format( + view_name)) + + out = uri + matched_params = re.findall( + self.router.parameter_pattern, uri) + + for match in matched_params: + name, _type, pattern = self.router.parse_parameter_string( + match) + specific_pattern = '^{}$'.format(pattern) + + supplied_param = None + if kwargs.get(name): + supplied_param = kwargs.get(name) + del kwargs[name] + else: + raise URLBuildError( + 'Required parameter `{}` was not passed to url_for'.format( + name)) + + supplied_param = str(supplied_param) + passes_pattern = re.match(specific_pattern, supplied_param) + + if not passes_pattern: + if _type != str: + msg = ( + 'Value "{}" for parameter `{}` does not ' + 'match pattern for type `{}`: {}'.format( + supplied_param, name, _type.__name__, pattern)) + else: + msg = ( + 'Value "{}" for parameter `{}` ' + 'does not satisfy pattern {}'.format( + supplied_param, name, pattern)) + raise URLBuildError(msg) + + replacement_regex = '(<{}.*?>)'.format(name) + + out = re.sub( + replacement_regex, supplied_param, out) + + # parse the remainder of the keyword arguments into a querystring + if kwargs: + query_string = urlencode(kwargs) + out = urlunparse(( + '', '', out, + '', query_string, '' + )) + + return out + # -------------------------------------------------------------------- # # Request Handling # -------------------------------------------------------------------- # diff --git a/sanic/views.py b/sanic/views.py index 407ba136..5d8e9d40 100644 --- a/sanic/views.py +++ b/sanic/views.py @@ -64,6 +64,7 @@ class HTTPMethodView: view.view_class = cls view.__doc__ = cls.__doc__ view.__module__ = cls.__module__ + view.__name__ = cls.__name__ return view diff --git a/tests/test_url_building.py b/tests/test_url_building.py new file mode 100644 index 00000000..c8209557 --- /dev/null +++ b/tests/test_url_building.py @@ -0,0 +1,261 @@ +import pytest as pytest +from urllib.parse import urlsplit, parse_qsl + +from sanic import Sanic +from sanic.response import text +from sanic.views import HTTPMethodView, CompositionView +from sanic.blueprints import Blueprint +from sanic.utils import sanic_endpoint_test +from sanic.exceptions import URLBuildError + +import string + + +def _generate_handlers_from_names(app, l): + for name in l: + # this is the easiest way to generate functions with dynamic names + exec('@app.route(name)\ndef {}(request):\n\treturn text("{}")'.format( + name, name)) + + +@pytest.fixture +def simple_app(): + app = Sanic('simple_app') + handler_names = list(string.ascii_letters) + + _generate_handlers_from_names(app, handler_names) + + return app + + +def test_simple_url_for_getting(simple_app): + for letter in string.ascii_letters: + url = simple_app.url_for(letter) + + assert url == '/{}'.format(letter) + request, response = sanic_endpoint_test( + simple_app, uri=url) + assert response.status == 200 + assert response.text == letter + + +def test_fails_if_endpoint_not_found(): + app = Sanic('fail_url_build') + + @app.route('/fail') + def fail(): + return text('this should fail') + + with pytest.raises(URLBuildError) as e: + app.url_for('passes') + + assert str(e.value) == 'Endpoint with name `passes` was not found' + + +def test_fails_url_build_if_param_not_passed(): + url = '/' + + for letter in string.ascii_letters: + url += '<{}>/'.format(letter) + + app = Sanic('fail_url_build') + + @app.route(url) + def fail(): + return text('this should fail') + + fail_args = list(string.ascii_letters) + fail_args.pop() + + fail_kwargs = {l: l for l in fail_args} + + with pytest.raises(URLBuildError) as e: + app.url_for('fail', **fail_kwargs) + + assert 'Required parameter `Z` was not passed to url_for' in str(e.value) + + +COMPLEX_PARAM_URL = ( + '///' + '//') +PASSING_KWARGS = { + 'foo': 4, 'four_letter_string': 'woof', + 'two_letter_string': 'ba', 'normal_string': 'normal', + 'some_number': '1.001'} +EXPECTED_BUILT_URL = '/4/woof/ba/normal/1.001' + + +def test_fails_with_int_message(): + app = Sanic('fail_url_build') + + @app.route(COMPLEX_PARAM_URL) + def fail(): + return text('this should fail') + + failing_kwargs = dict(PASSING_KWARGS) + failing_kwargs['foo'] = 'not_int' + + with pytest.raises(URLBuildError) as e: + app.url_for('fail', **failing_kwargs) + + expected_error = ( + 'Value "not_int" for parameter `foo` ' + 'does not match pattern for type `int`: \d+') + assert str(e.value) == expected_error + + +def test_fails_with_two_letter_string_message(): + app = Sanic('fail_url_build') + + @app.route(COMPLEX_PARAM_URL) + def fail(): + return text('this should fail') + + failing_kwargs = dict(PASSING_KWARGS) + failing_kwargs['two_letter_string'] = 'foobar' + + with pytest.raises(URLBuildError) as e: + app.url_for('fail', **failing_kwargs) + + expected_error = ( + 'Value "foobar" for parameter `two_letter_string` ' + 'does not satisfy pattern [A-z]{2}') + + assert str(e.value) == expected_error + + +def test_fails_with_number_message(): + app = Sanic('fail_url_build') + + @app.route(COMPLEX_PARAM_URL) + def fail(): + return text('this should fail') + + failing_kwargs = dict(PASSING_KWARGS) + failing_kwargs['some_number'] = 'foo' + + with pytest.raises(URLBuildError) as e: + app.url_for('fail', **failing_kwargs) + + expected_error = ( + 'Value "foo" for parameter `some_number` ' + 'does not match pattern for type `float`: [0-9\\\\.]+') + + assert str(e.value) == expected_error + + +def test_adds_other_supplied_values_as_query_string(): + app = Sanic('passes') + + @app.route(COMPLEX_PARAM_URL) + def passes(): + return text('this should pass') + + new_kwargs = dict(PASSING_KWARGS) + new_kwargs['added_value_one'] = 'one' + new_kwargs['added_value_two'] = 'two' + + url = app.url_for('passes', **new_kwargs) + + query = dict(parse_qsl(urlsplit(url).query)) + + assert query['added_value_one'] == 'one' + assert query['added_value_two'] == 'two' + + +@pytest.fixture +def blueprint_app(): + app = Sanic('blueprints') + + first_print = Blueprint('first', url_prefix='/first') + second_print = Blueprint('second', url_prefix='/second') + + @first_print.route('/foo') + def foo(): + return text('foo from first') + + @first_print.route('/foo/') + def foo_with_param(request, param): + return text( + 'foo from first : {}'.format(param)) + + @second_print.route('/foo') # noqa + def foo(): + return text('foo from second') + + @second_print.route('/foo/') # noqa + def foo_with_param(request, param): + return text( + 'foo from second : {}'.format(param)) + + app.blueprint(first_print) + app.blueprint(second_print) + + return app + + +def test_blueprints_are_named_correctly(blueprint_app): + first_url = blueprint_app.url_for('first.foo') + assert first_url == '/first/foo' + + second_url = blueprint_app.url_for('second.foo') + assert second_url == '/second/foo' + + +def test_blueprints_work_with_params(blueprint_app): + first_url = blueprint_app.url_for('first.foo_with_param', param='bar') + assert first_url == '/first/foo/bar' + + second_url = blueprint_app.url_for('second.foo_with_param', param='bar') + assert second_url == '/second/foo/bar' + + +@pytest.fixture +def methodview_app(): + app = Sanic('methodview') + + class ViewOne(HTTPMethodView): + def get(self, request): + return text('I am get method') + + def post(self, request): + return text('I am post method') + + def put(self, request): + return text('I am put method') + + def patch(self, request): + return text('I am patch method') + + def delete(self, request): + return text('I am delete method') + + app.add_route(ViewOne.as_view('view_one'), '/view_one') + + class ViewTwo(HTTPMethodView): + def get(self, request): + return text('I am get method') + + def post(self, request): + return text('I am post method') + + def put(self, request): + return text('I am put method') + + def patch(self, request): + return text('I am patch method') + + def delete(self, request): + return text('I am delete method') + + app.add_route(ViewTwo.as_view(), '/view_two') + + return app + + +def test_methodview_naming(methodview_app): + viewone_url = methodview_app.url_for('ViewOne') + viewtwo_url = methodview_app.url_for('ViewTwo') + + assert viewone_url == '/view_one' + assert viewtwo_url == '/view_two' From f9056099f9cb69e9e7166bcd5ae79974ce37ca94 Mon Sep 17 00:00:00 2001 From: Suby Raman Date: Thu, 2 Feb 2017 12:52:48 -0500 Subject: [PATCH 2/6] all works --- docs/sanic/blueprints.md | 19 +++++++++++++++++++ docs/sanic/class_based_views.md | 25 ++++++++++++++++++++++++- docs/sanic/routing.md | 32 ++++++++++++++++++++++++++++++++ docs/sanic/static_files.md | 2 ++ sanic/sanic.py | 28 +++++++++++++++++++++++++++- tests/test_url_building.py | 2 +- 6 files changed, 105 insertions(+), 3 deletions(-) diff --git a/docs/sanic/blueprints.md b/docs/sanic/blueprints.md index 71849ac1..120aa1dd 100644 --- a/docs/sanic/blueprints.md +++ b/docs/sanic/blueprints.md @@ -158,3 +158,22 @@ app.blueprint(blueprint_v2) app.run(host='0.0.0.0', port=8000, debug=True) ``` + +## URL Building with `url_for` + +If you wish to generate a URL for a route inside of a blueprint, remember that the endpoint name +takes the format `.`. For example: + +``` +@blueprint_v1.route('/') +async def root(request): + url = app.url_for('v1.post_handler', post_id=5) + return redirect(url) + + +@blueprint_v1.route('/post/') +async def post_handler(request, post_id): + return text('Post {} in Blueprint V1'.format(post_id)) +``` + + diff --git a/docs/sanic/class_based_views.md b/docs/sanic/class_based_views.md index 5f99a0b5..02b02140 100644 --- a/docs/sanic/class_based_views.md +++ b/docs/sanic/class_based_views.md @@ -67,7 +67,7 @@ app.add_route(NameView.as_view(), '/') If you want to add any decorators to the class, you can set the `decorators` class variable. These will be applied to the class when `as_view` is called. -``` +```python class ViewWithDecorator(HTTPMethodView): decorators = [some_decorator_here] @@ -77,6 +77,27 @@ class ViewWithDecorator(HTTPMethodView): app.add_route(ViewWithDecorator.as_view(), '/url') ``` +#### URL Building + +If you wish to build a URL for an HTTPMethodView, remember that the class name will be the endpoint +that you will pass into `url_for`. For example: + +```python +@app.route('/') +def index(request): + url = app.url_for('SpecialClassView') + return redirect(url) + + +class SpecialClassView(HTTPMethodView): + def get(self, request): + return text('Hello from the Special Class View!') + + +app.add_route(SpecialClassView.as_view(), '/special_class_view') +``` + + ## Using CompositionView As an alternative to the `HTTPMethodView`, you can use `CompositionView` to @@ -106,3 +127,5 @@ view.add(['POST', 'PUT'], lambda request: text('I am a post/put method')) # Use the new view to handle requests to the base URL app.add_route(view, '/') ``` + +Note: currently you cannot build a URL for a CompositionView using `url_for`. diff --git a/docs/sanic/routing.md b/docs/sanic/routing.md index 2dda39c4..84945904 100644 --- a/docs/sanic/routing.md +++ b/docs/sanic/routing.md @@ -119,3 +119,35 @@ app.add_route(handler1, '/test') app.add_route(handler2, '/folder/') app.add_route(person_handler2, '/person/', methods=['GET']) ``` + +## URL building with `url_for`. + +Sanic provides a `url_for` method, to generate URLs based on the handler method name. This is useful if you want to avoid hardcoding url paths into your app; instead, you can just reference the handler name. For example: + +``` +@app.route('/') +async def index(request): + # generate a URL for the endpoint `post_handler` + url = app.url_for('post_handler', post_id=5) + # the URL is `/posts/5`, redirect to it + return redirect(url) + + +@app.route('/posts/') +async def post_handler(request, post_id): + return text('Post - {}'.format(post_id)) +``` + +Other things to keep in mind when using `url_for`: + +- Keyword arguments passed to `url_for` that are not request parameters will be included in the URL's query string. For example: +``` +url = app.url_for('post_handler', post_id=5, arg_one='one', arg_two='two') +# /posts/5?arg_one=one&arg_two=two +``` +- All valid parameters must be passed to `url_for` to build a URL. If a parameter is not supplied, or if a parameter does not match the specified type, a `URLBuildError` will be thrown. + + + + + diff --git a/docs/sanic/static_files.md b/docs/sanic/static_files.md index d7e4866d..f0ce9d78 100644 --- a/docs/sanic/static_files.md +++ b/docs/sanic/static_files.md @@ -17,3 +17,5 @@ app.static('/the_best.png', '/home/ubuntu/test.png') app.run(host="0.0.0.0", port=8000) ``` + +Note: currently you cannot build a URL for a static file using `url_for`. diff --git a/sanic/sanic.py b/sanic/sanic.py index 11b104ac..2d2f83fe 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -195,6 +195,26 @@ class Sanic: return self.blueprint(*args, **kwargs) def url_for(self, view_name: str, **kwargs): + """Builds a URL based on a view name and the values provided. + + In order to build a URL, all request parameters must be supplied as + keyword arguments, and each parameter must pass the test for the + specified parameter type. If these conditions are not met, a + `URLBuildError` will be thrown. + + Keyword arguments that are not request parameters will be included in + the output URL's query string. + + :param view_name: A string referencing the view name + :param **kwargs: keys and values that are used to build request + parameters and query string arguments. + + :return: the built URL + + Raises: + URLBuildError + """ + # find the route by the supplied view name uri, route = self.router.find_route_by_view_name(view_name) if not uri or not route: @@ -203,15 +223,18 @@ class Sanic: view_name)) out = uri + + # find all the parameters we will need to build in the URL matched_params = re.findall( self.router.parameter_pattern, uri) for match in matched_params: name, _type, pattern = self.router.parse_parameter_string( match) + # we only want to match against each individual parameter specific_pattern = '^{}$'.format(pattern) - supplied_param = None + if kwargs.get(name): supplied_param = kwargs.get(name) del kwargs[name] @@ -221,6 +244,8 @@ class Sanic: name)) supplied_param = str(supplied_param) + # determine if the parameter supplied by the caller passes the test + # in the URL passes_pattern = re.match(specific_pattern, supplied_param) if not passes_pattern: @@ -236,6 +261,7 @@ class Sanic: supplied_param, name, pattern)) raise URLBuildError(msg) + # replace the parameter in the URL with the supplied value replacement_regex = '(<{}.*?>)'.format(name) out = re.sub( diff --git a/tests/test_url_building.py b/tests/test_url_building.py index c8209557..480313b3 100644 --- a/tests/test_url_building.py +++ b/tests/test_url_building.py @@ -3,7 +3,7 @@ from urllib.parse import urlsplit, parse_qsl from sanic import Sanic from sanic.response import text -from sanic.views import HTTPMethodView, CompositionView +from sanic.views import HTTPMethodView from sanic.blueprints import Blueprint from sanic.utils import sanic_endpoint_test from sanic.exceptions import URLBuildError From 5632d073bedefac3c70dbaa41d16ee1ec2c4a922 Mon Sep 17 00:00:00 2001 From: Suby Raman Date: Thu, 2 Feb 2017 13:00:15 -0500 Subject: [PATCH 3/6] update docs --- sanic/router.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/sanic/router.py b/sanic/router.py index 25b1b9ee..c065ffd4 100644 --- a/sanic/router.py +++ b/sanic/router.py @@ -4,8 +4,10 @@ from functools import lru_cache from .exceptions import NotFound, InvalidUsage from .views import CompositionView -Route = namedtuple('Route', ['handler', 'methods', 'pattern', 'parameters', 'name']) -Parameter = namedtuple('Parameter', ['name', 'cast', 'pattern']) +Route = namedtuple( + 'Route', + ['handler', 'methods', 'pattern', 'parameters', 'name']) +Parameter = namedtuple('Parameter', ['name', 'cast']) REGEX_TYPES = { 'string': (str, r'[^/]+'), @@ -69,6 +71,16 @@ class Router: self.hosts = None def parse_parameter_string(self, parameter_string): + """ + Parse a parameter string into its constituent name, type, and pattern + For example: + `parse_parameter_string(' + ('param_one', str, '[A-z]') + + :param parameter_string: String to parse + :return: tuple containing + (parameter_name, parameter_type, parameter_pattern) + """ # We could receive NAME or NAME:PATTERN name = parameter_string pattern = 'string' @@ -118,15 +130,11 @@ class Router: properties = {"unhashable": None} def add_parameter(match): - # We could receive NAME or NAME:PATTERN name = match.group(1) name, _type, pattern = self.parse_parameter_string(name) - # store a regex for matching on a specific parameter - # this will be useful for URL building - specific_parameter_pattern = '^{}$'.format(pattern) parameter = Parameter( - name=name, cast=_type, pattern=specific_parameter_pattern) + name=name, cast=_type) parameters.append(parameter) # Mark the whole route as unhashable if it has the hash key in it @@ -231,6 +239,12 @@ class Router: @lru_cache(maxsize=ROUTER_CACHE_SIZE) def find_route_by_view_name(self, view_name): + """ + Find a route in the router based on the specified view name. + + :param view_name: string of view name to search by + :return: tuple containing (uri, Route) + """ for uri, route in self.routes_all.items(): if route.name == view_name: return uri, route From 5c63ce666c1be3b4d5ef9272da459ef10c9770e8 Mon Sep 17 00:00:00 2001 From: Suby Raman Date: Thu, 2 Feb 2017 14:21:59 -0500 Subject: [PATCH 4/6] punctuation --- docs/sanic/routing.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/sanic/routing.md b/docs/sanic/routing.md index 84945904..0cadd707 100644 --- a/docs/sanic/routing.md +++ b/docs/sanic/routing.md @@ -120,11 +120,11 @@ app.add_route(handler2, '/folder/') app.add_route(person_handler2, '/person/', methods=['GET']) ``` -## URL building with `url_for`. +## URL building with `url_for` Sanic provides a `url_for` method, to generate URLs based on the handler method name. This is useful if you want to avoid hardcoding url paths into your app; instead, you can just reference the handler name. For example: -``` +```python @app.route('/') async def index(request): # generate a URL for the endpoint `post_handler` @@ -141,7 +141,7 @@ async def post_handler(request, post_id): Other things to keep in mind when using `url_for`: - Keyword arguments passed to `url_for` that are not request parameters will be included in the URL's query string. For example: -``` +```python url = app.url_for('post_handler', post_id=5, arg_one='one', arg_two='two') # /posts/5?arg_one=one&arg_two=two ``` From aa547859186760e933780fb9f76a2405a2b9b8f4 Mon Sep 17 00:00:00 2001 From: Fengyuan Chen Date: Sat, 4 Feb 2017 15:54:37 +0800 Subject: [PATCH 5/6] fix always warning loop is passed issue --- sanic/sanic.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sanic/sanic.py b/sanic/sanic.py index 2d2f83fe..a6a65204 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -445,18 +445,18 @@ class Sanic: Helper function used by `run` and `create_server`. """ - self.error_handler.debug = debug - self.debug = debug - self.loop = loop = get_event_loop() - if loop is not None: - if self.debug: + if debug: warnings.simplefilter('default') warnings.warn("Passing a loop will be deprecated in version" " 0.4.0 https://github.com/channelcat/sanic/" "pull/335 has more information.", DeprecationWarning) + self.error_handler.debug = debug + self.debug = debug + self.loop = loop = get_event_loop() + server_settings = { 'protocol': protocol, 'host': host, From 36d519026f3b02772d961820449f8810a2c9b19c Mon Sep 17 00:00:00 2001 From: Suby Raman Date: Fri, 3 Feb 2017 10:12:33 -0500 Subject: [PATCH 6/6] reject unnamed handlers --- sanic/router.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sanic/router.py b/sanic/router.py index c065ffd4..24d0438f 100644 --- a/sanic/router.py +++ b/sanic/router.py @@ -196,7 +196,7 @@ class Router: handler_name = '{}.{}'.format( handler.__blueprintname__, handler.__name__) else: - handler_name = handler.__name__ + handler_name = getattr(handler, '__name__', None) route = Route( handler=handler, methods=methods, pattern=pattern, @@ -245,6 +245,9 @@ class Router: :param view_name: string of view name to search by :return: tuple containing (uri, Route) """ + if not view_name: + return (None, None) + for uri, route in self.routes_all.items(): if route.name == view_name: return uri, route