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 @@ class async_chat (asyncore.dispatcher):
raise NotImplementedError("must be implemented in subclass")
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:
term = bytes(term, self.encoding)
self.terminator = term
@ -155,10 +158,12 @@ class async_chat (asyncore.dispatcher):
if index != -1:
# we found the terminator
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.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()
else:
# check for a prefix of the terminator
@ -219,10 +224,8 @@ class async_chat (asyncore.dispatcher):
if not first:
del self.producer_fifo[0]
if first is None:
## print("first is None")
self.handle_close()
return
## print("first is not None")
# handle classic producer behavior
obs = self.ac_out_buffer_size
@ -260,6 +263,7 @@ class async_chat (asyncore.dispatcher):
del self.incoming[:]
self.producer_fifo.clear()
class simple_producer:
def __init__(self, data, buffer_size=512):
@ -276,6 +280,7 @@ class simple_producer:
self.data = b''
return result
class fifo:
def __init__(self, list=None):
if not list:
@ -301,6 +306,7 @@ class fifo:
else:
return (0, None)
# 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
# characters matched.

View file

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