compat.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import sys
  2. import itertools
  3. if sys.version_info[0] < 3:
  4. PY3 = False
  5. basestring = basestring
  6. import __builtin__ as builtins
  7. import ConfigParser
  8. from StringIO import StringIO
  9. BytesIO = StringIO
  10. execfile = execfile
  11. func_code = lambda o: o.func_code
  12. func_globals = lambda o: o.func_globals
  13. im_func = lambda o: o.im_func
  14. from htmlentitydefs import name2codepoint
  15. import httplib
  16. from BaseHTTPServer import HTTPServer
  17. from SimpleHTTPServer import SimpleHTTPRequestHandler
  18. from BaseHTTPServer import BaseHTTPRequestHandler
  19. iteritems = lambda o: o.iteritems()
  20. long_type = long
  21. maxsize = sys.maxint
  22. next = lambda o: o.next()
  23. numeric_types = (int, long, float)
  24. unichr = unichr
  25. unicode = unicode
  26. bytes = str
  27. from urllib import url2pathname, splittag, pathname2url
  28. import urllib2
  29. from urllib2 import urlopen, HTTPError, URLError, unquote, splituser
  30. from urlparse import urlparse, urlunparse, urljoin, urlsplit, urlunsplit
  31. filterfalse = itertools.ifilterfalse
  32. exec("""def reraise(tp, value, tb=None):
  33. raise tp, value, tb""")
  34. else:
  35. PY3 = True
  36. basestring = str
  37. import builtins
  38. import configparser as ConfigParser
  39. from io import StringIO, BytesIO
  40. func_code = lambda o: o.__code__
  41. func_globals = lambda o: o.__globals__
  42. im_func = lambda o: o.__func__
  43. from html.entities import name2codepoint
  44. import http.client as httplib
  45. from http.server import HTTPServer, SimpleHTTPRequestHandler
  46. from http.server import BaseHTTPRequestHandler
  47. iteritems = lambda o: o.items()
  48. long_type = int
  49. maxsize = sys.maxsize
  50. next = next
  51. numeric_types = (int, float)
  52. unichr = chr
  53. unicode = str
  54. bytes = bytes
  55. from urllib.error import HTTPError, URLError
  56. import urllib.request as urllib2
  57. from urllib.request import urlopen, url2pathname, pathname2url
  58. from urllib.parse import (
  59. urlparse, urlunparse, unquote, splituser, urljoin, urlsplit,
  60. urlunsplit, splittag,
  61. )
  62. filterfalse = itertools.filterfalse
  63. def execfile(fn, globs=None, locs=None):
  64. if globs is None:
  65. globs = globals()
  66. if locs is None:
  67. locs = globs
  68. f = open(fn, 'rb')
  69. try:
  70. source = f.read()
  71. finally:
  72. f.close()
  73. exec(compile(source, fn, 'exec'), globs, locs)
  74. def reraise(tp, value, tb=None):
  75. if value.__traceback__ is not tb:
  76. raise value.with_traceback(tb)
  77. raise value