Skip to content

Commit

Permalink
[svn r3758] r4374@delle: sbehnel | 2008-05-26 12:12:39 +0200
Browse files Browse the repository at this point in the history
 doctest work-arounds for Py3

--HG--
branch : trunk
  • Loading branch information
scoder committed May 26, 2008
1 parent ccfa21e commit 6167469
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 31 deletions.
8 changes: 4 additions & 4 deletions doc/extensions.txt
Expand Up @@ -240,7 +240,7 @@ the global mapping of the FunctionNamespace objects:
>>> e2('/foo:a') >>> e2('/foo:a')
Traceback (most recent call last): Traceback (most recent call last):
... ...
XPathEvalError: Undefined namespace prefix lxml.etree.XPathEvalError: Undefined namespace prefix




Evaluator-local extensions Evaluator-local extensions
Expand Down Expand Up @@ -405,13 +405,13 @@ XPath node-sets:
'Alpha' 'Alpha'


>>> print(etree.tostring(r[0])) >>> print(etree.tostring(r[0]))
<results1><result>Alpha</result><result>Beta</result></results1> b'<results1><result>Alpha</result><result>Beta</result></results1>'


>>> print(etree.tostring(r[1])) >>> print(etree.tostring(r[1]))
<results2><result>Gamma</result><result>Delta</result><subresult/></results2> b'<results2><result>Gamma</result><result>Delta</result><subresult/></results2>'


>>> print(etree.tostring(r[2])) >>> print(etree.tostring(r[2]))
<subresult/> b'<subresult/>'


The current implementation deep-copies newly created elements in node-sets. The current implementation deep-copies newly created elements in node-sets.
Only the elements and their children are passed on, no outlying parents or Only the elements and their children are passed on, no outlying parents or
Expand Down
2 changes: 1 addition & 1 deletion doc/objectify.txt
Expand Up @@ -471,7 +471,7 @@ Or an invalid document:
>>> xml = "<a><b>test</b><c/></a>" >>> xml = "<a><b>test</b><c/></a>"
>>> a = objectify.fromstring(xml, parser) >>> a = objectify.fromstring(xml, parser)
Traceback (most recent call last): Traceback (most recent call last):
XMLSyntaxError: Element 'c': This element is not expected. lxml.etree.XMLSyntaxError: Element 'c': This element is not expected.


Note that the same works for parse-time DTD validation, except that Note that the same works for parse-time DTD validation, except that
DTDs do not support any data types by design. DTDs do not support any data types by design.
Expand Down
2 changes: 1 addition & 1 deletion doc/sax.txt
Expand Up @@ -51,7 +51,7 @@ property of the handler:


>>> tree = handler.etree >>> tree = handler.etree
>>> lxml.etree.tostring(tree.getroot()) >>> lxml.etree.tostring(tree.getroot())
'<a><b foo="bar">Hello world</b></a>' b'<a><b foo="bar">Hello world</b></a>'


By passing a ``makeelement`` function the constructor of By passing a ``makeelement`` function the constructor of
``ElementTreeContentHandler``, e.g. the one of a parser you configured, you ``ElementTreeContentHandler``, e.g. the one of a parser you configured, you
Expand Down
6 changes: 3 additions & 3 deletions doc/validation.txt
Expand Up @@ -88,7 +88,7 @@ will raise an exception:


>>> root = etree.fromstring("<a>no int</a>", parser) >>> root = etree.fromstring("<a>no int</a>", parser)
Traceback (most recent call last): Traceback (most recent call last):
XMLSyntaxError: Element 'a': 'no int' is not a valid value of the atomic type 'xs:integer'. lxml.etree.XMLSyntaxError: Element 'a': 'no int' is not a valid value of the atomic type 'xs:integer'.


