Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b9328f

Browse files
committed
(Merge 3.4) asynchat: PEP8-ify the code
2 parents e8209da + fd5d1b5 commit 7b9328f

2 files changed

Lines changed: 68 additions & 57 deletions

File tree

Lib/asynchat.py

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@
4949
from collections import deque
5050

5151

52-
class async_chat (asyncore.dispatcher):
52+
class async_chat(asyncore.dispatcher):
5353
"""This is an abstract class. You must derive from this class, and add
5454
the two methods collect_incoming_data() and found_terminator()"""
5555

5656
# these are overridable defaults
5757

58-
ac_in_buffer_size = 65536
59-
ac_out_buffer_size = 65536
58+
ac_in_buffer_size = 65536
59+
ac_out_buffer_size = 65536
6060

6161
# we don't want to enable the use of encoding by default, because that is a
6262
# sign of an application bug that we don't want to pass silently
6363

64-
use_encoding = 0
65-
encoding = 'latin-1'
64+
use_encoding = 0
65+
encoding = 'latin-1'
6666

67-
def __init__ (self, sock=None, map=None):
67+
def __init__(self, sock=None, map=None):
6868
# for string terminator matching
6969
self.ac_in_buffer = b''
7070

@@ -76,7 +76,7 @@ def __init__ (self, sock=None, map=None):
7676
# we toss the use of the "simple producer" and replace it with
7777
# a pure deque, which the original fifo was a wrapping of
7878
self.producer_fifo = deque()
79-
asyncore.dispatcher.__init__ (self, sock, map)
79+
asyncore.dispatcher.__init__(self, sock, map)
8080

8181
def collect_incoming_data(self, data):
8282
raise NotImplementedError("must be implemented in subclass")
@@ -92,24 +92,27 @@ def _get_data(self):
9292
def found_terminator(self):
9393
raise NotImplementedError("must be implemented in subclass")
9494

95-
def set_terminator (self, term):
96-
"Set the input delimiter. Can be a fixed string of any length, an integer, or None"
95+
def set_terminator(self, term):
96+
"""Set the input delimiter.
97+
98+
Can be a fixed string of any length, an integer, or None.
99+
"""
97100
if isinstance(term, str) and self.use_encoding:
98101
term = bytes(term, self.encoding)
99102
self.terminator = term
100103

101-
def get_terminator (self):
104+
def get_terminator(self):
102105
return self.terminator
103106

104107
# grab some more data from the socket,
105108
# throw it to the collector method,
106109
# check for the terminator,
107110
# if found, transition to the next state.
108111

109-
def handle_read (self):
112+
def handle_read(self):
110113

111114
try:
112-
data = self.recv (self.ac_in_buffer_size)
115+
data = self.recv(self.ac_in_buffer_size)
113116
except OSError as why:
114117
self.handle_error()
115118
return
@@ -128,17 +131,17 @@ def handle_read (self):
128131
terminator = self.get_terminator()
129132
if not terminator:
130133
# no terminator, collect it all
131-
self.collect_incoming_data (self.ac_in_buffer)
134+
self.collect_incoming_data(self.ac_in_buffer)
132135
self.ac_in_buffer = b''
133136
elif isinstance(terminator, int):
134137
# numeric terminator
135138
n = terminator
136139
if lb < n:
137-
self.collect_incoming_data (self.ac_in_buffer)
140+
self.collect_incoming_data(self.ac_in_buffer)
138141
self.ac_in_buffer = b''
139142
self.terminator = self.terminator - lb
140143
else:
141-
self.collect_incoming_data (self.ac_in_buffer[:n])
144+
self.collect_incoming_data(self.ac_in_buffer[:n])
142145
self.ac_in_buffer = self.ac_in_buffer[n:]
143146
self.terminator = 0
144147
self.found_terminator()
@@ -155,32 +158,34 @@ def handle_read (self):
155158
if index != -1:
156159
# we found the terminator
157160
if index > 0:
158-
# don't bother reporting the empty string (source of subtle bugs)
159-
self.collect_incoming_data (self.ac_in_buffer[:index])
161+
# don't bother reporting the empty string
162+
# (source of subtle bugs)
163+
self.collect_incoming_data(self.ac_in_buffer[:index])
160164
self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:]
161-
# This does the Right Thing if the terminator is changed here.
165+
# This does the Right Thing if the terminator
166+
# is changed here.
162167
self.found_terminator()
163168
else:
164169
# check for a prefix of the terminator
165-
index = find_prefix_at_end (self.ac_in_buffer, terminator)
170+
index = find_prefix_at_end(self.ac_in_buffer, terminator)
166171
if index:
167172
if index != lb:
168173
# we found a prefix, collect up to the prefix
169-
self.collect_incoming_data (self.ac_in_buffer[:-index])
174+
self.collect_incoming_data(self.ac_in_buffer[:-index])
170175
self.ac_in_buffer = self.ac_in_buffer[-index:]
171176
break
172177
else:
173178
# no prefix, collect it all
174-
self.collect_incoming_data (self.ac_in_buffer)
179+
self.collect_incoming_data(self.ac_in_buffer)
175180
self.ac_in_buffer = b''
176181

