1
|
|
# coding=utf-8
|
2
|
|
# This file is part of Buildbot. Buildbot is free software: you can
|
3
|
|
# redistribute it and/or modify it under the terms of the GNU General Public
|
4
|
|
# License as published by the Free Software Foundation, version 2.
|
5
|
|
#
|
6
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
7
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
8
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
9
|
|
# details.
|
10
|
|
#
|
11
|
|
# You should have received a copy of the GNU General Public License along with
|
12
|
|
# this program; if not, write to the Free Software Foundation, Inc., 51
|
13
|
|
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
14
|
|
#
|
15
|
|
# Copyright Buildbot Team Members
|
16
|
|
|
17
|
4
|
"""
|
18
|
|
Helpers for handling compatibility differences
|
19
|
|
between Python 2 and Python 3.
|
20
|
|
"""
|
21
|
|
|
22
|
4
|
from __future__ import absolute_import
|
23
|
4
|
from __future__ import print_function
|
24
|
4
|
from future.utils import text_type
|
25
|
|
|
26
|
4
|
if str != bytes:
|
27
|
|
# On Python 3 and higher, str and bytes
|
28
|
|
# are not equivalent. We must use StringIO for
|
29
|
|
# doing io on native strings.
|
30
|
4
|
from io import StringIO as NativeStringIO
|
31
|
|
else:
|
32
|
|
# On Python 2 and older, str and bytes
|
33
|
|
# are equivalent. We must use BytesIO for
|
34
|
|
# doing io on native strings.
|
35
|
0
|
from io import BytesIO as NativeStringIO
|
36
|
|
|
37
|
|
|
38
|
4
|
def bytes2NativeString(x, encoding='utf-8'):
|
39
|
|
"""
|
40
|
|
Convert C{bytes} to a native C{str}.
|
41
|
|
|
42
|
|
On Python 3 and higher, str and bytes
|
43
|
|
are not equivalent. In this case, decode
|
44
|
|
the bytes, and return a native string.
|
45
|
|
|
46
|
|
On Python 2 and lower, str and bytes
|
47
|
|
are equivalent. In this case, just
|
48
|
|
just return the native string.
|
49
|
|
|
50
|
|
@param x: a string of type C{bytes}
|
51
|
|
@param encoding: an optional codec, default: 'utf-8'
|
52
|
|
@return: a string of type C{str}
|
53
|
|
"""
|
54
|
4
|
if isinstance(x, bytes) and str != bytes:
|
55
|
4
|
return x.decode(encoding)
|
56
|
0
|
return x
|
57
|
|
|
58
|
|
|
59
|
4
|
def unicode2bytes(x, encoding='utf-8', errors='strict'):
|
60
|
|
"""
|
61
|
|
Convert a unicode string to C{bytes}.
|
62
|
|
|
63
|
|
@param x: a unicode string, of type C{unicode} on Python 2,
|
64
|
|
or C{str} on Python 3.
|
65
|
|
@param encoding: an optional codec, default: 'utf-8'
|
66
|
|
@param errors: error handling scheme, default 'strict'
|
67
|
|
@return: a string of type C{bytes}
|
68
|
|
"""
|
69
|
4
|
if isinstance(x, text_type):
|
70
|
4
|
x = x.encode(encoding, errors)
|
71
|
4
|
return x
|
72
|
|
|
73
|
|
|
74
|
4
|
def bytes2unicode(x, encoding='utf-8', errors='strict'):
|
75
|
|
"""
|
76
|
|
Convert a C{bytes} to a unicode string.
|
77
|
|
|
78
|
|
@param x: a unicode string, of type C{unicode} on Python 2,
|
79
|
|
or C{str} on Python 3.
|
80
|
|
@param encoding: an optional codec, default: 'utf-8'
|
81
|
|
@param errors: error handling scheme, default 'strict'
|
82
|
|
@return: a unicode string of type C{unicode} on Python 2, or
|
83
|
|
C{str} on Python 3.
|
84
|
|
"""
|
85
|
4
|
if isinstance(x, (text_type, type(None))):
|
86
|
4
|
return x
|
87
|
4
|
return text_type(x, encoding, errors)
|
88
|
|
|
89
|
|
|
90
|
4
|
__all__ = [
|
91
|
|
"NativeStringIO",
|
92
|
|
"bytes2NativeString",
|
93
|
|
"bytes2unicode",
|
94
|
|
"unicode2bytes"
|
95
|
|
]
|