If you want the parser to succeed regardless of the outcome of the If you want the parser to succeed regardless of the outcome of the
validation, you should use a non validating parser and run the validation, you should use a non validating parser and run the
Expand Down Expand Up @@ -200,7 +200,7 @@ If you prefer getting an exception when validating, you can use the
>>> relaxng.assertValid(doc2) >>> relaxng.assertValid(doc2)
Traceback (most recent call last): Traceback (most recent call last):
... ...
DocumentInvalid: Did not expect element c there, line 1 lxml.etree.DocumentInvalid: Did not expect element c there, line 1


>>> relaxng.assert_(doc2) >>> relaxng.assert_(doc2)
Traceback (most recent call last): Traceback (most recent call last):
Expand Down Expand Up @@ -307,7 +307,7 @@ If you prefer getting an exception when validating, you can use the
>>> xmlschema.assertValid(doc2) >>> xmlschema.assertValid(doc2)
Traceback (most recent call last): Traceback (most recent call last):
... ...
DocumentInvalid: Element 'c': This element is not expected. Expected is ( b )., line 1 lxml.etree.DocumentInvalid: Element 'c': This element is not expected. Expected is ( b )., line 1


>>> xmlschema.assert_(doc2) >>> xmlschema.assert_(doc2)
Traceback (most recent call last): Traceback (most recent call last):
Expand Down
12 changes: 6 additions & 6 deletions doc/xpathxslt.txt
Expand Up @@ -337,7 +337,7 @@ XPath expression:
>>> find = etree.XPath("\\") >>> find = etree.XPath("\\")
Traceback (most recent call last): Traceback (most recent call last):
... ...
XPathSyntaxError: Invalid expression lxml.etree.XPathSyntaxError: Invalid expression


lxml will also try to give you a hint what went wrong, so if you pass a more lxml will also try to give you a hint what went wrong, so if you pass a more
complex expression, you may get a somewhat more specific error: complex expression, you may get a somewhat more specific error:
Expand All @@ -347,7 +347,7 @@ complex expression, you may get a somewhat more specific error:
>>> find = etree.XPath("//*[1.1.1]") >>> find = etree.XPath("//*[1.1.1]")
Traceback (most recent call last): Traceback (most recent call last):
... ...
XPathSyntaxError: Invalid predicate lxml.etree.XPathSyntaxError: Invalid predicate


During evaluation, lxml will emit an XPathEvalError on errors: During evaluation, lxml will emit an XPathEvalError on errors:


Expand All @@ -357,7 +357,7 @@ During evaluation, lxml will emit an XPathEvalError on errors:
>>> find(root) >>> find(root)
Traceback (most recent call last): Traceback (most recent call last):
... ...
XPathEvalError: Undefined namespace prefix lxml.etree.XPathEvalError: Undefined namespace prefix


This works for the ``XPath`` class, however, the other evaluators (including This works for the ``XPath`` class, however, the other evaluators (including
the ``xpath()`` method) are one-shot operations that do parsing and evaluation the ``xpath()`` method) are one-shot operations that do parsing and evaluation
Expand All @@ -369,17 +369,17 @@ in one step. They therefore raise evaluation exceptions in all cases:
>>> find = root.xpath("//*[1.1.1]") >>> find = root.xpath("//*[1.1.1]")
Traceback (most recent call last): Traceback (most recent call last):
... ...
XPathEvalError: Invalid predicate lxml.etree.XPathEvalError: Invalid predicate


>>> find = root.xpath("//ns:a") >>> find = root.xpath("//ns:a")
Traceback (most recent call last): Traceback (most recent call last):
... ...
XPathEvalError: Undefined namespace prefix lxml.etree.XPathEvalError: Undefined namespace prefix


>>> find = root.xpath("\\") >>> find = root.xpath("\\")
Traceback (most recent call last): Traceback (most recent call last):
... ...
XPathEvalError: Invalid expression lxml.etree.XPathEvalError: Invalid expression


