4
4
# few places, generally for interacting with external modules
5
5
# requiring that type to be used.
6
6
7
- import sys
7
+ import ast
8
8
import io
9
+ import sys
9
10
10
11
_py3 = sys .version_info [0 ] > 2
11
12
@@ -63,22 +64,20 @@ def us2u(s, errors='strict'):
63
64
"""Convert a string or Unicode string to a Unicode string."""
64
65
if _py3 :
65
66
return s
67
+ elif isinstance (s , unicode ): # noqa: F821
68
+ return s
66
69
else :
67
- if isinstance (s , unicode ): # noqa: F821
68
- return s
69
- else :
70
- return unicode (s , 'utf-8' , errors ) # noqa: F821
70
+ return unicode (s , 'utf-8' , errors ) # noqa: F821
71
71
72
72
73
73
def us2s (u ):
74
74
"""Convert a string or Unicode string to the internal string format."""
75
75
if _py3 :
76
76
return u
77
+ elif isinstance (u , unicode ): # noqa: F821
78
+ return u .encode ('utf-8' )
77
79
else :
78
- if isinstance (u , unicode ): # noqa: F821
79
- return u .encode ('utf-8' )
80
- else :
81
- return u
80
+ return u
82
81
83
82
84
83
def uany2s (u ):
@@ -87,19 +86,18 @@ def uany2s(u):
87
86
Objects that are not Unicode strings are passed to str()."""
88
87
if _py3 :
89
88
return str (u )
89
+ elif isinstance (u , unicode ): # noqa: F821
90
+ return u .encode ('utf-8' )
90
91
else :
91
- if isinstance (u , unicode ): # noqa: F821
92
- return u .encode ('utf-8' )
93
- else :
94
- return str (u )
92
+ return str (u )
95
93
96
94
97
95
def is_us (s ):
98
96
"""Return whether an object is a string or Unicode string."""
99
97
if _py3 :
100
98
return isinstance (s , str )
101
99
else :
102
- return isinstance (s , str ) or isinstance ( s , unicode ) # noqa: F821
100
+ return isinstance (s , ( str , unicode ) ) # noqa: F821
103
101
104
102
105
103
def uchr (c ):
@@ -141,28 +139,37 @@ def repr_export(v):
141
139
142
140
def eval_import (s ):
143
141
"""Evaluate a Python-2-style value imported from a CSV file."""
144
- if _py3 :
145
- try :
146
- v = eval (s )
147
- except SyntaxError :
148
- # handle case where link operation reports id a long int
149
- # ('issue', 5002L, "status") rather than as a string.
150
- # This was a bug that existed and was fixed before or with v1.2.0
151
- import re
152
- v = eval (re .sub (r', ([0-9]+)L,' , r', \1,' , s ))
153
-
154
- if isinstance (v , str ):
155
- return v .encode ('iso-8859-1' ).decode ('utf-8' )
156
- elif isinstance (v , dict ):
157
- v_mod = {}
158
- for key , value in v .items ():
159
- if isinstance (key , str ):
160
- key = key .encode ('iso-8859-1' ).decode ('utf-8' )
161
- if isinstance (value , str ):
162
- value = value .encode ('iso-8859-1' ).decode ('utf-8' )
163
- v_mod [key ] = value
164
- return v_mod
142
+ try :
143
+ if _py3 :
144
+ try :
145
+ v = ast .literal_eval (s )
146
+ except SyntaxError :
147
+ # handle case where link operation reports id a long
148
+ # int ('issue', 5002L, "status") rather than as a
149
+ # string. This was a bug that existed and was fixed
150
+ # before or with v1.2.0
151
+ import re
152
+ v = ast .literal_eval (re .sub (r', ([0-9]+)L,' , r', \1,' , s ))
153
+
154
+ if isinstance (v , str ):
155
+ return v .encode ('iso-8859-1' ).decode ('utf-8' )
156
+ elif isinstance (v , dict ):
157
+ v_mod = {}
158
+ # ruff: noqa: PLW2901
159
+ for key , value in v .items ():
160
+ if isinstance (key , str ):
161
+ key = key .encode ('iso-8859-1' ).decode ('utf-8' )
162
+ if isinstance (value , str ):
163
+ value = value .encode ('iso-8859-1' ).decode ('utf-8' )
164
+ v_mod [key ] = value
165
+ return v_mod
166
+ else :
167
+ return v
165
168
else :
166
- return v
167
- else :
168
- return eval (s )
169
+ return ast .literal_eval (s )
170
+
171
+ except (ValueError , SyntaxError ) as e :
172
+ raise ValueError (
173
+ ("Error %(exception)s trying to parse value '%(value)s'" ) %
174
+ { 'exception' : e , 'value' : s })
175
+
0 commit comments