put all compat fix code into a compat.py, import it.
* imports fix
some modules have been moved or rearanged.
* unicode fix
use from __future__ import unicode_literals, it only supports py2.6+
fix `unicode`, `basestring` in py3+
* grammer
with expression: never use. if you want to support py2.5-
except ... as ...: use function fix
* sample compat.py
try:
____import urlparse
____from urllib import urlencode, quote, unquote, splithost, splittype
____urlparse.urlencode = urlencode
____urlparse.quote = quote
____urlparse.unquote = unquote
____urlparse.splithost = splithost
____urlparse.splittype = splittype
____# fix py2.5-
____if not hasattr(urlparse, 'parse_qs'):
________from cgi import parse_qs
________urlparse.parse_qs = parse_qs
____globals()['bytes'] = str
except ImportError:
____import urllib.parse as urlparse
____globals()['unicode'] = str
____globals()['basestring'] = str
没有评论:
发表评论