Merge ~cjwatson/lazr.restful:remove-py2-only-code into lazr.restful:main

Proposed by Colin Watson
Status: Merged
Merged at revision: 29a34f164aee7de6def133981ec90a85e37a3632
Proposed branch: ~cjwatson/lazr.restful:remove-py2-only-code
Merge into: lazr.restful:main
Diff against target: 221 lines (+19/-70)
8 files modified
setup.py (+1/-3)
src/lazr/restful/_resource.py (+4/-17)
src/lazr/restful/declarations.py (+0/-12)
src/lazr/restful/docs/webservice-declarations.rst (+2/-3)
src/lazr/restful/docs/webservice-marshallers.rst (+0/-5)
src/lazr/restful/example/base/filemanager.py (+3/-13)
src/lazr/restful/testing/helpers.py (+1/-4)
src/lazr/restful/testing/webservice.py (+8/-13)
Reviewer Review Type Date Requested Status
Jürgen Gmach Approve
Review via email: mp+413795@code.launchpad.net

Commit message

Remove some Python-2-only code and dependencies

To post a comment you must log in.
Revision history for this message
Jürgen Gmach (jugmac00) wrote :

LGTM - many occurrences of six left, but I assume there was no intention to remove six with this MP.

review: Approve
Revision history for this message
Colin Watson (cjwatson) wrote :

