From 0ee995b76cb509847b1c87234933fef7f197a998 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Thu, 6 Nov 2014 09:18:19 +0200 Subject: [PATCH 1/4] Update to latest bootstrap.py Downloaded from http://downloads.buildout.org/2/bootstrap.py --- bootstrap.py | 60 +++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/bootstrap.py b/bootstrap.py index 1b28969..ed57894 100644 --- a/bootstrap.py +++ b/bootstrap.py @@ -56,6 +56,9 @@ "file to be used.")) parser.add_option("-f", "--find-links", help=("Specify a URL to search for buildout releases")) +parser.add_option("--allow-site-packages", + action="store_true", default=False, + help=("Let bootstrap.py use existing site packages")) options, args = parser.parse_args() @@ -63,32 +66,38 @@ ###################################################################### # load/install setuptools -to_reload = False try: - import pkg_resources - import setuptools + if options.allow_site_packages: + import setuptools + import pkg_resources + from urllib.request import urlopen except ImportError: - ez = {} - - try: - from urllib.request import urlopen - except ImportError: - from urllib2 import urlopen - - # XXX use a more permanent ez_setup.py URL when available. - exec(urlopen('https://bitbucket.org/pypa/setuptools/raw/0.7.2/ez_setup.py' - ).read(), ez) - setup_args = dict(to_dir=tmpeggs, download_delay=0) - ez['use_setuptools'](**setup_args) - - if to_reload: - reload(pkg_resources) - import pkg_resources - # This does not (always?) update the default working set. We will - # do it. - for path in sys.path: - if path not in pkg_resources.working_set.entries: - pkg_resources.working_set.add_entry(path) + from urllib2 import urlopen + +ez = {} +exec(urlopen('https://bootstrap.pypa.io/ez_setup.py').read(), ez) + +if not options.allow_site_packages: + # ez_setup imports site, which adds site packages + # this will remove them from the path to ensure that incompatible versions + # of setuptools are not in the path + import site + # inside a virtualenv, there is no 'getsitepackages'. + # We can't remove these reliably + if hasattr(site, 'getsitepackages'): + for sitepackage_path in site.getsitepackages(): + sys.path[:] = [x for x in sys.path if sitepackage_path not in x] + +setup_args = dict(to_dir=tmpeggs, download_delay=0) +ez['use_setuptools'](**setup_args) +import setuptools +import pkg_resources + +# This does not (always?) update the default working set. We will +# do it. +for path in sys.path: + if path not in pkg_resources.working_set.entries: + pkg_resources.working_set.add_entry(path) ###################################################################### # Install buildout @@ -149,8 +158,7 @@ def _final_version(parsed_version): import subprocess if subprocess.call(cmd, env=dict(os.environ, PYTHONPATH=setuptools_path)) != 0: raise Exception( - "Failed to execute command:\n%s", - repr(cmd)[1:-1]) + "Failed to execute command:\n%s" % repr(cmd)[1:-1]) ###################################################################### # Import and run buildout From 35f4c29a3f0c6f946796cf3ac3fd056de69a72df Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Fri, 21 Nov 2014 16:10:54 -0500 Subject: [PATCH 2/4] encode unicode redirect URLs automatically --- bobo/README.txt | 2 ++ bobo/src/bobo.py | 12 ++++++++- .../src/bobodoctestumentation/more.txt | 25 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/bobo/README.txt b/bobo/README.txt index bdddc64..acb12e4 100644 --- a/bobo/README.txt +++ b/bobo/README.txt @@ -22,6 +22,8 @@ To learn more. visit: http://bobo.digicool.com Change History ============== +- Bobo will encode Unicode URLs for redirects automatically. + - Bobo will pass resource function arguments from data in JSON request bodies. 2.1.1 2014-07-06 diff --git a/bobo/src/bobo.py b/bobo/src/bobo.py index 3023b3f..c30f22c 100644 --- a/bobo/src/bobo.py +++ b/bobo/src/bobo.py @@ -249,8 +249,15 @@ def __call__(self, environ, start_response): # Maybe middleware can be more tricky? request.charset = 'utf8' + def start_bobo_response(status, headers): + headers = list(headers or ()) + for i, (k, v) in enumerate(headers): + if k.lower() == "location" and isinstance(v, unicode): + headers[i] = k, v.encode("utf-8") + start_response(status, headers) + return self.bobo_response(request, request.path_info, request.method - )(environ, start_response) + )(environ, start_bobo_response) def build_response(self, request, method, data): """Build a response object from raw data. @@ -334,6 +341,9 @@ def _err_response(status, method, title, message, headers=()): """ +def _massage_headers(headers): + return headers + def redirect(url, status=302, body=None, content_type="text/html; charset=UTF-8"): """Generate a response to redirect to a URL. diff --git a/bobodoctestumentation/src/bobodoctestumentation/more.txt b/bobodoctestumentation/src/bobodoctestumentation/more.txt index 8c8fb52..deb79bb 100644 --- a/bobodoctestumentation/src/bobodoctestumentation/more.txt +++ b/bobodoctestumentation/src/bobodoctestumentation/more.txt @@ -1087,3 +1087,28 @@ with an Allow header. >>> app.request('/users/54321', ... method="OPTIONS", status=405).headers["Allow"] 'GET, HEAD, POST, PUT' + + +Automatic encoding of redirect destinations +------------------------------------------- + +Since URLs are often computed based on request data, it's easy for +applications to generate Unicode URLs. If we get this, UTF-8 is +generated automatically:: + + import bobo + + @bobo.get("/redirect-me") + def redirect(): + return bobo.redirect(u"http://www.\u0113xample.com/") + + +.. -> src + + >>> update_module('redirection', src) + >>> app = webtest.TestApp(bobo.Application( + ... bobo_resources='redirection')) + + >>> response = app.get("/redirect-me", status=302) + >>> response.headers["location"] + 'http://www.\xc4\x93xample.com/' From ab62e19e90ebc7223a9075d2135b14a3508ed9da Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Fri, 21 Nov 2014 16:26:13 -0500 Subject: [PATCH 3/4] simplify; only encode for bobo.redirect; separate documentation from test --- bobo/src/bobo.py | 11 +++------- .../src/bobodoctestumentation/main.test | 8 +++++++ .../src/bobodoctestumentation/more.txt | 21 ++----------------- 3 files changed, 13 insertions(+), 27 deletions(-) diff --git a/bobo/src/bobo.py b/bobo/src/bobo.py index c30f22c..ca7b4f3 100644 --- a/bobo/src/bobo.py +++ b/bobo/src/bobo.py @@ -249,15 +249,8 @@ def __call__(self, environ, start_response): # Maybe middleware can be more tricky? request.charset = 'utf8' - def start_bobo_response(status, headers): - headers = list(headers or ()) - for i, (k, v) in enumerate(headers): - if k.lower() == "location" and isinstance(v, unicode): - headers[i] = k, v.encode("utf-8") - start_response(status, headers) - return self.bobo_response(request, request.path_info, request.method - )(environ, start_bobo_response) + )(environ, start_response) def build_response(self, request, method, data): """Build a response object from raw data. @@ -355,6 +348,8 @@ def redirect(url, status=302, body=None, """ if body is None: body = u'See %s' % url + if isinstance(url, unicode): + url = url.encode('utf-8') response = webob.Response(status=status, headerlist=[('Location', url)]) response.content_type = content_type response.unicode_body = body diff --git a/bobodoctestumentation/src/bobodoctestumentation/main.test b/bobodoctestumentation/src/bobodoctestumentation/main.test index cba04f6..c9773bf 100644 --- a/bobodoctestumentation/src/bobodoctestumentation/main.test +++ b/bobodoctestumentation/src/bobodoctestumentation/main.test @@ -336,3 +336,11 @@ Ordering >>> app = makeapp(bobo_resources='bobo.testmodule1') >>> app.get('/o').text 'f3' + + +Automatic encoding of redirect destinations +------------------------------------------- + + >>> response = bobo.redirect(u"http://www.\u0113xample.com/") + >>> response.headers["location"] + 'http://www.\xc4\x93xample.com/' diff --git a/bobodoctestumentation/src/bobodoctestumentation/more.txt b/bobodoctestumentation/src/bobodoctestumentation/more.txt index deb79bb..34408cd 100644 --- a/bobodoctestumentation/src/bobodoctestumentation/more.txt +++ b/bobodoctestumentation/src/bobodoctestumentation/more.txt @@ -1093,22 +1093,5 @@ Automatic encoding of redirect destinations ------------------------------------------- Since URLs are often computed based on request data, it's easy for -applications to generate Unicode URLs. If we get this, UTF-8 is -generated automatically:: - - import bobo - - @bobo.get("/redirect-me") - def redirect(): - return bobo.redirect(u"http://www.\u0113xample.com/") - - -.. -> src - - >>> update_module('redirection', src) - >>> app = webtest.TestApp(bobo.Application( - ... bobo_resources='redirection')) - - >>> response = app.get("/redirect-me", status=302) - >>> response.headers["location"] - 'http://www.\xc4\x93xample.com/' +applications to generate Unicode URLs. For this reason, unicode URL's +passed to ``bobo.redirect`` are UTF-8 encoded. From 57fc685ebd536e8111ecff8aeda4383f79b427f4 Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Fri, 21 Nov 2014 16:30:43 -0500 Subject: [PATCH 4/4] remove turd --- bobo/src/bobo.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/bobo/src/bobo.py b/bobo/src/bobo.py index ca7b4f3..78e38c9 100644 --- a/bobo/src/bobo.py +++ b/bobo/src/bobo.py @@ -334,9 +334,6 @@ def _err_response(status, method, title, message, headers=()): """ -def _massage_headers(headers): - return headers - def redirect(url, status=302, body=None, content_type="text/html; charset=UTF-8"): """Generate a response to redirect to a URL.