asynchat: PEP8-ify the code

This commit is contained in:
Victor Stinner 2014-07-08 00:16:54 +02:00
parent d9e810a870
commit fd5d1b51d6
2 changed files with 68 additions and 57 deletions

View file

@ -93,7 +93,10 @@ def found_terminator(self):
raise NotImplementedError("must be implemented in subclass") raise NotImplementedError("must be implemented in subclass")
def set_terminator(self, term): def set_terminator(self, term):
"Set the input delimiter. Can be a fixed string of any length, an integer, or None" """Set the input delimiter.
Can be a fixed string of any length, an integer, or None.
"""
if isinstance(term, str) and self.use_encoding: if isinstance(term, str) and self.use_encoding:
term = bytes(term, self.encoding) term = bytes(term, self.encoding)
self.terminator = term self.terminator = term
@ -155,10 +158,12 @@ def handle_read (self):
if index != -1: if index != -1:
# we found the terminator # we found the terminator
if index > 0: if index > 0:
# don't bother reporting the empty string (source of subtle bugs) # don't bother reporting the empty string
# (source of subtle bugs)
self.collect_incoming_data(self.ac_in_buffer[:index]) self.collect_incoming_data(self.ac_in_buffer[:index])
self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:] self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:]
# This does the Right Thing if the terminator is changed here. # This does the Right Thing if the terminator
# is changed here.
self.found_terminator() self.found_terminator()
else: else:
# check for a prefix of the terminator # check for a prefix of the terminator
@ -219,10 +224,8 @@ def initiate_send(self):
if not first: if not first:
del self.producer_fifo[0] del self.producer_fifo[0]
if first is None: if first is None:
## print("first is None")
self.handle_close() self.handle_close()
return return
## print("first is not None")
# handle classic producer behavior # handle classic producer behavior
obs = self.ac_out_buffer_size obs = self.ac_out_buffer_size
@ -260,6 +263,7 @@ def discard_buffers (self):
del self.incoming[:] del self.incoming[:]
self.producer_fifo.clear() self.producer_fifo.clear()
class simple_producer: class simple_producer:
def __init__(self, data, buffer_size=512): def __init__(self, data, buffer_size=512):
@ -276,6 +280,7 @@ def more (self):
self.data = b'' self.data = b''
return result return result
class fifo: class fifo:
def __init__(self, list=None): def __init__(self, list=None):
if not list: if not list:
@ -301,6 +306,7 @@ def pop (self):
else: else:
return (0, None) return (0, None)
# Given 'haystack', see if any prefix of 'needle' is at its end. This # Given 'haystack', see if any prefix of 'needle' is at its end. This
# assumes an exact match has already been checked. Return the number of # assumes an exact match has already been checked. Return the number of
# characters matched. # characters matched.

View file

@ -5,9 +5,12 @@
# If this fails, the test will be skipped. # If this fails, the test will be skipped.
thread = support.import_module('_thread') thread = support.import_module('_thread')
import asyncore, asynchat, socket, time import asynchat
import unittest import asyncore
import socket
import sys import sys
import time
import unittest
try: try:
import threading import threading
except ImportError: except ImportError:
@ -28,8 +31,8 @@ def __init__(self, event):
self.event = event self.event = event
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.port = support.bind_port(self.sock) self.port = support.bind_port(self.sock)
# This will be set if the client wants us to wait before echoing data # This will be set if the client wants us to wait before echoing
# back. # data back.
self.start_resend_event = None self.start_resend_event = None
def run(self): def run(self):
@ -52,8 +55,8 @@ def run(self):
# re-send entire set of collected data # re-send entire set of collected data
try: try:
# this may fail on some tests, such as test_close_when_done, since # this may fail on some tests, such as test_close_when_done,
# the client closes the channel when it's done sending # since the client closes the channel when it's done sending
while self.buffer: while self.buffer:
n = conn.send(self.buffer[:self.chunk_size]) n = conn.send(self.buffer[:self.chunk_size])
time.sleep(0.001) time.sleep(0.001)
@ -269,11 +272,13 @@ def test_push(self):
class TestAsynchat_WithPoll(TestAsynchat): class TestAsynchat_WithPoll(TestAsynchat):
usepoll = True usepoll = True
class TestHelperFunctions(unittest.TestCase): class TestHelperFunctions(unittest.TestCase):
def test_find_prefix_at_end(self): def test_find_prefix_at_end(self):
self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1) self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1)
self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0) self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0)
class TestFifo(unittest.TestCase): class TestFifo(unittest.TestCase):
def test_basic(self): def test_basic(self):
f = asynchat.fifo() f = asynchat.fifo()