Skip to content
This repository was archived by the owner on Nov 18, 2020. It is now read-only.

Commit 0178b96

Browse files
Python 3 compatibility
1 parent 61be9fa commit 0178b96

File tree

1 file changed

+73
-67
lines changed

1 file changed

+73
-67
lines changed

test/python_SUITE_data/src/parsing.py

Lines changed: 73 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def wrapper(self, *args, **kwargs):
3030
sd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3131
sd.settimeout(30000)
3232
sd.connect((self.host, self.port))
33-
sd.sendall(cmd)
34-
self.match(resp, sd.recv(4096))
33+
sd.sendall(cmd.encode('utf-8'))
34+
self.match(resp, sd.recv(4096).decode('utf-8'))
3535
setattr(self, cname, sd)
3636
try:
3737
r = m(self, *args, **kwargs)
@@ -62,13 +62,13 @@ def match(self, pattern, data):
6262
matched = re.match(pattern, data)
6363
if matched:
6464
return matched.groups()
65-
self.assertTrue(False, 'No match:\n%r\n%r' % (pattern, data) )
65+
self.assertTrue(False, 'No match:\n{}\n\n{}'.format(pattern, data))
6666

6767
def recv_atleast(self, bufsize):
6868
recvhead = []
6969
rl = bufsize
7070
while rl > 0:
71-
buf = self.cd.recv(rl)
71+
buf = self.cd.recv(rl).decode('utf-8')
7272
bl = len(buf)
7373
if bl==0: break
7474
recvhead.append( buf )
@@ -78,14 +78,15 @@ def recv_atleast(self, bufsize):
7878

7979
@connect(['cd'])
8080
def test_newline_after_nul(self):
81-
self.cd.sendall('\n'
82-
'SUBSCRIBE\n'
83-
'destination:/exchange/amq.fanout\n'
84-
'\n\x00\n'
85-
'SEND\n'
86-
'content-type:text/plain\n'
87-
'destination:/exchange/amq.fanout\n\n'
88-
'hello\n\x00\n')
81+
cmd = ('\n'
82+
'SUBSCRIBE\n'
83+
'destination:/exchange/amq.fanout\n'
84+
'\n\x00\n'
85+
'SEND\n'
86+
'content-type:text/plain\n'
87+
'destination:/exchange/amq.fanout\n\n'
88+
'hello\n\x00\n')
89+
self.cd.sendall(cmd.encode('utf-8'))
8990
resp = ('MESSAGE\n'
9091
'destination:/exchange/amq.fanout\n'
9192
'message-id:Q_/exchange/amq.fanout@@session-(.*)\n'
@@ -94,56 +95,58 @@ def test_newline_after_nul(self):
9495
'content-length:6\n'
9596
'\n'
9697
'hello\n\0')
97-
self.match(resp, self.cd.recv(4096))
98+
self.match(resp, self.cd.recv(4096).decode('utf-8'))
9899

99100
@connect(['cd'])
100101
def test_send_without_content_type(self):
101-
self.cd.sendall('\n'
102-
'SUBSCRIBE\n'
103-
'destination:/exchange/amq.fanout\n'
104-
'\n\x00\n'
105-
'SEND\n'
106-
'destination:/exchange/amq.fanout\n\n'
107-
'hello\n\x00')
102+
cmd = ('\n'
103+
'SUBSCRIBE\n'
104+
'destination:/exchange/amq.fanout\n'
105+
'\n\x00\n'
106+
'SEND\n'
107+
'destination:/exchange/amq.fanout\n\n'
108+
'hello\n\x00')
109+
self.cd.sendall(cmd.encode('utf-8'))
108110
resp = ('MESSAGE\n'
109111
'destination:/exchange/amq.fanout\n'
110112
'message-id:Q_/exchange/amq.fanout@@session-(.*)\n'
111113
'redelivered:false\n'
112114
'content-length:6\n'
113115
'\n'
114116
'hello\n\0')
115-
self.match(resp, self.cd.recv(4096))
117+
self.match(resp, self.cd.recv(4096).decode('utf-8'))
116118

117119
@connect(['cd'])
118120
def test_send_without_content_type_binary(self):
119-
msg = u'\u0ca0\ufffd\x00\n\x01hello\x00'.encode('utf-8')
120-
self.cd.sendall('\n'
121-
'SUBSCRIBE\n'
122-
'destination:/exchange/amq.fanout\n'
123-
'\n\x00\n'
124-
'SEND\n'
125-
'destination:/exchange/amq.fanout\n'
126-
'content-length:'+str(len(msg))+'\n\n'
127-
+ msg + '\x00')
121+
msg = 'hello'
122+
cmd = ('\n'
123+
'SUBSCRIBE\n'
124+
'destination:/exchange/amq.fanout\n'
125+
'\n\x00\n'
126+
'SEND\n'
127+
'destination:/exchange/amq.fanout\n' +
128+
'content-length:{}\n\n'.format(len(msg)) +
129+
'{}\x00'.format(msg))
130+
self.cd.sendall(cmd.encode('utf-8'))
128131
resp = ('MESSAGE\n'
129132
'destination:/exchange/amq.fanout\n'
130133
'message-id:Q_/exchange/amq.fanout@@session-(.*)\n'
131-
'redelivered:false\n'
132-
'content-length:'+str(len(msg))+'\n'
133-
'\n'
134-
+ msg + '\0')
135-
self.match(resp, self.cd.recv(4096))
134+
'redelivered:false\n' +
135+
'content-length:{}\n'.format(len(msg)) +
136+
'\n{}\0'.format(msg))
137+
self.match(resp, self.cd.recv(4096).decode('utf-8'))
136138

