From db388f453e3b4fe018fc3403722ff5ce50579080 Mon Sep 17 00:00:00 2001 From: Devin Fee Date: Fri, 13 Apr 2012 17:12:43 -0700 Subject: escaped double quotes are now unescaped --- pyramid/scaffolds/template.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pyramid/scaffolds/template.py b/pyramid/scaffolds/template.py index 39d0e4b3f..b587adb73 100644 --- a/pyramid/scaffolds/template.py +++ b/pyramid/scaffolds/template.py @@ -35,7 +35,8 @@ class Template(object): content = native_(content, fsenc) try: return bytes_( - substitute_double_braces(content, TypeMapper(vars)), fsenc) + substitute_escaped_double_braces( + substitute_double_braces(content, TypeMapper(vars))), fsenc) except Exception as e: _add_except(e, ' in file %s' % filename) raise @@ -149,6 +150,14 @@ def substitute_double_braces(content, values): return values[value] return double_brace_pattern.sub(double_bracerepl, content) +escaped_double_brace_pattern = re.compile(r'\\{\\{(?P.*?)\\}\\}') + +def substitute_escaped_double_braces(content): + def escaped_double_bracerepl(match): + value = match.group('escape_braced').strip() + return "{{%(value)s}}" % locals() + return escaped_double_brace_pattern.sub(escaped_double_bracerepl, content) + def _add_except(exc, info): # pragma: no cover if not hasattr(exc, 'args') or exc.args is None: return -- cgit v1.2.3 From eeadb3a763fe2da39fbdc4c645ac786257b3bd73 Mon Sep 17 00:00:00 2001 From: Takahiro Fujiwara Date: Mon, 18 Feb 2013 23:56:22 +0900 Subject: Added a test case for templating with escaped double braces. --- pyramid/tests/test_scaffolds/test_template.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pyramid/tests/test_scaffolds/test_template.py b/pyramid/tests/test_scaffolds/test_template.py index d7cf638b6..da6caffb9 100644 --- a/pyramid/tests/test_scaffolds/test_template.py +++ b/pyramid/tests/test_scaffolds/test_template.py @@ -11,7 +11,7 @@ class TestTemplate(unittest.TestCase): inst = self._makeOne() result = inst.render_template('{{a}} {{b}}', {'a':'1', 'b':'2'}) self.assertEqual(result, bytes_('1 2')) - + def test_render_template_expr_failure(self): inst = self._makeOne() self.assertRaises(AttributeError, inst.render_template, @@ -37,6 +37,11 @@ class TestTemplate(unittest.TestCase): result = inst.render_template('{{a}}', {'a':None}) self.assertEqual(result, b'') + def test_render_template_with_escaped_double_quotes(self): + inst = self._makeOne() + result = inst.render_template('{{a}} {{b}} \{\{a\}\} \{\{c\}\}', {'a':'1', 'b':'2'}) + self.assertEqual(result, bytes_('1 2 {{a}} {{c}}')) + def test_module_dir(self): import sys import pkg_resources @@ -90,7 +95,7 @@ class TestTemplate(unittest.TestCase): 'overwrite':False, 'interactive':False, }) - + def test_write_files_path_missing(self): L = [] inst = self._makeOne() @@ -132,9 +137,9 @@ class DummyOptions(object): simulate = False overwrite = False interactive = False - + class DummyCommand(object): options = DummyOptions() verbosity = 1 - - + + -- cgit v1.2.3 From de6b2875091e0b03e22e2e51ce0db938ecf4b98a Mon Sep 17 00:00:00 2001 From: Takahiro Fujiwara Date: Tue, 19 Feb 2013 02:20:12 +0900 Subject: Added edged test cases for templating with escpaed doubled braces. --- pyramid/scaffolds/template.py | 6 +++--- pyramid/tests/test_scaffolds/test_template.py | 12 +++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pyramid/scaffolds/template.py b/pyramid/scaffolds/template.py index b587adb73..d88f5b2a6 100644 --- a/pyramid/scaffolds/template.py +++ b/pyramid/scaffolds/template.py @@ -149,15 +149,15 @@ def substitute_double_braces(content, values): value = match.group('braced').strip() return values[value] return double_brace_pattern.sub(double_bracerepl, content) - -escaped_double_brace_pattern = re.compile(r'\\{\\{(?P.*?)\\}\\}') + +escaped_double_brace_pattern = re.compile(r'\\{\\{(?P[^\\]*?)\\}\\}') def substitute_escaped_double_braces(content): def escaped_double_bracerepl(match): value = match.group('escape_braced').strip() return "{{%(value)s}}" % locals() return escaped_double_brace_pattern.sub(escaped_double_bracerepl, content) - + def _add_except(exc, info): # pragma: no cover if not hasattr(exc, 'args') or exc.args is None: return diff --git a/pyramid/tests/test_scaffolds/test_template.py b/pyramid/tests/test_scaffolds/test_template.py index da6caffb9..2e961c516 100644 --- a/pyramid/tests/test_scaffolds/test_template.py +++ b/pyramid/tests/test_scaffolds/test_template.py @@ -37,11 +37,21 @@ class TestTemplate(unittest.TestCase): result = inst.render_template('{{a}}', {'a':None}) self.assertEqual(result, b'') - def test_render_template_with_escaped_double_quotes(self): + def test_render_template_with_escaped_double_braces(self): inst = self._makeOne() result = inst.render_template('{{a}} {{b}} \{\{a\}\} \{\{c\}\}', {'a':'1', 'b':'2'}) self.assertEqual(result, bytes_('1 2 {{a}} {{c}}')) + def test_render_template_with_breaking_escaped_braces(self): + inst = self._makeOne() + result = inst.render_template('{{a}} {{b}} \{\{a\} \{b\}\}', {'a':'1', 'b':'2'}) + self.assertEqual(result, bytes_('1 2 \{\{a\} \{b\}\}')) + + def test_render_template_with_escaped_single_braces(self): + inst = self._makeOne() + result = inst.render_template('{{a}} {{b}} \{a\} \{b', {'a':'1', 'b':'2'}) + self.assertEqual(result, bytes_('1 2 \{a\} \{b')) + def test_module_dir(self): import sys import pkg_resources -- cgit v1.2.3 From ece96f658c631cae662d3849b35ee2723c368abe Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Tue, 27 Aug 2013 17:37:48 -0400 Subject: - Fix an obscure problem when combining a virtual root with a route with a ``*traverse`` in its pattern. Now the traversal path generated in such a configuration will be correct, instead of an element missing a leading slash. --- CHANGES.txt | 5 +++++ pyramid/tests/test_traversal.py | 17 +++++++++++++++++ pyramid/traversal.py | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index 1eeb0ce7b..0db274a19 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -149,6 +149,11 @@ Features Bug Fixes --------- +- Fix an obscure problem when combining a virtual root with a route with a + ``*traverse`` in its pattern. Now the traversal path generated in + such a configuration will be correct, instead of an element missing + a leading slash. + - Fixed a Mako renderer bug returning a tuple with a previous defname value in some circumstances. See https://github.com/Pylons/pyramid/issues/1037 for more information. diff --git a/pyramid/tests/test_traversal.py b/pyramid/tests/test_traversal.py index 2e45ae1a9..ba0be7e06 100644 --- a/pyramid/tests/test_traversal.py +++ b/pyramid/tests/test_traversal.py @@ -456,6 +456,23 @@ class ResourceTreeTraverserTests(unittest.TestCase): self.assertEqual(result['virtual_root'], resource) self.assertEqual(result['virtual_root_path'], ()) + def test_withroute_and_traverse_and_vroot(self): + abc = DummyContext() + resource = DummyContext(next=abc) + environ = self._getEnviron(HTTP_X_VHM_ROOT='/abc') + request = DummyRequest(environ) + traverser = self._makeOne(resource) + matchdict = {'traverse':text_('/foo/bar')} + request.matchdict = matchdict + result = traverser(request) + self.assertEqual(result['context'], abc) + self.assertEqual(result['view_name'], 'foo') + self.assertEqual(result['subpath'], ('bar',)) + self.assertEqual(result['traversed'], ('abc', 'foo')) + self.assertEqual(result['root'], resource) + self.assertEqual(result['virtual_root'], abc) + self.assertEqual(result['virtual_root_path'], ('abc',)) + class FindInterfaceTests(unittest.TestCase): def _callFUT(self, context, iface): from pyramid.traversal import find_interface diff --git a/pyramid/traversal.py b/pyramid/traversal.py index ed49d8743..469e77454 100644 --- a/pyramid/traversal.py +++ b/pyramid/traversal.py @@ -640,7 +640,7 @@ class ResourceTreeTraverser(object): # this is a *traverse stararg (not a {traverse}) # routing has already decoded these elements, so we just # need to join them - path = slash.join(path) or slash + path = '/' + slash.join(path) or slash subpath = matchdict.get('subpath', ()) if not is_nonstr_iter(subpath): -- cgit v1.2.3 From 0f98233875282c3cde49e946a06820dbb922cd55 Mon Sep 17 00:00:00 2001 From: Philip Jenvey Date: Tue, 27 Aug 2013 15:36:06 -0700 Subject: utilize the tuple form of starts/endswith --- pyramid/path.py | 2 +- pyramid/scaffolds/copydir.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pyramid/path.py b/pyramid/path.py index ab39a85d9..eb92ea62b 100644 --- a/pyramid/path.py +++ b/pyramid/path.py @@ -324,7 +324,7 @@ class DottedNameResolver(Resolver): def _pkg_resources_style(self, value, package): """ package.module:attr style """ - if value.startswith('.') or value.startswith(':'): + if value.startswith(('.', ':')): if not package: raise ValueError( 'relative name %r irresolveable without package' % (value,) diff --git a/pyramid/scaffolds/copydir.py b/pyramid/scaffolds/copydir.py index 7864dd1a1..3b871dc19 100644 --- a/pyramid/scaffolds/copydir.py +++ b/pyramid/scaffolds/copydir.py @@ -156,9 +156,9 @@ def should_skip_file(name): """ if name.startswith('.'): return 'Skipping hidden file %(filename)s' - if name.endswith('~') or name.endswith('.bak'): + if name.endswith(('~', '.bak')): return 'Skipping backup file %(filename)s' - if name.endswith('.pyc') or name.endswith('.pyo'): + if name.endswith(('.pyc', '.pyo')): return 'Skipping %s file ' % os.path.splitext(name)[1] + '%(filename)s' if name.endswith('$py.class'): return 'Skipping $py.class file %(filename)s' -- cgit v1.2.3 From 7aa3cb81fac320ba1d35c221d5e7aec6f470361f Mon Sep 17 00:00:00 2001 From: Takahiro Fujiwara Date: Wed, 28 Aug 2013 13:36:53 +0900 Subject: Added me to the CONTRIBUTERS.txt. --- CONTRIBUTORS.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 971c172f8..2bfc6c386 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -192,3 +192,5 @@ Contributors - Robert Jackiewicz, 2012/11/12 - John Anderson, 2012/11/14 + +- Takahiro Fujiwara, 2013/08/28 -- cgit v1.2.3 From 58951c0a9a4fc2a2122d88170b0761b1d89ea91c Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Wed, 28 Aug 2013 03:39:02 -0400 Subject: - The ``route_url`` and ``route_path`` APIs no longer quote ``/`` to ``%2F`` when a replacement value contains a ``/``. This was pointless, as WSGI servers always unquote the slash anyway, and Pyramid never sees the quoted value. --- CHANGES.txt | 5 +++++ pyramid/tests/test_urldispatch.py | 10 +++++----- pyramid/urldispatch.py | 6 ++++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 0db274a19..a527f7c1e 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -207,6 +207,11 @@ Backwards Incompatibilities previously returned the URL without the query string by default, it now does attach the query string unless it is overriden. +- The ``route_url`` and ``route_path`` APIs no longer quote ``/`` + to ``%2F`` when a replacement value contains a ``/``. This was pointless, + as WSGI servers always unquote the slash anyway, and Pyramid never sees the + quoted value. + 1.4 (2012-12-18) ================ diff --git a/pyramid/tests/test_urldispatch.py b/pyramid/tests/test_urldispatch.py index b2164717e..1755d9f47 100644 --- a/pyramid/tests/test_urldispatch.py +++ b/pyramid/tests/test_urldispatch.py @@ -295,7 +295,7 @@ class TestCompileRoute(unittest.TestCase): 'remainder':'/everything/else/here'}) self.assertEqual(matcher('foo/baz/biz/buz/bar'), None) self.assertEqual(generator( - {'baz':1, 'buz':2, 'remainder':'/a/b'}), '/foo/1/biz/2/bar%2Fa%2Fb') + {'baz':1, 'buz':2, 'remainder':'/a/b'}), '/foo/1/biz/2/bar/a/b') def test_no_beginning_slash(self): matcher, generator = self._callFUT('foo/:baz/biz/:buz/bar') @@ -491,10 +491,10 @@ class TestCompileRouteFunctional(unittest.TestCase): self.generates('zzz/{x}*traverse', {'x':'abc', 'traverse':'/def/g'}, '/zzz/abc/def/g') self.generates('/{x}', {'x':text_(b'/La Pe\xc3\xb1a', 'utf-8')}, - '/%2FLa%20Pe%C3%B1a') + '//La%20Pe%C3%B1a') self.generates('/{x}*y', {'x':text_(b'/La Pe\xc3\xb1a', 'utf-8'), 'y':'/rest/of/path'}, - '/%2FLa%20Pe%C3%B1a/rest/of/path') + '//La%20Pe%C3%B1a/rest/of/path') self.generates('*traverse', {'traverse':('a', text_(b'La Pe\xf1a'))}, '/a/La%20Pe%C3%B1a') self.generates('/foo/{id}.html', {'id':'bar'}, '/foo/bar.html') @@ -511,10 +511,10 @@ class TestCompileRouteFunctional(unittest.TestCase): self.generates('zzz/:x*traverse', {'x':'abc', 'traverse':'/def/g'}, '/zzz/abc/def/g') self.generates('/:x', {'x':text_(b'/La Pe\xc3\xb1a', 'utf-8')}, - '/%2FLa%20Pe%C3%B1a') + '//La%20Pe%C3%B1a') self.generates('/:x*y', {'x':text_(b'/La Pe\xc3\xb1a', 'utf-8'), 'y':'/rest/of/path'}, - '/%2FLa%20Pe%C3%B1a/rest/of/path') + '//La%20Pe%C3%B1a/rest/of/path') self.generates('*traverse', {'traverse':('a', text_(b'La Pe\xf1a'))}, '/a/La%20Pe%C3%B1a') self.generates('/foo/:id.html', {'id':'bar'}, '/foo/bar.html') diff --git a/pyramid/urldispatch.py b/pyramid/urldispatch.py index 4182ea665..8090f07f2 100644 --- a/pyramid/urldispatch.py +++ b/pyramid/urldispatch.py @@ -213,7 +213,9 @@ def _compile_route(route): if k == remainder: # a stararg argument if is_nonstr_iter(v): - v = '/'.join([quote_path_segment(x) for x in v]) # native + v = '/'.join( + [quote_path_segment(x, safe='/') for x in v] + ) # native else: if v.__class__ not in string_types: v = str(v) @@ -222,7 +224,7 @@ def _compile_route(route): if v.__class__ not in string_types: v = str(v) # v may be bytes (py2) or native string (py3) - v = quote_path_segment(v) + v = quote_path_segment(v, safe='/') # at this point, the value will be a native string newdict[k] = v -- cgit v1.2.3