summaryrefslogtreecommitdiff
path: root/creole/py3compat.py
blob: f48da750e7a8419baa0fee255805c2c3ee22f76d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# coding: utf-8

"""
    Helper to support Python v2 and v3
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Some ideas borrowed from six
    
    See also:
        http://python3porting.com
        https://bitbucket.org/gutworth/six/src/tip/six.py
        http://packages.python.org/six/
"""

from __future__ import division, absolute_import, print_function, unicode_literals

import sys
import doctest
import re

# True if we are running on Python 3.
PY3 = sys.version_info[0] == 3


if PY3:
    TEXT_TYPE = str
    BINARY_TYPE = bytes
else:
    TEXT_TYPE = unicode
    BINARY_TYPE = str

    # Simple remove 'u' from python 2 unicode repr string
    # See also:
    # http://bugs.python.org/issue3955
    # http://www.python-forum.de/viewtopic.php?f=1&t=27509 (de)
    origin_OutputChecker = doctest.OutputChecker
    class OutputChecker2(origin_OutputChecker):
        def check_output(self, want, got, optionflags):
            got = got.replace("u'", "'").replace('u"', '"')
            return origin_OutputChecker.check_output(self, want, got, optionflags)
    doctest.OutputChecker = OutputChecker2