Skip to content

Commit

Permalink
Don't bring value of previous calls of handle() to the new one.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matěj Cepl committed Jul 2, 2014
1 parent a8a0bb3 commit f91a6aa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
11 changes: 10 additions & 1 deletion html2text.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,15 @@ def outtextf(self, s):
def close(self):
HTMLParser.HTMLParser.close(self)

try:
nochr = unicode('')
except NameError:
nochr = str('')

self.pbr()
self.o('', 0, 'end')

self.outtext = self.outtext.join(self.outtextlist)
self.outtext = nochr.join(self.outtextlist)
if self.unicode_snob:
try:
nbsp = unichr(name2cp('nbsp'))
Expand All @@ -303,6 +308,10 @@ def close(self):
except NameError:
self.outtext = self.outtext.replace('&nbsp_place_holder;', nbsp)

# Clear self.outtextlist to avoid memory leak of its content to
# the next handling.
self.outtextlist = []

return self.outtext

def handle_charref(self, c):
Expand Down
32 changes: 32 additions & 0 deletions test/test_memleak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sys
if sys.version_info[:2] < (2, 7):
import unittest2 as unittest
else:
import unittest
import logging
logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s',
level=logging.DEBUG)

import html2text


class TestMemleak(unittest.TestCase):
"""
See https://github.com/Alir3z4/html2text/issues/13 for more
information on this.
"""

def setUp(self):
self.instr = 'miow '

def test_same_string(self):
h2t = html2text.HTML2Text()
result = h2t.handle(self.instr)
# Now, we shouldn't get leak of the previous run to the new one
self.assertEqual(h2t.handle(self.instr), result)

def test_empty_string(self):
h2t = html2text.HTML2Text()
h2t.handle(self.instr)
# And even less when the input is empty
self.assertEqual(h2t.handle(''), u'\n\n')

0 comments on commit f91a6aa

Please sign in to comment.