123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- """Internal module for Python 2 backwards compatibility."""
- import sys
- if sys.version_info[0] < 3:
- from urllib import unquote
- from urlparse import parse_qs, urlparse
- from itertools import imap, izip
- from string import letters as ascii_letters
- from Queue import Queue
- try:
- from cStringIO import StringIO as BytesIO
- except ImportError:
- from StringIO import StringIO as BytesIO
- # special unicode handling for python2 to avoid UnicodeDecodeError
- def safe_unicode(obj, *args):
- """ return the unicode representation of obj """
- try:
- return unicode(obj, *args)
- except UnicodeDecodeError:
- # obj is byte string
- ascii_text = str(obj).encode('string_escape')
- return unicode(ascii_text)
- def iteritems(x):
- return x.iteritems()
- def iterkeys(x):
- return x.iterkeys()
- def itervalues(x):
- return x.itervalues()
- def nativestr(x):
- return x if isinstance(x, str) else x.encode('utf-8', 'replace')
- def u(x):
- return x.decode()
- def b(x):
- return x
- def next(x):
- return x.next()
- def byte_to_chr(x):
- return x
- unichr = unichr
- xrange = xrange
- basestring = basestring
- unicode = unicode
- bytes = str
- long = long
- else:
- from urllib.parse import parse_qs, unquote, urlparse
- from io import BytesIO
- from string import ascii_letters
- from queue import Queue
- def iteritems(x):
- return iter(x.items())
- def iterkeys(x):
- return iter(x.keys())
- def itervalues(x):
- return iter(x.values())
- def byte_to_chr(x):
- return chr(x)
- def nativestr(x):
- return x if isinstance(x, str) else x.decode('utf-8', 'replace')
- def u(x):
- return x
- def b(x):
- return x.encode('latin-1') if not isinstance(x, bytes) else x
- next = next
- unichr = chr
- imap = map
- izip = zip
- xrange = range
- basestring = str
- unicode = str
- safe_unicode = str
- bytes = bytes
- long = int
- try: # Python 3
- from queue import LifoQueue, Empty, Full
- except ImportError:
- from Queue import Empty, Full
- try: # Python 2.6 - 2.7
- from Queue import LifoQueue
- except ImportError: # Python 2.5
- from Queue import Queue
- # From the Python 2.7 lib. Python 2.5 already extracted the core
- # methods to aid implementating different queue organisations.
- class LifoQueue(Queue):
- "Override queue methods to implement a last-in first-out queue."
- def _init(self, maxsize):
- self.maxsize = maxsize
- self.queue = []
- def _qsize(self, len=len):
- return len(self.queue)
- def _put(self, item):
- self.queue.append(item)
- def _get(self):
- return self.queue.pop()
|