2009-09-05 01:08:10 +08:00
|
|
|
import py
|
2009-08-28 22:25:29 +08:00
|
|
|
import sys
|
|
|
|
|
|
|
|
if sys.version_info >= (3, 0):
|
|
|
|
exec ("print_ = print ; exec_=exec")
|
2009-08-29 01:16:15 +08:00
|
|
|
import builtins
|
2009-08-29 22:40:03 +08:00
|
|
|
|
|
|
|
# some backward compatibility helpers
|
|
|
|
_basestring = str
|
2009-08-29 21:51:49 +08:00
|
|
|
def _totext(obj, encoding):
|
2009-09-01 01:51:25 +08:00
|
|
|
if isinstance(obj, bytes):
|
|
|
|
obj = obj.decode(encoding)
|
|
|
|
elif not isinstance(obj, str):
|
|
|
|
obj = str(obj)
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def _isbytes(x):
|
|
|
|
return isinstance(x, bytes)
|
|
|
|
def _istext(x):
|
|
|
|
return isinstance(x, str)
|
2009-08-29 01:16:15 +08:00
|
|
|
|
2009-09-01 22:10:21 +08:00
|
|
|
def _getimself(function):
|
|
|
|
return getattr(function, '__self__', None)
|
|
|
|
|
2009-09-04 05:45:28 +08:00
|
|
|
def _getfuncdict(function):
|
|
|
|
return getattr(function, "__dict__", None)
|
|
|
|
|
2009-08-30 04:34:24 +08:00
|
|
|
def execfile(fn, globs=None, locs=None):
|
|
|
|
if globs is None:
|
|
|
|
back = sys._getframe(1)
|
|
|
|
globs = back.f_globals
|
|
|
|
locs = back.f_locals
|
|
|
|
del back
|
|
|
|
elif locs is None:
|
|
|
|
locs = globs
|
|
|
|
fp = open(fn, "rb")
|
|
|
|
try:
|
|
|
|
source = fp.read()
|
|
|
|
finally:
|
|
|
|
fp.close()
|
2009-09-04 05:38:15 +08:00
|
|
|
co = compile(source, fn, "exec", dont_inherit=True)
|
|
|
|
exec_(co, globs, locs)
|
2009-08-30 04:34:24 +08:00
|
|
|
|
2009-08-30 04:46:50 +08:00
|
|
|
def callable(obj):
|
|
|
|
return hasattr(obj, "__call__")
|
|
|
|
|
2009-08-28 22:25:29 +08:00
|
|
|
else:
|
2009-09-04 22:32:49 +08:00
|
|
|
import __builtin__ as builtins
|
2009-08-29 21:51:49 +08:00
|
|
|
_totext = unicode
|
2009-08-29 22:40:03 +08:00
|
|
|
_basestring = basestring
|
2009-08-30 04:34:24 +08:00
|
|
|
execfile = execfile
|
2009-08-30 04:46:50 +08:00
|
|
|
callable = callable
|
2009-09-01 01:51:25 +08:00
|
|
|
def _isbytes(x):
|
|
|
|
return isinstance(x, str)
|
|
|
|
def _istext(x):
|
|
|
|
return isinstance(x, unicode)
|
2009-08-29 22:40:03 +08:00
|
|
|
|
2009-09-01 22:10:21 +08:00
|
|
|
def _getimself(function):
|
|
|
|
return getattr(function, 'im_self', None)
|
|
|
|
|
2009-09-04 05:45:28 +08:00
|
|
|
def _getfuncdict(function):
|
|
|
|
return getattr(function, "__dict__", None)
|
|
|
|
|
2009-08-28 22:25:29 +08:00
|
|
|
def print_(*args, **kwargs):
|
|
|
|
""" minimal backport of py3k print statement. """
|
2009-08-29 19:47:10 +08:00
|
|
|
sep = ' '
|
|
|
|
if 'sep' in kwargs:
|
|
|
|
sep = kwargs.pop('sep')
|
|
|
|
end = '\n'
|
|
|
|
if 'end' in kwargs:
|
|
|
|
end = kwargs.pop('end')
|
2009-08-28 22:25:29 +08:00
|
|
|
file = 'file' in kwargs and kwargs.pop('file') or sys.stdout
|
|
|
|
if kwargs:
|
|
|
|
args = ", ".join([str(x) for x in kwargs])
|
|
|
|
raise TypeError("invalid keyword arguments: %s" % args)
|
2009-08-30 03:39:37 +08:00
|
|
|
at_start = True
|
|
|
|
for x in args:
|
|
|
|
if not at_start:
|
|
|
|
file.write(sep)
|
|
|
|
file.write(str(x))
|
|
|
|
at_start = False
|
|
|
|
file.write(end)
|
2009-08-28 22:25:29 +08:00
|
|
|
|
|
|
|
def exec_(obj, globals=None, locals=None):
|
|
|
|
""" minimal backport of py3k exec statement. """
|
|
|
|
if globals is None:
|
|
|
|
frame = sys._getframe(1)
|
|
|
|
globals = frame.f_globals
|
|
|
|
if locals is None:
|
|
|
|
locals = frame.f_locals
|
|
|
|
elif locals is None:
|
|
|
|
locals = globals
|
|
|
|
exec2(obj, globals, locals)
|
|
|
|
|
|
|
|
if sys.version_info >= (3,0):
|
|
|
|
exec ("""
|
|
|
|
def _reraise(cls, val, tb):
|
|
|
|
assert hasattr(val, '__traceback__')
|
|
|
|
raise val
|
|
|
|
""")
|
|
|
|
else:
|
|
|
|
exec ("""
|
|
|
|
def _reraise(cls, val, tb):
|
|
|
|
raise cls, val, tb
|
|
|
|
def exec2(obj, globals, locals):
|
|
|
|
exec obj in globals, locals
|
|
|
|
""")
|
2009-09-05 01:08:10 +08:00
|
|
|
|
|
|
|
def _tryimport(*names):
|
|
|
|
""" return the first successfully imported module. """
|
|
|
|
assert names
|
|
|
|
for name in names:
|
|
|
|
try:
|
|
|
|
return __import__(name, None, None, '__doc__')
|
|
|
|
except ImportError:
|
|
|
|
excinfo = sys.exc_info()
|
|
|
|
py.builtin._reraise(*excinfo)
|