]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/util.py
make inversion optional
[z_archive/twitter.git] / twitter / util.py
index 3dca53e6807cb0f7e0e512500b134209d2e529c3..b0a1f48e854d46f5845fc9e57157ae2f024c2778 100644 (file)
@@ -7,12 +7,17 @@ Internal utility functions.
 
 
 import re
-from html.entities import name2codepoint
+import sys
+try:
+    from html.entities import name2codepoint
+    unichr = chr
+except ImportError:
+    from htmlentitydefs import name2codepoint
 
 def htmlentitydecode(s):
     return re.sub(
         '&(%s);' % '|'.join(name2codepoint),
-        lambda m: chr(name2codepoint[m.group(1)]), s)
+        lambda m: unichr(name2codepoint[m.group(1)]), s)
 
 def smrt_input(globals_, locals_, ps1=">>> ", ps2="... "):
     inputs = []
@@ -25,9 +30,16 @@ def smrt_input(globals_, locals_, ps1=">>> ", ps2="... "):
         try:
             ret = eval('\n'.join(inputs), globals_, locals_)
             if ret:
-                print(ret)
+                print(str(ret))
             return
         except SyntaxError:
             pass
 
+def printNicely(string):
+    if hasattr(sys.stdout, 'buffer'):
+        sys.stdout.buffer.write(string.encode('utf8'))
+        print()
+    else:
+        print(string.encode('utf8'))
+
 __all__ = ["htmlentitydecode", "smrt_input"]