Note that lxml versions before 1.3 always raised an ``XPathSyntaxError`` for Note that lxml versions before 1.3 always raised an ``XPathSyntaxError`` for
all errors, including evaluation errors. The best way to support older all errors, including evaluation errors. The best way to support older
Expand Down
25 changes: 24 additions & 1 deletion src/lxml/tests/common_imports.py
Expand Up @@ -43,6 +43,7 @@ def make_version_tuple(version_string):
import doctest import doctest
# check if the system version has everything we need # check if the system version has everything we need
doctest.DocFileSuite doctest.DocFileSuite
doctest.DocTestParser
doctest.NORMALIZE_WHITESPACE doctest.NORMALIZE_WHITESPACE
doctest.ELLIPSIS doctest.ELLIPSIS
except (ImportError, AttributeError): except (ImportError, AttributeError):
Expand All @@ -59,7 +60,7 @@ def sorted(seq, **kwargs):
else: else:
locals()['sorted'] = sorted locals()['sorted'] = sorted


if sys.version_info >= (3,): if sys.version_info[0] >= 3:
# Python 3 # Python 3
unicode = str unicode = str
def _str(s, encoding="UTF-8"): def _str(s, encoding="UTF-8"):
Expand All @@ -71,6 +72,16 @@ def BytesIO(*args):
if args and isinstance(args[0], str): if args and isinstance(args[0], str):
args = (args[0].encode("UTF-8"),) args = (args[0].encode("UTF-8"),)
return _BytesIO(*args) return _BytesIO(*args)

doctest_parser = doctest.DocTestParser()
_fix_unicode = re.compile(r'(\s+)u(["\'])').sub
def make_doctest(filename):
filename = os.path.normpath(os.path.join(os.path.dirname(__file__), filename))
doctests = open(filename).read()
doctests = _fix_unicode(r'\1\2', doctests)
return doctest.DocTestCase(
doctest_parser.get_doctest(
doctests, {}, os.path.basename(filename), filename, 0))
else: else:
# Python 2 # Python 2
def _str(s, encoding="UTF-8"): def _str(s, encoding="UTF-8"):
Expand All @@ -80,6 +91,18 @@ def _bytes(s, encoding="UTF-8"):
from StringIO import StringIO from StringIO import StringIO
BytesIO = StringIO BytesIO = StringIO


doctest_parser = doctest.DocTestParser()
_fix_exceptions = re.compile(r'(\s+)(?:\w+\.)+([^.]*(?:Error|Exception):)').sub
_fix_bytes = re.compile(r'(\s+)b(["\'])').sub
def make_doctest(filename):
filename = os.path.normpath(os.path.join(os.path.dirname(__file__), filename))
doctests = open(filename).read()
doctests = _fix_exceptions(r'\1\2', doctests)
doctests = _fix_bytes(r'\1\2', doctests)
return doctest.DocTestCase(
doctest_parser.get_doctest(
doctests, {}, os.path.basename(filename), filename, 0))

class HelperTestCase(unittest.TestCase): class HelperTestCase(unittest.TestCase):
def tearDown(self): def tearDown(self):
gc.collect() gc.collect()
Expand Down
4 changes: 2 additions & 2 deletions src/lxml/tests/test_dtd.py
Expand Up @@ -11,7 +11,7 @@
sys.path.insert(0, this_dir) # needed for Py3 sys.path.insert(0, this_dir) # needed for Py3


from common_imports import etree, StringIO, BytesIO, _bytes, doctest from common_imports import etree, StringIO, BytesIO, _bytes, doctest
from common_imports import HelperTestCase, fileInTestDir from common_imports import HelperTestCase, fileInTestDir, make_doctest


class ETreeDtdTestCase(HelperTestCase): class ETreeDtdTestCase(HelperTestCase):
def test_dtd(self): def test_dtd(self):
Expand Down Expand Up @@ -131,7 +131,7 @@ def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTests([unittest.makeSuite(ETreeDtdTestCase)]) suite.addTests([unittest.makeSuite(ETreeDtdTestCase)])
suite.addTests( suite.addTests(
[doctest.DocFileSuite('../../../doc/validation.txt')]) [make_doctest('../../../doc/validation.txt')])
return suite return suite


