88 long = long
99 raw_input = raw_input
1010 unicode = unicode
11+ unichr = unichr
1112 basestring = basestring
12- def as_bytes (s ): return s if isinstance (s , str ) else s .encode ('utf-8' )
13- def as_string (s ): return s if isinstance (s , unicode ) else s .decode ('utf-8' )
13+
14+ def as_bytes (s , encoding = 'utf-8' ):
15+ if isinstance (s , str ):
16+ return s
17+ else :
18+ return s .encode (encoding )
19+
20+ def as_string (s , encoding = 'utf-8' ):
21+ if isinstance (s , unicode ):
22+ return s
23+ else :
24+ return s .decode (encoding )
1425
1526 def is_text_stream (stream ):
1627 try :
@@ -25,15 +36,28 @@ def is_text_stream(stream):
2536 except ImportError :
2637 import io
2738 return isinstance (stream , io .TextIOWrapper )
39+
2840else : # pragma: no cover
2941 long = int
3042 basestring = str
3143 raw_input = input
44+ unichr = chr
45+
3246 class unicode (str ):
3347 def __init__ (self , string , encoding , errors ):
3448 str .__init__ (self , string )
35- def as_bytes (s ): return s if isinstance (s ,bytes ) else s .encode ('utf8' )
36- def as_string (s ): return s if isinstance (s ,str ) else s .decode ('utf8' )
49+
50+ def as_bytes (s , encoding = 'utf8' ):
51+ if isinstance (s , bytes ):
52+ return s
53+ else :
54+ return s .encode (encoding )
55+
56+ def as_string (s , encoding = 'utf8' ):
57+ if isinstance (s , str ):
58+ return s
59+ else :
60+ return s .decode (encoding )
3761
3862 def is_text_stream (stream ):
3963 import _io
@@ -106,7 +130,22 @@ def is_text_stream(stream):
106130except ImportError : # pragma: no cover
107131 import _thread as thread
108132
133+ try : # pragma: no cover
134+ from types import StringTypes
135+ except ImportError : # pragma: no cover
136+ StringTypes = (str ,)
137+
109138try : # pragma: no cover
110139 from html import escape
111140except ImportError : # pragma: no cover
112141 from cgi import escape
142+
143+ try : # pragma: no cover
144+ import html .entities as htmlentitydefs
145+ except ImportError : # pragma: no cover
146+ import htmlentitydefs
147+
148+ try : # pragma: no cover
149+ from html .parser import HTMLParser
150+ except ImportError : # pragma: no cover
151+ from HTMLParser import HTMLParser
0 commit comments