view 2.00/compat.py @ 16:f2279b7602d3

Initial 2.00 commit
author Oleg Oshmyan <chortos@inbox.lv>
date Mon, 07 Jun 2010 23:36:52 +0000
parents
children ec6f1a132109
line wrap: on
line source

#!/usr/bin/python
# Copyright (c) 2010 Chortos-2 <chortos@inbox.lv>

try:
	# Python 3
	exec('say = print')
except SyntaxError:
	try:
		# Python 2.6/2.7
		exec('say = __builtins__["print"]')
	except Exception:
		# Python 2.5
		import sys
		# This should fully emulate the print function of Python 2.6 in Python 2.3+
		# The error messages are taken from Python 2.6/2.7
		def saytypeerror(value, name):
			return TypeError(name + ' must be None, str or unicode, not ' + type(value).__name__)
		def say(*values, **kwargs):
			sep  = kwargs.pop('sep' , None)
			end  = kwargs.pop('end' , None)
			file = kwargs.pop('file', None)
			if kwargs: raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.popitem()[0])
			if sep  is None: sep  = ' '
			if end  is None: end  = '\n'
			if file is None: file = sys.stdout
			if not isinstance(sep, basestring): raise saytypeerror(sep, 'sep')
			if not isinstance(end, basestring): raise saytypeerror(end, 'end')
			file.write(sep.join((str(i) for i in values)) + end)

def import_urllib():
	try:
		# Python 3
		import urllib.request
		return urllib.request, lambda url: urllib.request.urlopen(url).read().decode()
	except ImportError:
		# Python 2
		import urllib
		return urllib, lambda url: urllib.urlopen(url).read()