55# requiring that type to be used.
66
77import sys
8+ import io
9+
810_py3 = sys .version_info [0 ] > 2
911
10- import io
1112if _py3 :
1213 StringIO = io .StringIO
1314else :
1415 StringIO = io .BytesIO
1516
17+
1618def b2s (b ):
1719 """Convert a UTF-8 encoded bytes object to the internal string format."""
1820 if _py3 :
1921 return b .decode ('utf-8' )
2022 else :
2123 return b
2224
25+
2326def s2b (s ):
2427 """Convert a string object to UTF-8 encoded bytes."""
2528 if _py3 :
2629 return s .encode ('utf-8' )
2730 else :
2831 return s
2932
33+
3034def bs2b (s ):
31- """Convert a string object or UTF-8 encoded bytes to UTF-8 encoded bytes."""
35+ """Convert a string object or UTF-8 encoded bytes to UTF-8 encoded bytes.
36+ """
3237 if _py3 :
3338 if isinstance (s , bytes ):
3439 return s
@@ -37,12 +42,14 @@ def bs2b(s):
3742 else :
3843 return s
3944
45+
4046def s2u (s , errors = 'strict' ):
4147 """Convert a string object to a Unicode string."""
4248 if _py3 :
4349 return s
4450 else :
45- return unicode (s , 'utf-8' , errors )
51+ return unicode (s , 'utf-8' , errors ) # noqa: 821
52+
4653
4754def u2s (u ):
4855 """Convert a Unicode string to the internal string format."""
@@ -51,51 +58,56 @@ def u2s(u):
5158 else :
5259 return u .encode ('utf-8' )
5360
61+
5462def us2u (s , errors = 'strict' ):
5563 """Convert a string or Unicode string to a Unicode string."""
5664 if _py3 :
5765 return s
5866 else :
59- if isinstance (s , unicode ):
67+ if isinstance (s , unicode ): # noqa: 821
6068 return s
6169 else :
62- return unicode (s , 'utf-8' , errors )
70+ return unicode (s , 'utf-8' , errors ) # noqa: 821
71+
6372
6473def us2s (u ):
6574 """Convert a string or Unicode string to the internal string format."""
6675 if _py3 :
6776 return u
6877 else :
69- if isinstance (u , unicode ):
78+ if isinstance (u , unicode ): # noqa: 821
7079 return u .encode ('utf-8' )
7180 else :
7281 return u
7382
83+
7484def uany2s (u ):
7585 """Convert a Unicode string or other object to the internal string format.
7686
7787 Objects that are not Unicode strings are passed to str()."""
7888 if _py3 :
7989 return str (u )
8090 else :
81- if isinstance (u , unicode ):
91+ if isinstance (u , unicode ): # noqa: 821
8292 return u .encode ('utf-8' )
8393 else :
8494 return str (u )
8595
96+
8697def is_us (s ):
8798 """Return whether an object is a string or Unicode string."""
8899 if _py3 :
89100 return isinstance (s , str )
90101 else :
91- return isinstance (s , str ) or isinstance (s , unicode )
102+ return isinstance (s , str ) or isinstance (s , unicode ) # noqa: 821
103+
92104
93105def uchr (c ):
94106 """Return the Unicode string containing the given character."""
95107 if _py3 :
96108 return chr (c )
97109 else :
98- return unichr (c )
110+ return unichr (c ) # noqa: 821
99111
100112# CSV files used for export and import represent strings in the style
101113# used by repr in Python 2; this means that each byte of the UTF-8
@@ -109,6 +121,7 @@ def uchr(c):
109121# databases, so that the database can be compatible between Python 2
110122# and Python 3.
111123
124+
112125def repr_export (v ):
113126 """Return a Python-2-style representation of a value for export to CSV."""
114127 if _py3 :
@@ -125,6 +138,7 @@ def repr_export(v):
125138 else :
126139 return repr (v )
127140
141+
128142def eval_import (s ):
129143 """Evaluate a Python-2-style value imported from a CSV file."""
130144 if _py3 :
0 commit comments