Skip to content

Commit

Permalink
fix StringIO/BytesIO usage in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scoder committed Apr 5, 2014
1 parent b2ea55f commit 85e65a9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion doc/api.txt
Expand Up @@ -44,7 +44,7 @@ lxml is extremely extensible through `XPath functions in Python`_, custom


.. ..
>>> from io import BytesIO >>> from io import BytesIO
... def StringIO(s=None): >>> def StringIO(s=None):
... if isinstance(s, str): s = s.encode("UTF-8") ... if isinstance(s, str): s = s.encode("UTF-8")
... return BytesIO(s) ... return BytesIO(s)


Expand Down
10 changes: 6 additions & 4 deletions src/lxml/tests/common_imports.py
Expand Up @@ -147,7 +147,7 @@ def _str(s, encoding="UTF-8"):
return unicode(s, encoding=encoding) return unicode(s, encoding=encoding)
def _bytes(s, encoding="UTF-8"): def _bytes(s, encoding="UTF-8"):
return s return s
BytesIO = StringIO from io import BytesIO


doctest_parser = doctest.DocTestParser() doctest_parser = doctest.DocTestParser()
_fix_traceback = re.compile(r'^(\s*)(?:\w+\.)+(\w*(?:Error|Exception|Invalid):)', re.M).sub _fix_traceback = re.compile(r'^(\s*)(?:\w+\.)+(\w*(?:Error|Exception|Invalid):)', re.M).sub
Expand All @@ -173,12 +173,13 @@ def skipIf(condition, why,
return _skip return _skip
return _keep return _keep



class HelperTestCase(unittest.TestCase): class HelperTestCase(unittest.TestCase):
def tearDown(self): def tearDown(self):
gc.collect() gc.collect()


def parse(self, text, parser=None): def parse(self, text, parser=None):
f = BytesIO(text) f = BytesIO(text) if isinstance(text, bytes) else StringIO(text)
return etree.parse(f, parser=parser) return etree.parse(f, parser=parser)


def _rootstring(self, tree): def _rootstring(self, tree):
Expand All @@ -190,7 +191,8 @@ def _rootstring(self, tree):
unittest.TestCase.assertFalse unittest.TestCase.assertFalse
except AttributeError: except AttributeError:
assertFalse = unittest.TestCase.failIf assertFalse = unittest.TestCase.failIf



class SillyFileLike: class SillyFileLike:
def __init__(self, xml_data=_bytes('<foo><bar/></foo>')): def __init__(self, xml_data=_bytes('<foo><bar/></foo>')):
self.xml_data = xml_data self.xml_data = xml_data
Expand Down Expand Up @@ -293,7 +295,7 @@ def readFileInTestDir(name, mode='r'):
return read_file(fileInTestDir(name), mode) return read_file(fileInTestDir(name), mode)


def canonicalize(xml): def canonicalize(xml):
tree = etree.parse(BytesIO(xml)) tree = etree.parse(BytesIO(xml) if isinstance(xml, bytes) else StringIO(xml))
f = BytesIO() f = BytesIO()
tree.write_c14n(f) tree.write_c14n(f)
return f.getvalue() return f.getvalue()
Expand Down

0 comments on commit 85e65a9

Please sign in to comment.