From 203101c06c61a80d1023f1d21302eceae5f66e81 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Sat, 6 May 2017 20:15:40 -0400 Subject: Update url.py | Refactor parse_url_overrides Refactor parse_url_overrides: * pop values with default if key is missing * change conditionals to test for truth * prevent throwing an exception if passing keyword with default value * test if anchor starts with '#' before blindly adding it --- pyramid/url.py | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/pyramid/url.py b/pyramid/url.py index 9634f61da..7b88c385c 100644 --- a/pyramid/url.py +++ b/pyramid/url.py @@ -34,40 +34,28 @@ ANCHOR_SAFE = QUERY_SAFE def parse_url_overrides(kw): """Parse special arguments passed when generating urls. - The supplied dictionary is mutated, popping arguments as necessary. - Returns a 6-tuple of the format ``(app_url, scheme, host, port, - qs, anchor)``. + The supplied dictionary is mutated when we pop arguments. + Returns a 6-tuple of the format: + + ``(app_url, scheme, host, port, qs, anchor)``. """ - anchor = '' - qs = '' - app_url = None - host = None - scheme = None - port = None - - if '_query' in kw: - query = kw.pop('_query') + app_url = kw.pop('_app_url', None) + scheme = kw.pop('_scheme', None) + host = kw.pop('_host', None) + port = kw.pop('_port', None) + qs = query = kw.pop('_query', '') + anchor = kw.pop('_anchor', '') + + if query: if isinstance(query, string_types): qs = '?' + url_quote(query, QUERY_SAFE) elif query: qs = '?' + urlencode(query, doseq=True) - if '_anchor' in kw: - anchor = kw.pop('_anchor') + if anchor: anchor = url_quote(anchor, ANCHOR_SAFE) - anchor = '#' + anchor - - if '_app_url' in kw: - app_url = kw.pop('_app_url') - - if '_host' in kw: - host = kw.pop('_host') - - if '_scheme' in kw: - scheme = kw.pop('_scheme') - - if '_port' in kw: - port = kw.pop('_port') + if anchor.startswith('#') == False: + anchor = '#' + anchor return app_url, scheme, host, port, qs, anchor -- cgit v1.2.3