| 1 |
#!/usr/bin/env python |
| 2 |
"""Run doctests.""" |
| 3 |
|
| 4 |
import doctest |
| 5 |
import platform |
| 6 |
import re |
| 7 |
import sys |
| 8 |
|
| 9 |
try: |
| 10 |
import unittest2 as unittest |
| 11 |
except ImportError: |
| 12 |
import unittest |
| 13 |
|
| 14 |
def _is_new_jpype(): |
| 15 |
if platform.python_implementation() != 'Jython': |
| 16 |
import jpype |
| 17 |
try: |
| 18 |
ver_match = re.match('\d+\.\d+', jpype.__version__) |
| 19 |
if ver_match: |
| 20 |
jpype_ver = float(ver_match.group(0)) |
| 21 |
if jpype_ver >= 0.7: |
| 22 |
return True |
| 23 |
except ValueError: |
| 24 |
pass |
| 25 |
return False |
| 26 |
|
| 27 |
class Py23DocChecker(doctest.OutputChecker): |
| 28 |
"""Doctest checker to avoid Python 2/3 unicode comparison |
| 29 |
issues. Code mostly taken from Dirkjan Ochtman""" |
| 30 |
def check_output(self, want, got, optionflags): |
| 31 |
if sys.version_info[0] > 2 or _is_new_jpype(): |
| 32 |
# new python has unicode as default |
| 33 |
# new JPype does not automatically convert to unicode on Python 2 |
| 34 |
want = re.sub("u'(.*?)'", "'\\1'", want) |
| 35 |
want = re.sub('u"(.*?)"', '"\\1"', want) |
| 36 |
return doctest.OutputChecker.check_output(self, want, got, optionflags) |
| 37 |
|
| 38 |
def suite(): |
| 39 |
suite = unittest.TestSuite() |
| 40 |
suite.addTest(doctest.DocFileSuite('../README.rst', |
| 41 |
checker=Py23DocChecker())) |
| 42 |
return suite; |
| 43 |
|
| 44 |
def main(): |
| 45 |
runner = unittest.TextTestRunner() |
| 46 |
result = runner.run(suite()) |
| 47 |
if result.wasSuccessful(): |
| 48 |
return 0 |
| 49 |
else: |
| 50 |
return 1 |
| 51 |
|
| 52 |
if __name__ == '__main__': |
| 53 |
sys.exit(main()) |