From 9549997d246ff0428c72609951ebcb70056cedc4 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Fri, 23 Sep 2011 18:05:47 -0400 Subject: beat me, whip me --- pyramid/compat.py | 4 ++++ pyramid/tests/pkgs/wsgiapp2app/__init__.py | 2 +- pyramid/tests/test_chameleon_text.py | 17 +++++++++-------- pyramid/tests/test_config/test_init.py | 4 ++-- pyramid/tests/test_config/test_views.py | 2 +- pyramid/tests/test_httpexceptions.py | 2 +- pyramid/tests/test_session.py | 20 +++++++------------- pyramid/tests/test_traversal.py | 5 +++-- pyramid/traversal.py | 15 +++++++-------- pyramid/urldispatch.py | 11 ++++++----- 10 files changed, 41 insertions(+), 41 deletions(-) diff --git a/pyramid/compat.py b/pyramid/compat.py index a19c61034..d83205c87 100644 --- a/pyramid/compat.py +++ b/pyramid/compat.py @@ -65,6 +65,7 @@ try: # pragma: no cover urlparse = parse from urllib.parse import quote as url_quote from urllib.parse import unquote as url_unquote + url_unquote_text = url_unquote from urllib.parse import urlencode as url_encode from urllib.request import urlopen as url_open except ImportError: @@ -73,6 +74,9 @@ except ImportError: from urllib import unquote as url_unquote from urllib import urlencode as url_encode from urllib2 import urlopen as url_open + def url_unquote_text(v, encoding='utf-8', errors='replace'): + v = url_unquote(v) + return v.decode(encoding, errors) if PY3: # pragma: no cover import builtins diff --git a/pyramid/tests/pkgs/wsgiapp2app/__init__.py b/pyramid/tests/pkgs/wsgiapp2app/__init__.py index 0880556ef..e2024198e 100644 --- a/pyramid/tests/pkgs/wsgiapp2app/__init__.py +++ b/pyramid/tests/pkgs/wsgiapp2app/__init__.py @@ -8,7 +8,7 @@ def hello(environ, start_response): assert environ['SCRIPT_NAME'] == '/hello' response_headers = [('Content-Type', 'text/plain')] start_response('200 OK', response_headers) - return ['Hello!'] + return [b'Hello!'] def main(): from pyramid.config import Configurator diff --git a/pyramid/tests/test_chameleon_text.py b/pyramid/tests/test_chameleon_text.py index cae52c2f4..8d23c8753 100644 --- a/pyramid/tests/test_chameleon_text.py +++ b/pyramid/tests/test_chameleon_text.py @@ -1,5 +1,6 @@ import unittest +from pyramid.compat import binary_type from pyramid.testing import skip_on from pyramid import testing @@ -98,8 +99,8 @@ class TextTemplateRendererTests(Base, unittest.TestCase): lookup = DummyLookup() instance = self._makeOne(minimal, lookup) result = instance({}, {}) - self.assertTrue(isinstance(result, str)) - self.assertEqual(result, 'Hello.\n') + self.assertTrue(isinstance(result, binary_type)) + self.assertEqual(result, b'Hello.\n') @skip_on('java') def test_call_with_nondict_value(self): @@ -114,8 +115,8 @@ class TextTemplateRendererTests(Base, unittest.TestCase): lookup = DummyLookup() instance = self._makeOne(nonminimal, lookup) result = instance({'name':'Chris'}, {}) - self.assertTrue(isinstance(result, str)) - self.assertEqual(result, 'Hello, Chris!\n') + self.assertTrue(isinstance(result, binary_type)) + self.assertEqual(result, b'Hello, Chris!\n') @skip_on('java') def test_implementation(self): @@ -123,8 +124,8 @@ class TextTemplateRendererTests(Base, unittest.TestCase): lookup = DummyLookup() instance = self._makeOne(minimal, lookup) result = instance.implementation()() - self.assertTrue(isinstance(result, str)) - self.assertEqual(result, 'Hello.\n') + self.assertTrue(isinstance(result, binary_type)) + self.assertEqual(result, b'Hello.\n') class RenderTemplateTests(Base, unittest.TestCase): def _callFUT(self, name, **kw): @@ -135,8 +136,8 @@ class RenderTemplateTests(Base, unittest.TestCase): def test_it(self): minimal = self._getTemplatePath('minimal.txt') result = self._callFUT(minimal) - self.assertTrue(isinstance(result, str)) - self.assertEqual(result, 'Hello.\n') + self.assertTrue(isinstance(result, binary_type)) + self.assertEqual(result, b'Hello.\n') class RenderTemplateToResponseTests(Base, unittest.TestCase): def _callFUT(self, name, **kw): diff --git a/pyramid/tests/test_config/test_init.py b/pyramid/tests/test_config/test_init.py index d3db47369..302a185f2 100644 --- a/pyramid/tests/test_config/test_init.py +++ b/pyramid/tests/test_config/test_init.py @@ -1218,7 +1218,7 @@ class TestConfiguratorDeprecatedFeatures(unittest.TestCase): request_type = self._getRouteRequestIface(config, 'name') wrapper = self._getViewCallable(config, None, request_type) self._assertRoute(config, 'name', 'path') - self.assertEqual(wrapper(None, None).body, 'Hello!') + self.assertEqual(wrapper(None, None).body, b'Hello!') def test_add_route_with_view_attr(self): from pyramid.renderers import null_renderer @@ -1246,7 +1246,7 @@ class TestConfiguratorDeprecatedFeatures(unittest.TestCase): request_type = self._getRouteRequestIface(config, 'name') wrapper = self._getViewCallable(config, None, request_type) self._assertRoute(config, 'name', 'path') - self.assertEqual(wrapper(None, None).body, 'Hello!') + self.assertEqual(wrapper(None, None).body, b'Hello!') def test_add_route_with_view_permission(self): from pyramid.interfaces import IAuthenticationPolicy diff --git a/pyramid/tests/test_config/test_views.py b/pyramid/tests/test_config/test_views.py index eceea94d5..141b60e87 100644 --- a/pyramid/tests/test_config/test_views.py +++ b/pyramid/tests/test_config/test_views.py @@ -2276,7 +2276,7 @@ class TestViewDeriver(unittest.TestCase): self.assertFalse(result is view) self.assertEqual(view.__module__, result.__module__) self.assertEqual(view.__doc__, result.__doc__) - self.assertTrue('instance' in result.__name__) + self.assertTrue('test_views' in result.__name__) self.assertFalse(hasattr(result, '__call_permissive__')) self.assertEqual(result(None, None), response) diff --git a/pyramid/tests/test_httpexceptions.py b/pyramid/tests/test_httpexceptions.py index 60c97b5b4..b58145be8 100644 --- a/pyramid/tests/test_httpexceptions.py +++ b/pyramid/tests/test_httpexceptions.py @@ -256,7 +256,7 @@ class TestWSGIHTTPException(unittest.TestCase): def test_body_template_unicode(self): cls = self._getTargetSubclass() - la = text_('/La Pe\xc3\xb1a', 'utf-8') + la = text_(b'/La Pe\xc3\xb1a', 'utf-8') environ = _makeEnviron(unicodeval=la) exc = cls(body_template='${unicodeval}') start_response = DummyStartResponse() diff --git a/pyramid/tests/test_session.py b/pyramid/tests/test_session.py index 5c6454a38..6d75c7950 100644 --- a/pyramid/tests/test_session.py +++ b/pyramid/tests/test_session.py @@ -263,21 +263,15 @@ class Test_manage_accessed(unittest.TestCase): self.assertEqual(session.response, response) def serialize(data, secret): - try: - from hashlib import sha1 - except ImportError: # pragma: no cover - import sha as sha1 - - try: - import cPickle as pickle - except ImportError: # pragma: no cover - import pickle - import hmac import base64 - pickled = pickle.dumps('123', pickle.HIGHEST_PROTOCOL) - sig = hmac.new(secret, pickled, sha1).hexdigest() - return sig + base64.standard_b64encode(pickled) + from hashlib import sha1 + from pyramid.compat import bytes_ + from pyramid.compat import native_ + from pyramid.compat import pickle + pickled = pickle.dumps(data, pickle.HIGHEST_PROTOCOL) + sig = hmac.new(bytes_(secret), pickled, sha1).hexdigest() + return sig + native_(base64.b64encode(pickled)) class Test_signed_serialize(unittest.TestCase): def _callFUT(self, data, secret): diff --git a/pyramid/tests/test_traversal.py b/pyramid/tests/test_traversal.py index 837b2afc8..cd48d0268 100644 --- a/pyramid/tests/test_traversal.py +++ b/pyramid/tests/test_traversal.py @@ -2,6 +2,7 @@ import unittest from pyramid.testing import cleanUp from pyramid.compat import text_ +from pyramid.compat import native_ from pyramid.compat import text_type class TraversalPathTests(unittest.TestCase): @@ -281,7 +282,7 @@ class ResourceTreeTraverserTests(unittest.TestCase): foo = DummyContext() root = DummyContext(foo) policy = self._makeOne(root) - segment = text_('LaPe\xc3\xb1a', 'utf-8').encode('utf-16') + segment = native_(text_(b'LaPe\xc3\xb1a', 'utf-8'), 'utf-16') environ = self._getEnviron(PATH_INFO='/%s' % segment) request = DummyRequest(environ) from pyramid.exceptions import URLDecodeError @@ -291,7 +292,7 @@ class ResourceTreeTraverserTests(unittest.TestCase): foo = DummyContext() root = DummyContext(foo) policy = self._makeOne(root) - segment = text_('LaPe\xc3\xb1a', 'utf-8').encode('utf-16') + segment = native_(text_(b'LaPe\xc3\xb1a', 'utf-8'), 'utf-16') environ = self._getEnviron(PATH_INFO='/%s' % segment) request = DummyRequest(environ) from pyramid.exceptions import URLDecodeError diff --git a/pyramid/traversal.py b/pyramid/traversal.py index d05805c06..e535da657 100644 --- a/pyramid/traversal.py +++ b/pyramid/traversal.py @@ -13,7 +13,7 @@ from pyramid.interfaces import VH_ROOT_KEY from pyramid.compat import native_ from pyramid.compat import text_ from pyramid.compat import text_type -from pyramid.compat import url_unquote +from pyramid.compat import url_unquote_text from pyramid.compat import is_nonstr_iter from pyramid.encode import url_quote from pyramid.exceptions import URLDecodeError @@ -477,13 +477,12 @@ def traversal_path(path): writing applications in :app:`Pyramid`. """ if isinstance(path, text_type): - path = native_(path, 'ascii') + path = native_(path.encode('ascii')) path = path.strip('/') clean = [] for segment in path.split('/'): - segment = url_unquote(segment) try: - segment = text_(segment, 'utf-8') + segment = url_unquote_text(segment, 'utf-8', 'strict') except UnicodeDecodeError as e: raise URLDecodeError(e.encoding, e.object, e.start, e.end, e.reason) if not segment or segment == '.': @@ -530,10 +529,10 @@ def quote_path_segment(segment, safe=''): try: return _segment_cache[(segment, safe)] except KeyError: - if segment.__class__ is text_type: # isinstance slighly slower (~15%) - result = url_quote(segment.encode('utf-8'), safe) - else: - result = url_quote(native_(segment), safe) + ## if segment.__class__ is text_type: # isinstance slighly slower (~15%) + ## result = url_quote(segment.encode('utf-8'), safe) + ## else: + result = url_quote(native_(segment, 'utf-8'), safe) # we don't need a lock to mutate _segment_cache, as the below # will generate exactly one Python bytecode (STORE_SUBSCR) _segment_cache[(segment, safe)] = result diff --git a/pyramid/urldispatch.py b/pyramid/urldispatch.py index a25bd58a9..696b69f89 100644 --- a/pyramid/urldispatch.py +++ b/pyramid/urldispatch.py @@ -4,10 +4,10 @@ from zope.interface import implementer from pyramid.interfaces import IRoutesMapper from pyramid.interfaces import IRoute -from pyramid.compat import url_unquote +from pyramid.compat import url_unquote_text from pyramid.compat import native_ from pyramid.compat import text_type -from pyramid.compat import text_ +from pyramid.compat import is_nonstr_iter from pyramid.encode import url_quote from pyramid.exceptions import URLDecodeError from pyramid.traversal import traversal_path @@ -140,9 +140,10 @@ def _compile_route(route): if k == star: d[k] = traversal_path(v) else: - encoded = url_unquote(v) try: - d[k] = text_(encoded, 'utf-8') + val = url_unquote_text( + v, encoding='utf-8', errors='strict') + d[k] = val except UnicodeDecodeError as e: raise URLDecodeError( e.encoding, e.object, e.start, e.end, e.reason @@ -158,7 +159,7 @@ def _compile_route(route): for k, v in dict.items(): if isinstance(v, text_type): v = native_(v, 'utf-8') - if k == star and hasattr(v, '__iter__'): + if k == star and is_nonstr_iter(v): v = '/'.join([quote_path_segment(x) for x in v]) elif k != star: try: -- cgit v1.2.3