Yeah, I have a whole series that ultimately involves removing six, but it was a bit much to propose all at once.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/setup.py b/setup.py
2index 7f0ed89..1017963 100755
3--- a/setup.py
4+++ b/setup.py
5@@ -65,7 +65,6 @@ setup(
6 "six>=1.13.0",
7 "testtools",
8 "van.testing",
9- 'wsgiref; python_version < "3"',
10 "zope.component [zcml]",
11 "zope.configuration",
12 "zope.datetime",
13@@ -74,8 +73,7 @@ setup(
14 "zope.pagetemplate",
15 "zope.processlifetime",
16 "zope.proxy",
17- 'zope.publisher; python_version < "3"',
18- 'zope.publisher>=6.0.0; python_version >= "3"',
19+ "zope.publisher>=6.0.0",
20 "zope.schema",
21 "zope.security",
22 "zope.traversing",
23diff --git a/src/lazr/restful/_resource.py b/src/lazr/restful/_resource.py
24index 45f717e..2d4a486 100644
25--- a/src/lazr/restful/_resource.py
26+++ b/src/lazr/restful/_resource.py
27@@ -29,6 +29,8 @@ __all__ = [
28 from collections import OrderedDict
29 from email.utils import formatdate
30 import copy
31+import hashlib
32+import html
33 from operator import itemgetter
34 import os
35 import simplejson
36@@ -36,21 +38,6 @@ import simplejson.encoder
37 import sys
38 import time
39
40-# Import SHA in a way compatible with both Python 2.4 and Python 2.6.
41-try:
42- import hashlib
43-
44- sha_constructor = hashlib.sha1
45-except ImportError:
46- import sha
47-
48- sha_constructor = sha.new
49-
50-try:
51- from html import escape
52-except ImportError:
53- from cgi import escape
54-
55 from zope.component import (
56 adapter,
57 getAdapters,
58@@ -167,7 +154,7 @@ def decode_value(value):
59
60 def _default_html_renderer(value):
61 """The default technique for rendering a value as an HTML snippet."""
62- return escape(str(value), quote=False)
63+ return html.escape(str(value), quote=False)
64
65
66 @adapter(Interface, IField, IWebServiceClientRequest)
67@@ -442,7 +429,7 @@ class HTTPResource:
68
69 core_hashes = []
70 for core in etag_cores:
71- hash_object = sha_constructor()
72+ hash_object = hashlib.sha1()
73 hash_object.update(core)
74 core_hashes.append(hash_object)
75
76diff --git a/src/lazr/restful/declarations.py b/src/lazr/restful/declarations.py
77index 886eedb..1ea9e21 100644
78--- a/src/lazr/restful/declarations.py
79+++ b/src/lazr/restful/declarations.py
80@@ -56,7 +56,6 @@ from zope.schema import (
81 from zope.schema.interfaces import (
82 IField,
83 IObject,
84- IText,
85 )
86 from zope.security.checker import CheckerPublic
87 from zope.traversing.browser import absoluteURL
88@@ -707,17 +706,6 @@ def _update_default_and_required_params(params, method_info):
89 # a default, set it to the same as the method.
90 if name in optional and param_def.default is None:
91 default = optional[name]
92-
93- # This is to work around the fact that all strings in
94- # zope schema are expected to be unicode, whereas it's
95- # really possible that the method's default is a simple
96- # string.
97- if (
98- six.PY2
99- and isinstance(default, str)
100- and IText.providedBy(param_def)
101- ):
102- default = unicode(default) # noqa: F821
103 param_def.default = default
104 param_def.required = False
105 elif name in required and param_def.default is not None:
106diff --git a/src/lazr/restful/docs/webservice-declarations.rst b/src/lazr/restful/docs/webservice-declarations.rst
107index 2db2286..964f5ff 100644
108--- a/src/lazr/restful/docs/webservice-declarations.rst
109+++ b/src/lazr/restful/docs/webservice-declarations.rst
110@@ -910,9 +910,8 @@ The created interface is named with 'Entry' appended to the original
111 name, and is in the same module
112
113 >>> import sys
114- >>> entry_interface.__module__ == (
115- ... 'builtins' if sys.version_info[0] >= 3 else '__builtin__')
116- True
117+ >>> entry_interface.__module__
118+ 'builtins'
119 >>> entry_interface.__name__
120 'IBookEntry_beta'
121
122diff --git a/src/lazr/restful/docs/webservice-marshallers.rst b/src/lazr/restful/docs/webservice-marshallers.rst
123index 20028f3..0d18d2a 100644
124--- a/src/lazr/restful/docs/webservice-marshallers.rst
125+++ b/src/lazr/restful/docs/webservice-marshallers.rst
126@@ -31,11 +31,6 @@ across Python versions.
127 ... return '"%s"' % value
128 ... else:
129 ... return "'%s'" % value.replace("'", "\\'")
130- ... elif isinstance(value, bytes):
131- ... if six.PY2:
132- ... return 'b%r' % value
133- ... else:
134- ... return repr(value)
135 ... else:
136 ... return repr(value)
137
138diff --git a/src/lazr/restful/example/base/filemanager.py b/src/lazr/restful/example/base/filemanager.py
139index 781bf88..98c70ce 100644
140--- a/src/lazr/restful/example/base/filemanager.py
141+++ b/src/lazr/restful/example/base/filemanager.py
142@@ -5,20 +5,10 @@
143 __all__ = ["FileManager", "ManagedFileResource"]
144
145 import datetime
146-
147-# Import SHA in a way compatible with both Python 2.4 and Python 2.6.
148-try:
149- import hashlib
150-
151- sha_constructor = hashlib.sha1
152-except ImportError:
153- import sha
154-
155- sha_constructor = sha.new
156-
157-from zope.interface import implementer
158+import hashlib
159
160 import grokcore.component
161+from zope.interface import implementer
162
163 from lazr.restful import ReadOnlyResource
164 from lazr.restful.example.base.interfaces import IFileManager
165@@ -61,7 +51,7 @@ class ManagedFileResource(ReadOnlyResource):
166 self.mediaType = mediaType
167 self.filename = filename
168 self.last_modified = last_modified
169- sum = sha_constructor()
170+ sum = hashlib.sha1()
171 sum.update(representation)
172 self.etag = sum.hexdigest()
173
174diff --git a/src/lazr/restful/testing/helpers.py b/src/lazr/restful/testing/helpers.py
175index fc9c8b1..c0dd63c 100644
176--- a/src/lazr/restful/testing/helpers.py
177+++ b/src/lazr/restful/testing/helpers.py
178@@ -52,10 +52,7 @@ def encode_response(response):
179
180 :param response: an httplib HTTPResponse object.
181 """
182- response_unicode = str(response)
183- if isinstance(response_unicode, bytes): # Python 2
184- response_unicode = response_unicode.decode("utf-8")
185- return encode_unicode(response_unicode)
186+ return encode_unicode(str(response))
187
188
189 def encode_unicode(unicode_string):
190diff --git a/src/lazr/restful/testing/webservice.py b/src/lazr/restful/testing/webservice.py
191index a4405b1..148fd4c 100644
192--- a/src/lazr/restful/testing/webservice.py
193+++ b/src/lazr/restful/testing/webservice.py
194@@ -572,19 +572,14 @@ class WebServiceResponseWrapper(ProxyBase):
195 return self.getheader(key, default)
196
197 def __str__(self):
198- if sys.version_info[0] >= 3:
199- # Reconstructing the original list of headers seems to involve a
200- # bit more effort on Python 3.
201- def normalize(key):
202- """Normalize an HTTP header name to title case."""
203- return "-".join(s.title() for s in key.split("-"))
204-
205- headers = [
206- "%s: %s\n" % (normalize(key), value)
207- for key, value in self.msg.items()
208- ]
209- else:
210- headers = self.msg.headers
211+ def normalize(key):
212+ """Normalize an HTTP header name to title case."""
213+ return "-".join(s.title() for s in key.split("-"))
214+
215+ headers = [
216+ "%s: %s\n" % (normalize(key), value)
217+ for key, value in self.msg.items()
218+ ]
219 body = self.body.decode("UTF-8")
220 return "HTTP/1.1 %s %s\n%s\n%s" % (
221 self.status,

Subscribers

People subscribed via source and target branches