44# few places, generally for interacting with external modules
55# requiring that type to be used.
66
7- import sys
7+ import ast
88import io
9+ import sys
910
1011_py3 = sys .version_info [0 ] > 2
1112
@@ -63,22 +64,20 @@ def us2u(s, errors='strict'):
6364 """Convert a string or Unicode string to a Unicode string."""
6465 if _py3 :
6566 return s
67+ elif isinstance (s , unicode ): # noqa: F821
68+ return s
6669 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
7171
7272
7373def us2s (u ):
7474 """Convert a string or Unicode string to the internal string format."""
7575 if _py3 :
7676 return u
77+ elif isinstance (u , unicode ): # noqa: F821
78+ return u .encode ('utf-8' )
7779 else :
78- if isinstance (u , unicode ): # noqa: F821
79- return u .encode ('utf-8' )
80- else :
81- return u
80+ return u
8281
8382
8483def uany2s (u ):
@@ -87,19 +86,18 @@ def uany2s(u):
8786 Objects that are not Unicode strings are passed to str()."""
8887 if _py3 :
8988 return str (u )
89+ elif isinstance (u , unicode ): # noqa: F821
90+ return u .encode ('utf-8' )
9091 else :
91- if isinstance (u , unicode ): # noqa: F821
92- return u .encode ('utf-8' )
93- else :
94- return str (u )
92+ return str (u )
9593
9694
9795def is_us (s ):
9896 """Return whether an object is a string or Unicode string."""
9997 if _py3 :
10098 return isinstance (s , str )
10199 else :
102- return isinstance (s , str ) or isinstance ( s , unicode ) # noqa: F821
100+ return isinstance (s , ( str , unicode ) ) # noqa: F821
103101
104102
105103def uchr (c ):
@@ -141,28 +139,37 @@ def repr_export(v):
141139
142140def eval_import (s ):
143141 """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
165168 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