Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/mako/compat.py: 35%
46 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:02 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:02 +0000
1# mako/compat.py
2# Copyright 2006-2023 the Mako authors and contributors <see AUTHORS file>
3#
4# This module is part of Mako and is released under
5# the MIT License: http://www.opensource.org/licenses/mit-license.php
7import collections
8from importlib import util
9import inspect
10import sys
12win32 = sys.platform.startswith("win")
13pypy = hasattr(sys, "pypy_version_info")
14py38 = sys.version_info >= (3, 8)
16ArgSpec = collections.namedtuple(
17 "ArgSpec", ["args", "varargs", "keywords", "defaults"]
18)
21def inspect_getargspec(func):
22 """getargspec based on fully vendored getfullargspec from Python 3.3."""
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")
29 co = func.__code__
30 if not inspect.iscode(co):
31 raise TypeError(f"{co!r} is not a code object")
33 nargs = co.co_argcount
34 names = co.co_varnames
35 nkwargs = co.co_kwonlyargcount
36 args = list(names[:nargs])
38 nargs += nkwargs
39 varargs = None
40 if co.co_flags & inspect.CO_VARARGS:
41 varargs = co.co_varnames[nargs]
42 nargs = nargs + 1
43 varkw = None
44 if co.co_flags & inspect.CO_VARKEYWORDS:
45 varkw = co.co_varnames[nargs]
47 return ArgSpec(args, varargs, varkw, func.__defaults__)
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 module
57def exception_as():
58 return sys.exc_info()[1]
61def exception_name(exc):
62 return exc.__class__.__name__
65if py38:
66 from importlib import metadata as importlib_metadata
67else:
68 import importlib_metadata # noqa
71def importlib_metadata_get(group):
72 ep = importlib_metadata.entry_points()
73 if hasattr(ep, "select"):
74 return ep.select(group=group)
75 else:
76 return ep.get(group, ())