Coverage for cogapp/backward.py: 71.43%

1"""Compatibility between Py2 and Py3."""

2

3 import sys

4 import unittest

5

6 PY3 = sys.version_info[0] == 3

7

8 if PY3: 8 ↛ 14line 8 didn't jump to line 14, because the condition on line 8 was never false

9 string_types = (str,bytes)

10 bytes_types = (bytes,)

11 def to_bytes(s):

12 return s.encode('utf8')

13 else:

14 string_types = (basestring,)

15 bytes_types = (str,)

16 def to_bytes(s):

17 return s

18

19# Pythons 2 and 3 differ on where to get StringIO

20 try:

21 from cStringIO import StringIO

22 except ImportError:

23 from io import StringIO

24

25

26 def unittest_has(method):

27 """Does `unittest.TestCase` have `method` defined?"""

28 return hasattr(unittest.TestCase, method)

29

30

31 class TestCase(unittest.TestCase):

32 """Just like unittest.TestCase, but with assert methods added.

33

34 Designed to be compatible with 3.1 unittest. Methods are only defined if

35 `unittest` doesn't have them.

36

37 """

38 # pylint: disable=missing-docstring

39

40 if not unittest_has('assertRaisesRegex'): 40 ↛ 41line 40 didn't jump to line 41, because the condition on line 40 was never true

41 def assertRaisesRegex(self, *args, **kwargs):

42 return self.assertRaisesRegexp(*args, **kwargs)