if __name__ == '__main__': if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions src/lxml/tests/test_objectify.py
Expand Up @@ -12,7 +12,7 @@
sys.path.insert(0, this_dir) # needed for Py3 sys.path.insert(0, this_dir) # needed for Py3


from common_imports import etree, HelperTestCase, fileInTestDir from common_imports import etree, HelperTestCase, fileInTestDir
from common_imports import SillyFileLike, canonicalize, doctest from common_imports import SillyFileLike, canonicalize, doctest, make_doctest
from common_imports import _bytes, _str, StringIO, BytesIO from common_imports import _bytes, _str, StringIO, BytesIO


from lxml import objectify from lxml import objectify
Expand Down Expand Up @@ -2329,7 +2329,7 @@ def test_suite():
suite.addTests([unittest.makeSuite(ObjectifyTestCase)]) suite.addTests([unittest.makeSuite(ObjectifyTestCase)])
if sys.version_info >= (2,4): if sys.version_info >= (2,4):
suite.addTests( suite.addTests(
[doctest.DocFileSuite('../../../doc/objectify.txt')]) [make_doctest('../../../doc/objectify.txt')])
return suite return suite


if __name__ == '__main__': if __name__ == '__main__':
Expand Down
5 changes: 3 additions & 2 deletions src/lxml/tests/test_relaxng.py
Expand Up @@ -10,7 +10,8 @@
if this_dir not in sys.path: if this_dir not in sys.path:
sys.path.insert(0, this_dir) # needed for Py3 sys.path.insert(0, this_dir) # needed for Py3


from common_imports import etree, doctest, BytesIO, _bytes, HelperTestCase, fileInTestDir from common_imports import etree, BytesIO, _bytes, HelperTestCase, fileInTestDir
from common_imports import doctest, make_doctest


class ETreeRelaxNGTestCase(HelperTestCase): class ETreeRelaxNGTestCase(HelperTestCase):
def test_relaxng(self): def test_relaxng(self):
Expand Down Expand Up @@ -158,7 +159,7 @@ def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTests([unittest.makeSuite(ETreeRelaxNGTestCase)]) suite.addTests([unittest.makeSuite(ETreeRelaxNGTestCase)])
suite.addTests( suite.addTests(
[doctest.DocFileSuite('../../../doc/validation.txt')]) [make_doctest('../../../doc/validation.txt')])
return suite return suite


if __name__ == '__main__': if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions src/lxml/tests/test_sax.py
Expand Up @@ -10,7 +10,7 @@
if this_dir not in sys.path: if this_dir not in sys.path:
sys.path.insert(0, this_dir) # needed for Py3 sys.path.insert(0, this_dir) # needed for Py3


from common_imports import HelperTestCase, doctest, BytesIO, _bytes from common_imports import HelperTestCase, doctest, make_doctest, BytesIO, _bytes
from lxml import sax from lxml import sax
from xml.dom import pulldom from xml.dom import pulldom


Expand Down Expand Up @@ -221,7 +221,7 @@ def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTests([unittest.makeSuite(ETreeSaxTestCase)]) suite.addTests([unittest.makeSuite(ETreeSaxTestCase)])
suite.addTests( suite.addTests(
[doctest.DocFileSuite('../../../doc/sax.txt')]) [make_doctest('../../../doc/sax.txt')])
return suite return suite


if __name__ == '__main__': if __name__ == '__main__':
Expand Down
5 changes: 3 additions & 2 deletions src/lxml/tests/test_xmlschema.py
Expand Up @@ -10,7 +10,8 @@
if this_dir not in sys.path: if this_dir not in sys.path:
sys.path.insert(0, this_dir) # needed for Py3 sys.path.insert(0, this_dir) # needed for Py3


from common_imports import etree, doctest, BytesIO, HelperTestCase, fileInTestDir from common_imports import etree, BytesIO, HelperTestCase, fileInTestDir
from common_imports import doctest, make_doctest