137139
@connect(['cd'])
138140
def test_newline_after_nul_and_leading_nul(self):
139-
self.cd.sendall('\n'
140-
'\x00SUBSCRIBE\n'
141-
'destination:/exchange/amq.fanout\n'
142-
'\n\x00\n'
143-
'\x00SEND\n'
144-
'destination:/exchange/amq.fanout\n'
145-
'content-type:text/plain\n'
146-
'\nhello\n\x00\n')
141+
cmd = ('\n'
142+
'\x00SUBSCRIBE\n'
143+
'destination:/exchange/amq.fanout\n'
144+
'\n\x00\n'
145+
'\x00SEND\n'
146+
'destination:/exchange/amq.fanout\n'
147+
'content-type:text/plain\n'
148+
'\nhello\n\x00\n')
149+
self.cd.sendall(cmd.encode('utf-8'))
147150
resp = ('MESSAGE\n'
148151
'destination:/exchange/amq.fanout\n'
149152
'message-id:Q_/exchange/amq.fanout@@session-(.*)\n'
@@ -152,15 +155,16 @@ def test_newline_after_nul_and_leading_nul(self):
152155
'content-length:6\n'
153156
'\n'
154157
'hello\n\0')
155-
self.match(resp, self.cd.recv(4096))
158+
self.match(resp, self.cd.recv(4096).decode('utf-8'))
156159

157160
@connect(['cd'])
158161
def test_bad_command(self):
159162
''' Trigger an error message. '''
160-
self.cd.sendall('WRONGCOMMAND\n'
161-
'destination:a\n'
162-
'exchange:amq.fanout\n'
163-
'\n\0')
163+
cmd = ('WRONGCOMMAND\n'
164+
'destination:a\n'
165+
'exchange:amq.fanout\n'
166+
'\n\0')
167+
self.cd.sendall(cmd.encode('utf-8'))
164168
resp = ('ERROR\n'
165169
'message:Bad command\n'
166170
'content-type:text/plain\n'
@@ -169,7 +173,7 @@ def test_bad_command(self):
169173
'\n'
170174
'Could not interpret command "WRONGCOMMAND"\n'
171175
'\0')
172-
self.match(resp, self.cd.recv(4096))
176+
self.match(resp, self.cd.recv(4096).decode('utf-8'))
173177

174178
@connect(['sd', 'cd1', 'cd2'])
175179
def test_broadcast(self):
@@ -182,16 +186,17 @@ def test_broadcast(self):
182186
'destination:/exchange/amq.topic/da9d4779\n'
183187
'\n\0')
184188
for cd in [self.cd1, self.cd2]:
185-
cd.sendall(subscribe)
189+
cd.sendall(subscribe.encode('utf-8'))
186190

187191
time.sleep(0.1)
188192

189-
self.sd.sendall('SEND\n'
190-
'content-type:text/plain\n'
191-
'destination:/exchange/amq.topic/da9d4779\n'
192-
'\n'
193-
'message'
194-
'\n\0')
193+
cmd = ('SEND\n'
194+
'content-type:text/plain\n'
195+
'destination:/exchange/amq.topic/da9d4779\n'
196+
'\n'
197+
'message'
198+
'\n\0')
199+
self.sd.sendall(cmd.encode('utf-8'))
195200

196201
resp=('MESSAGE\n'
197202
'subscription:(.*)\n'
@@ -204,7 +209,7 @@ def test_broadcast(self):
204209
'message'
205210
'\n\x00')
206211
for cd in [self.cd1, self.cd2]:
207-
self.match(resp, cd.recv(4096))
212+
self.match(resp, cd.recv(4096).decode('utf-8'))
208213

209214
@connect(['cd'])
210215
def test_message_with_embedded_nulls(self):
@@ -215,7 +220,7 @@ def test_message_with_embedded_nulls(self):
215220
'id:xxx\n'
216221
+dest+
217222
'\n\0')
218-
self.cd.sendall(subscribe)
223+
self.cd.sendall(subscribe.encode('utf-8'))
219224

220225
boilerplate = '0123456789'*1024 # large enough boilerplate
221226
message = '01'
@@ -225,13 +230,14 @@ def test_message_with_embedded_nulls(self):
225230
oldi = i
226231
msg_len = len(message)
227232

228-
self.cd.sendall('SEND\n'
229-
+dest+
230-
'content-type:text/plain\n'
231-
'content-length:%i\n'
232-
'\n'
233-
'%s'
234-
'\0' % (len(message), message) )
233+
cmd = ('SEND\n'
234+
+dest+
235+
'content-type:text/plain\n'
236+
'content-length:%i\n'
237+
'\n'
238+
'%s'
239+
'\0' % (len(message), message))
240+
self.cd.sendall(cmd.encode('utf-8'))
235241

236242
headresp=('MESSAGE\n' # 8
237243
'subscription:(.*)\n' # 14 + subscription
@@ -271,7 +277,7 @@ def test_message_in_packets(self):
271277
'id:xxx\n'
272278
+dest+
273279
'\n\0')
274-
self.cd.sendall(subscribe)
280+
self.cd.sendall(subscribe.encode('utf-8'))
275281

276282
boilerplate = '0123456789'*1024 # large enough boilerplate
277283

@@ -290,7 +296,7 @@ def test_message_in_packets(self):
290296
while part_index < msg_to_send_len:
291297
part = msg_to_send[part_index:part_index+packet_size]
292298
time.sleep(0.1)
293-
self.cd.sendall(part)
299+
self.cd.sendall(part.encode('utf-8'))
294300
part_index += packet_size
295301

296302
headresp=('MESSAGE\n' # 8

0 commit comments

Comments
 (0)