177-
def handle_write (self):
182+
def handle_write(self):
178183
self.initiate_send()
179184

180-
def handle_close (self):
185+
def handle_close(self):
181186
self.close()
182187

183-
def push (self, data):
188+
def push(self, data):
184189
if not isinstance(data, (bytes, bytearray, memoryview)):
185190
raise TypeError('data argument must be byte-ish (%r)',
186191
type(data))
@@ -192,23 +197,23 @@ def push (self, data):
192197
self.producer_fifo.append(data)
193198
self.initiate_send()
194199

195-
def push_with_producer (self, producer):
200+
def push_with_producer(self, producer):
196201
self.producer_fifo.append(producer)
197202
self.initiate_send()
198203

199-
def readable (self):
204+
def readable(self):
200205
"predicate for inclusion in the readable for select()"
201206
# cannot use the old predicate, it violates the claim of the
202207
# set_terminator method.
203208

204209
# return (len(self.ac_in_buffer) <= self.ac_in_buffer_size)
205210
return 1
206211

207-
def writable (self):
212+
def writable(self):
208213
"predicate for inclusion in the writable for select()"
209214
return self.producer_fifo or (not self.connected)
210215

211-
def close_when_done (self):
216+
def close_when_done(self):
212217
"automatically close this channel once the outgoing queue is empty"
213218
self.producer_fifo.append(None)
214219

@@ -219,10 +224,8 @@ def initiate_send(self):
219224
if not first:
220225
del self.producer_fifo[0]
221226
if first is None:
222-
## print("first is None")
223227
self.handle_close()
224228
return
225-
## print("first is not None")
226229

227230
# handle classic producer behavior
228231
obs = self.ac_out_buffer_size
@@ -254,20 +257,21 @@ def initiate_send(self):
254257
# we tried to send some actual data
255258
return
256259

257-
def discard_buffers (self):
260+
def discard_buffers(self):
258261
# Emergencies only!
259262
self.ac_in_buffer = b''
260263
del self.incoming[:]
261264
self.producer_fifo.clear()
262265

266+
263267
class simple_producer:
264268

265-
def __init__ (self, data, buffer_size=512):
269+
def __init__(self, data, buffer_size=512):
266270
self.data = data
267271
self.buffer_size = buffer_size
268272

269-
def more (self):
270-
if len (self.data) > self.buffer_size:
273+
def more(self):
274+
if len(self.data) > self.buffer_size:
271275
result = self.data[:self.buffer_size]
272276
self.data = self.data[self.buffer_size:]
273277
return result
@@ -276,8 +280,9 @@ def more (self):
276280
self.data = b''
277281
return result
278282

283+
279284
class fifo:
280-
def __init__ (self, list=None):
285+
def __init__(self, list=None):
281286
import warnings
282287
warnings.warn('fifo class will be removed in Python 3.6',
283288
DeprecationWarning, stacklevel=2)
@@ -286,31 +291,32 @@ def __init__ (self, list=None):
286291
else:
287292
self.list = deque(list)
288293

289-
def __len__ (self):
294+
def __len__(self):
290295
return len(self.list)
291296

292-
def is_empty (self):
297+
def is_empty(self):
293298
return not self.list
294299

295-
def first (self):
300+
def first(self):
296301
return self.list[0]
297302

298-
def push (self, data):
303+
def push(self, data):
299304
self.list.append(data)
300305

301-
def pop (self):
306+
def pop(self):
302307
if self.list:
303308
return (1, self.list.popleft())
304309
else:
305310
return (0, None)
306311