class ETreeXMLSchemaTestCase(HelperTestCase): class ETreeXMLSchemaTestCase(HelperTestCase):
def test_xmlschema(self): def test_xmlschema(self):
Expand Down Expand Up @@ -156,7 +157,7 @@ def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTests([unittest.makeSuite(ETreeXMLSchemaTestCase)]) suite.addTests([unittest.makeSuite(ETreeXMLSchemaTestCase)])
suite.addTests( suite.addTests(
[doctest.DocFileSuite('../../../doc/validation.txt')]) [make_doctest('../../../doc/validation.txt')])
return suite return suite


if __name__ == '__main__': if __name__ == '__main__':
Expand Down
12 changes: 10 additions & 2 deletions src/lxml/tests/test_xpathevaluator.py
Expand Up @@ -10,7 +10,8 @@
if this_dir not in sys.path: if this_dir not in sys.path:
sys.path.insert(0, this_dir) # needed for Py3 sys.path.insert(0, this_dir) # needed for Py3


from common_imports import etree, HelperTestCase, doctest, _bytes, BytesIO from common_imports import etree, HelperTestCase, _bytes, BytesIO
from common_imports import doctest, make_doctest


class ETreeXPathTestCase(HelperTestCase): class ETreeXPathTestCase(HelperTestCase):
"""XPath tests etree""" """XPath tests etree"""
Expand Down Expand Up @@ -630,6 +631,13 @@ def xpath():
... print("Got error") ... print("Got error")
Got error Got error
""" """

if sys.version_info[0] >= 3:
xpath.__doc__ = xpath.__doc__.replace(" u'", " '")
xpath.__doc__ = xpath.__doc__.replace(" XPathResultError",
" lxml.etree.XPathResultError")
xpath.__doc__ = xpath.__doc__.replace(" exactly 2 arguments",
" exactly 2 positional arguments")


def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
Expand All @@ -638,7 +646,7 @@ def test_suite():
suite.addTests([unittest.makeSuite(ETreeETXPathClassTestCase)]) suite.addTests([unittest.makeSuite(ETreeETXPathClassTestCase)])
suite.addTests([doctest.DocTestSuite()]) suite.addTests([doctest.DocTestSuite()])
suite.addTests( suite.addTests(
[doctest.DocFileSuite('../../../doc/xpathxslt.txt')]) [make_doctest('../../../doc/xpathxslt.txt')])
return suite return suite


if __name__ == '__main__': if __name__ == '__main__':
Expand Down
6 changes: 3 additions & 3 deletions src/lxml/tests/test_xslt.py
Expand Up @@ -23,7 +23,7 @@
basestring = str basestring = str


from common_imports import etree, BytesIO, HelperTestCase, fileInTestDir from common_imports import etree, BytesIO, HelperTestCase, fileInTestDir
from common_imports import doctest, _bytes, _str from common_imports import doctest, _bytes, _str, make_doctest


class ETreeXSLTTestCase(HelperTestCase): class ETreeXSLTTestCase(HelperTestCase):
"""XSLT tests etree""" """XSLT tests etree"""
Expand Down Expand Up @@ -1355,9 +1355,9 @@ def test_suite():
if is_python3: if is_python3:
suite.addTests([unittest.makeSuite(Py3XSLTTestCase)]) suite.addTests([unittest.makeSuite(Py3XSLTTestCase)])
suite.addTests( suite.addTests(
[doctest.DocFileSuite('../../../doc/extensions.txt')]) [make_doctest('../../../doc/extensions.txt')])
suite.addTests( suite.addTests(
[doctest.DocFileSuite('../../../doc/xpathxslt.txt')]) [make_doctest('../../../doc/xpathxslt.txt')])
return suite return suite


if __name__ == '__main__': if __name__ == '__main__':
Expand Down

0 comments on commit 6167469

Please sign in to comment.