diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-05-29 18:18:04 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-05-31 14:03:02 -0400 |
commit | fb7f0437323ba836c68947d38f3604c3336e3a9b (patch) | |
tree | 756512a2b68f1c7270f028b487228fd0e3f13778 /mako/compat.py | |
parent | db498f217e03d772e0c0c37a526226d48ed790e2 (diff) | |
download | mako-fb7f0437323ba836c68947d38f3604c3336e3a9b.tar.gz |
Use tox / zimports
Change-Id: Ia047c7052a6d242c2cf1c7a83981f1e38ea4d928
Diffstat (limited to 'mako/compat.py')
-rw-r--r-- | mako/compat.py | 64 |
1 files changed, 45 insertions, 19 deletions
diff --git a/mako/compat.py b/mako/compat.py index 312bbd8..5f9d9ac 100644 --- a/mako/compat.py +++ b/mako/compat.py @@ -1,3 +1,4 @@ +import json # noqa import sys import time @@ -5,9 +6,9 @@ py3k = sys.version_info >= (3, 0) py33 = sys.version_info >= (3, 3) py2k = sys.version_info < (3,) py27 = sys.version_info >= (2, 7) -jython = sys.platform.startswith('java') -win32 = sys.platform.startswith('win') -pypy = hasattr(sys, 'pypy_version_info') +jython = sys.platform.startswith("java") +win32 = sys.platform.startswith("win") +pypy = hasattr(sys, "pypy_version_info") if py3k: # create a "getargspec" from getfullargspec(), which is not deprecated @@ -17,15 +18,16 @@ if py3k: # with that for now. import collections + ArgSpec = collections.namedtuple( - "ArgSpec", - ["args", "varargs", "keywords", "defaults"]) + "ArgSpec", ["args", "varargs", "keywords", "defaults"] + ) from inspect import getfullargspec as inspect_getfullargspec def inspect_getargspec(func): - return ArgSpec( - *inspect_getfullargspec(func)[0:4] - ) + return ArgSpec(*inspect_getfullargspec(func)[0:4]) + + else: from inspect import getargspec as inspect_getargspec # noqa @@ -35,7 +37,8 @@ if py3k: import builtins as compat_builtins from urllib.parse import quote_plus, unquote_plus from html.entities import codepoint2name, name2codepoint - string_types = str, + + string_types = (str,) binary_type = bytes text_type = str @@ -50,8 +53,10 @@ if py3k: def octal(lit): return eval("0o" + lit) + else: import __builtin__ as compat_builtins # noqa + try: from cStringIO import StringIO except: @@ -61,7 +66,8 @@ else: from urllib import quote_plus, unquote_plus # noqa from htmlentitydefs import codepoint2name, name2codepoint # noqa - string_types = basestring, # noqa + + string_types = (basestring,) # noqa binary_type = str text_type = unicode # noqa @@ -80,11 +86,13 @@ if py33: def load_module(module_id, path): return machinery.SourceFileLoader(module_id, path).load_module() + + else: import imp def load_module(module_id, path): - fp = open(path, 'rb') + fp = open(path, "rb") try: return imp.load_source(module_id, path, fp) finally: @@ -92,28 +100,36 @@ else: if py3k: + def reraise(tp, value, tb=None, cause=None): if cause is not None: value.__cause__ = cause if value.__traceback__ is not tb: raise value.with_traceback(tb) raise value + + else: - exec("def reraise(tp, value, tb=None, cause=None):\n" - " raise tp, value, tb\n") + exec( + "def reraise(tp, value, tb=None, cause=None):\n" + " raise tp, value, tb\n" + ) def exception_as(): return sys.exc_info()[1] + try: import threading + if py3k: import _thread as thread else: import thread except ImportError: import dummy_threading as threading # noqa + if py3k: import _dummy_thread as thread else: @@ -127,21 +143,23 @@ else: try: from functools import partial except: + def partial(func, *args, **keywords): def newfunc(*fargs, **fkeywords): newkeywords = keywords.copy() newkeywords.update(fkeywords) return func(*(args + fargs), **newkeywords) + return newfunc -all = all -import json # noqa +all = all # noqa def exception_name(exc): return exc.__class__.__name__ + try: from inspect import CO_VARKEYWORDS, CO_VARARGS @@ -167,17 +185,23 @@ try: return args, varargs, varkw, fn.__defaults__ else: return args, varargs, varkw, fn.func_defaults + + except ImportError: import inspect def inspect_func_args(fn): return inspect.getargspec(fn) + if py3k: - def callable(fn): - return hasattr(fn, '__call__') + # TODO: this has been restored in py3k + def callable(fn): # noqa + return hasattr(fn, "__call__") + + else: - callable = callable + callable = callable # noqa ################################################ @@ -186,6 +210,8 @@ else: def with_metaclass(meta, base=object): """Create a base class with a metaclass.""" return meta("%sBase" % meta.__name__, (base,), {}) + + ################################################ @@ -194,7 +220,7 @@ def arg_stringname(func_arg): In Python3.4 a function's args are of _ast.arg type not _ast.name """ - if hasattr(func_arg, 'arg'): + if hasattr(func_arg, "arg"): return func_arg.arg else: return str(func_arg) |