312+
307313
# Given 'haystack', see if any prefix of 'needle' is at its end. This
308314
# assumes an exact match has already been checked. Return the number of
309315
# characters matched.
310316
# for example:
311-
# f_p_a_e ("qwerty\r", "\r\n") => 1
312-
# f_p_a_e ("qwertydkjf", "\r\n") => 0
313-
# f_p_a_e ("qwerty\r\n", "\r\n") => <undefined>
317+
# f_p_a_e("qwerty\r", "\r\n") => 1
318+
# f_p_a_e("qwertydkjf", "\r\n") => 0
319+
# f_p_a_e("qwerty\r\n", "\r\n") => <undefined>
314320

315321
# this could maybe be made faster with a computed regex?
316322
# [answer: no; circa Python-2.0, Jan 2001]
@@ -319,7 +325,7 @@ def pop (self):
319325
# re: 12820/s
320326
# regex: 14035/s
321327

322-
def find_prefix_at_end (haystack, needle):
328+
def find_prefix_at_end(haystack, needle):
323329
l = len(needle) - 1
324330
while l and not haystack.endswith(needle[:l]):
325331
l -= 1

Lib/test/test_asynchat.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
# If this fails, the test will be skipped.
66
thread = support.import_module('_thread')
77

8-
import asyncore, asynchat, socket, time
9-
import unittest
8+
import asynchat
9+
import asyncore
10+
import socket
1011
import sys
12+
import time
13+
import unittest
1114
import warnings
1215
try:
1316
import threading
@@ -29,8 +32,8 @@ def __init__(self, event):
2932
self.event = event
3033
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3134
self.port = support.bind_port(self.sock)
32-
# This will be set if the client wants us to wait before echoing data
33-
# back.
35+
# This will be set if the client wants us to wait before echoing
36+
# data back.
3437
self.start_resend_event = None
3538

3639
def run(self):
@@ -53,8 +56,8 @@ def run(self):
5356

5457
# re-send entire set of collected data
5558
try:
56-
# this may fail on some tests, such as test_close_when_done, since
57-
# the client closes the channel when it's done sending
59+
# this may fail on some tests, such as test_close_when_done,
60+
# since the client closes the channel when it's done sending
5861
while self.buffer:
5962
n = conn.send(self.buffer[:self.chunk_size])
6063
time.sleep(0.001)
@@ -97,18 +100,18 @@ def start_echo_server():
97100
s.start()
98101
event.wait()
99102
event.clear()
100-
time.sleep(0.01) # Give server time to start accepting.
103+
time.sleep(0.01) # Give server time to start accepting.
101104
return s, event
102105

103106

104107
@unittest.skipUnless(threading, 'Threading required for this test.')
105108
class TestAsynchat(unittest.TestCase):
106109
usepoll = False
107110

108-
def setUp (self):
111+
def setUp(self):
109112
self._threads = support.threading_setup()
110113

111-
def tearDown (self):
114+
def tearDown(self):
112115
support.threading_cleanup(*self._threads)
113116

114117
def line_terminator_check(self, term, server_chunk):
@@ -118,7 +121,7 @@ def line_terminator_check(self, term, server_chunk):
118121
s.start()
119122
event.wait()
120123
event.clear()
121-
time.sleep(0.01) # Give server time to start accepting.
124+
time.sleep(0.01) # Give server time to start accepting.
122125
c = echo_client(term, s.port)
123126
c.push(b"hello ")
124127
c.push(b"world" + term)
@@ -137,17 +140,17 @@ def line_terminator_check(self, term, server_chunk):
137140

138141
def test_line_terminator1(self):
139142
# test one-character terminator
140-
for l in (1,2,3):
143+
for l in (1, 2, 3):
141144
self.line_terminator_check(b'\n', l)
142145

143146
def test_line_terminator2(self):
144147
# test two-character terminator
145-
for l in (1,2,3):
148+
for l in (1, 2, 3):
146149
self.line_terminator_check(b'\r\n', l)
147150

148151
def test_line_terminator3(self):
149152
# test three-character terminator
150-
for l in (1,2,3):
153+
for l in (1, 2, 3):
151154
self.line_terminator_check(b'qqq', l)
152155

153156
def numeric_terminator_check(self, termlen):
@@ -270,11 +273,13 @@ def test_push(self):
270273
class TestAsynchat_WithPoll(TestAsynchat):
271274
usepoll = True
272275

276+
273277
class TestHelperFunctions(unittest.TestCase):
274278
def test_find_prefix_at_end(self):
275279
self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1)
276280
self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0)
277281

282+
278283
class TestFifo(unittest.TestCase):
279284
def test_basic(self):
280285
with warnings.catch_warnings(record=True) as w:

0 commit comments

Comments
 (0)