summaryrefslogtreecommitdiff
path: root/Lib/email
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-08-12 14:00:29 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2014-08-12 14:00:29 +0300
commit275f732120532c2c91f9b7b7164503ee0fc253cc (patch)
tree33e19f0fbfb9c998b10ebae3ea624cc85efea7bc /Lib/email
parent74530a065244e0c68e1028d86b30d6b16ee54168 (diff)
parent35e105a171e892275e00bd932aa1c6bccc188f3d (diff)
downloadcpython-275f732120532c2c91f9b7b7164503ee0fc253cc.tar.gz
Issue #21448: Fixed FeedParser feed() to avoid O(N**2) behavior when parsing long line.
Original patch by Raymond Hettinger.
Diffstat (limited to 'Lib/email')
-rw-r--r--Lib/email/feedparser.py26
1 files changed, 18 insertions, 8 deletions
diff --git a/Lib/email/feedparser.py b/Lib/email/feedparser.py
index 6cf9b91c1f..0c3b572684 100644
--- a/Lib/email/feedparser.py
+++ b/Lib/email/feedparser.py
@@ -50,8 +50,8 @@ class BufferedSubFile(object):
simple abstraction -- it parses until EOF closes the current message.
"""
def __init__(self):
- # The last partial line pushed into this object.
- self._partial = ''
+ # Chunks of the last partial line pushed into this object.
+ self._partial = []
# The list of full, pushed lines, in reverse order
self._lines = []
# The stack of false-EOF checking predicates.
@@ -67,8 +67,8 @@ class BufferedSubFile(object):
def close(self):
# Don't forget any trailing partial line.
- self._lines.append(self._partial)
- self._partial = ''
+ self.pushlines(''.join(self._partial).splitlines(True))
+ self._partial = []
self._closed = True
def readline(self):
@@ -96,16 +96,26 @@ class BufferedSubFile(object):
def push(self, data):
"""Push some new data into this object."""
- # Handle any previous leftovers
- data, self._partial = self._partial + data, ''
# Crack into lines, but preserve the linesep characters on the end of each
parts = data.splitlines(True)
+
+ if not parts or not parts[0].endswith(('\n', '\r')):
+ # No new complete lines, so just accumulate partials
+ self._partial += parts
+ return
+
+ if self._partial:
+ # If there are previous leftovers, complete them now
+ self._partial.append(parts[0])
+ parts[0:1] = ''.join(self._partial).splitlines(True)
+ del self._partial[:]
+
# If the last element of the list does not end in a newline, then treat
# it as a partial line. We only check for '\n' here because a line
# ending with '\r' might be a line that was split in the middle of a
# '\r\n' sequence (see bugs 1555570 and 1721862).
- if parts and not parts[-1].endswith('\n'):
- self._partial = parts.pop()
+ if not parts[-1].endswith('\n'):
+ self._partial = [parts.pop()]
self.pushlines(parts)
def pushlines(self, lines):