codekingpro/portable-devtools
114k
1# mako/compat.py2# Copyright 2006-2024 the Mako authors and contributors <see AUTHORS file>3#4# This module is part of Mako and is released under5# the MIT License: http://www.opensource.org/licenses/mit-license.php6 7import collections8from importlib import metadata as importlib_metadata9from importlib import util10import inspect11import sys12 13win32 = sys.platform.startswith("win")14pypy = hasattr(sys, "pypy_version_info")15 16ArgSpec = collections.namedtuple(17 "ArgSpec", ["args", "varargs", "keywords", "defaults"]18)19 20 21def inspect_getargspec(func):22 """getargspec based on fully vendored getfullargspec from Python 3.3."""23 24 if inspect.ismethod(func):25 func = func.__func__26 if not inspect.isfunction(func):27 raise TypeError(f"{func!r} is not a Python function")28 29 co = func.__code__30 if not inspect.iscode(co):31 raise TypeError(f"{co!r} is not a code object")32 33 nargs = co.co_argcount34 names = co.co_varnames35 nkwargs = co.co_kwonlyargcount36 args = list(names[:nargs])37 38 nargs += nkwargs39 varargs = None40 if co.co_flags & inspect.CO_VARARGS:41 varargs = co.co_varnames[nargs]42 nargs = nargs + 143 varkw = None44 if co.co_flags & inspect.CO_VARKEYWORDS:45 varkw = co.co_varnames[nargs]46 47 return ArgSpec(args, varargs, varkw, func.__defaults__)48 49 50def load_module(module_id, path):51 spec = util.spec_from_file_location(module_id, path)52 module = util.module_from_spec(spec)53 spec.loader.exec_module(module)54 return module55 56 57def exception_as():58 return sys.exc_info()[1]59 60 61def exception_name(exc):62 return exc.__class__.__name__63 64 65def importlib_metadata_get(group):66 ep = importlib_metadata.entry_points()67 if hasattr(ep, "select"):68 return ep.select(group=group)69 else:70 return ep.get(group, ())71 