Merge lp:~cjwatson/lazr.restful/py3-request-body into lp:lazr.restful

Proposed by Colin Watson
Status: Merged
Merged at revision: 250
Proposed branch: lp:~cjwatson/lazr.restful/py3-request-body
Merge into: lp:lazr.restful
Diff against target: 197 lines (+25/-24)
5 files modified
src/lazr/restful/docs/webservice-marshallers.rst (+2/-2)
src/lazr/restful/docs/webservice.rst (+16/-16)
src/lazr/restful/marshallers.py (+2/-2)
src/lazr/restful/testing/webservice.py (+3/-3)
src/lazr/restful/tests/test_webservice.py (+2/-1)
To merge this branch: bzr merge lp:~cjwatson/lazr.restful/py3-request-body
Reviewer Review Type Date Requested Status
Tom Wardill (community) Approve
Review via email: mp+387899@code.launchpad.net

Commit message

Adjust request creation for Python 3.

Description of the change

The body_instream argument to BaseRequest (and its subclasses) is supposed to be a binary file object.

To post a comment you must log in.
Revision history for this message
Tom Wardill (twom) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/lazr/restful/docs/webservice-marshallers.rst'
2--- src/lazr/restful/docs/webservice-marshallers.rst 2020-06-30 15:52:03 +0000
3+++ src/lazr/restful/docs/webservice-marshallers.rst 2020-07-22 23:16:15 +0000
4@@ -457,8 +457,8 @@
5 Since multipart/form-data can be used to upload data, file-like objects
6 are read.
7
8- >>> from cStringIO import StringIO
9- >>> marshaller.marshall_from_request(StringIO('A line of data'))
10+ >>> from io import BytesIO
11+ >>> marshaller.marshall_from_request(BytesIO(b'A line of data'))
12 'A line of data'
13
14 Bytes field used in an entry are stored in the librarian, so their
15
16=== modified file 'src/lazr/restful/docs/webservice.rst'
17--- src/lazr/restful/docs/webservice.rst 2020-02-04 13:17:32 +0000
18+++ src/lazr/restful/docs/webservice.rst 2020-07-22 23:16:15 +0000
19@@ -1275,8 +1275,8 @@
20 operation's definition doesn't set send_modified_event to True, no
21 event will be sent and modified_cookbook_set() won't be called.
22
23- >>> body = ("ws.op=create_cookbook&title=Beard%20on%20Bread&"
24- ... "author_name=James%20Beard")
25+ >>> body = (b"ws.op=create_cookbook&title=Beard%20on%20Bread&"
26+ ... b"author_name=James%20Beard")
27 >>> request = create_web_service_request(
28 ... '/beta/cookbooks', 'POST', body,
29 ... {'CONTENT_TYPE' : 'application/x-www-form-urlencoded'})
30@@ -1290,8 +1290,8 @@
31
32 Here we create a cookbook for a new author.
33
34- >>> body = ("ws.op=create_cookbook&title=Everyday%20Greens&"
35- ... "author_name=Deborah%20Madison")
36+ >>> body = (b"ws.op=create_cookbook&title=Everyday%20Greens&"
37+ ... b"author_name=Deborah%20Madison")
38 >>> request = create_web_service_request(
39 ... '/beta/cookbooks', 'POST', body,
40 ... {'CONTENT_TYPE' : 'application/x-www-form-urlencoded'})
41@@ -1315,7 +1315,7 @@
42 this operation's definition does set send_modified_event to True, an
43 event will be sent and modified_cookbook_set() will be called.
44
45- >>> body = "ws.op=make_more_interesting"
46+ >>> body = b"ws.op=make_more_interesting"
47 >>> request = create_web_service_request(
48 ... '/beta/cookbooks/Everyday%20Greens', 'POST', body,
49 ... {'CONTENT_TYPE' : 'application/x-www-form-urlencoded'})
50@@ -1389,8 +1389,8 @@
51 For instance, creating a new cookbook and making a request to its URL
52 will wrap it into an ``EntryResource``.
53
54- >>> body = ("ws.op=create_cookbook&title=Feijoada&"
55- ... "author_name=Fernando%20Yokota")
56+ >>> body = (b"ws.op=create_cookbook&title=Feijoada&"
57+ ... b"author_name=Fernando%20Yokota")
58 >>> request = create_web_service_request(
59 ... '/beta/cookbooks', 'POST', body,
60 ... {'CONTENT_TYPE' : 'application/x-www-form-urlencoded'})
61@@ -1795,7 +1795,7 @@
62 set the field value to 'tag:launchpad.net:2008:redacted'.
63
64 >>> headers = {'CONTENT_TYPE' : 'application/json'}
65- >>> body = simplejson.dumps(author)
66+ >>> body = simplejson.dumps(author).encode('UTF-8')
67 >>> put_request = create_web_service_request(
68 ... beard_url, body=body, environ=headers, method='PUT')
69 >>> put_request.traverse(app)()
70@@ -1808,7 +1808,7 @@
71 >>> author['favorite_recipe_link'] = 'http://' + quote(
72 ... 'api.cookbooks.dev/beta/cookbooks/'
73 ... 'The Joy of Cooking/recipes/Roast chicken')
74- >>> body = simplejson.dumps(author)
75+ >>> body = simplejson.dumps(author).encode('UTF-8')
76 >>> put_request = create_web_service_request(
77 ... beard_url, body=body, environ=headers, method='PUT')
78 >>> put_request.traverse(app)()
79@@ -1828,7 +1828,7 @@
80
81 >>> author['favorite_recipe_link'] = (
82 ... 'http://api.cookbooks.dev' + private_recipe_url)
83- >>> body = simplejson.dumps(author)
84+ >>> body = simplejson.dumps(author).encode('UTF-8')
85 >>> put_request = create_web_service_request(
86 ... beard_url, body=body, environ=headers, method='PUT')
87 >>> print(put_request.traverse(app)())
88@@ -1869,7 +1869,7 @@
89 A cookbook can be given a cover with PUT.
90
91 >>> headers = {'CONTENT_TYPE' : 'image/png'}
92- >>> body = 'Pretend this is an image.'
93+ >>> body = b'Pretend this is an image.'
94 >>> put_request = create_web_service_request(
95 ... cover_url, body=body, environ=headers, method='PUT')
96 >>> file_resource = put_request.traverse(app)
97@@ -1958,8 +1958,8 @@
98 non-ASCII characters.
99
100 >>> headers = {'CONTENT_TYPE' : 'application/json'}
101- >>> body = '''{"name" : "The Joy of Cooking (revised)",
102- ... "cuisine" : "\xd7\x97\xd7\x95\xd7\x9e\xd7\x95\xd7\xa1"}'''
103+ >>> body = b'''{"name" : "The Joy of Cooking (revised)",
104+ ... "cuisine" : "\xd7\x97\xd7\x95\xd7\x9e\xd7\x95\xd7\xa1"}'''
105
106 >>> patch_request = create_web_service_request(
107 ... '/beta/cookbooks/The%20Joy%20of%20Cooking', body=body,
108@@ -1993,7 +1993,7 @@
109 intercepted by the modified_cookbook() listener.
110
111 >>> joy['name'] = 'The Joy of Cooking'
112- >>> body = simplejson.dumps(joy)
113+ >>> body = simplejson.dumps(joy).encode('UTF-8')
114 >>> put_request = create_web_service_request(
115 ... '/beta/cookbooks/The%20Joy%20of%20Cooking%20%28revised%29',
116 ... body=body, environ=headers, method='PUT')
117@@ -2032,8 +2032,8 @@
118 ... representation = {'author_link' : new_author_link}
119 ... resource = create_web_service_request(
120 ... '/beta/cookbooks/The%20Joy%20of%20Cooking',
121- ... body=simplejson.dumps(representation), environ=headers,
122- ... method='PATCH', hostname=host).traverse(app)
123+ ... body=simplejson.dumps(representation).encode('UTF-8'),
124+ ... environ=headers, method='PATCH', hostname=host).traverse(app)
125 ... return resource()
126 >>> path = '/beta/authors/Julia%20Child'
127
128
129=== modified file 'src/lazr/restful/marshallers.py'
130--- src/lazr/restful/marshallers.py 2020-06-30 15:52:03 +0000
131+++ src/lazr/restful/marshallers.py 2020-07-22 23:16:15 +0000
132@@ -25,8 +25,8 @@
133 ]
134
135 from datetime import datetime
136+from io import BytesIO
137 import pytz
138-from StringIO import StringIO
139
140 import simplejson
141 from six.moves.urllib.parse import unquote
142@@ -121,7 +121,7 @@
143 server_url = self.request.get('SERVER_URL')
144 if server_url is not None:
145 environ['SERVER_URL'] = server_url
146- request = config.createRequest(StringIO(), environ)
147+ request = config.createRequest(BytesIO(), environ)
148 request.setTraversalStack(path_parts)
149 root = request.publication.getApplication(self.request)
150 return request.traverse(root)
151
152=== modified file 'src/lazr/restful/testing/webservice.py'
153--- src/lazr/restful/testing/webservice.py 2020-02-04 21:53:16 +0000
154+++ src/lazr/restful/testing/webservice.py 2020-07-22 23:16:15 +0000
155@@ -20,7 +20,7 @@
156 'WebServiceTestPublication',
157 ]
158
159-from cStringIO import StringIO
160+from io import BytesIO
161 import simplejson
162 import sys
163 from types import ModuleType
164@@ -81,10 +81,10 @@
165 if environ is not None:
166 test_environ.update(environ)
167 if body is None:
168- body_instream = StringIO('')
169+ body_instream = BytesIO(b'')
170 else:
171 test_environ['CONTENT_LENGTH'] = len(body)
172- body_instream = StringIO(body)
173+ body_instream = BytesIO(body)
174 request = config.createRequest(body_instream, test_environ)
175
176 request.processInputs()
177
178=== modified file 'src/lazr/restful/tests/test_webservice.py'
179--- src/lazr/restful/tests/test_webservice.py 2020-02-19 16:09:10 +0000
180+++ src/lazr/restful/tests/test_webservice.py 2020-07-22 23:16:15 +0000
181@@ -8,6 +8,7 @@
182
183 from contextlib import contextmanager
184 from cStringIO import StringIO
185+from io import BytesIO
186 from lxml import etree
187 from operator import attrgetter
188 from textwrap import dedent
189@@ -156,7 +157,7 @@
190 def request(self, media_type=None):
191 media_type = media_type or self.default_media_type
192 request = getUtility(IWebServiceConfiguration).createRequest(
193- StringIO(""), {'HTTP_ACCEPT' : media_type})
194+ BytesIO(b""), {'HTTP_ACCEPT' : media_type})
195 newInteraction(request)
196 yield request
197 endInteraction()

Subscribers

People subscribed via source and target branches