Path: blob/main/test/lib/python3.9/site-packages/setuptools/depends.py
4798 views
import sys1import marshal2import contextlib3import dis45from setuptools.extern.packaging import version67from ._imp import find_module, PY_COMPILED, PY_FROZEN, PY_SOURCE8from . import _imp91011__all__ = [12'Require', 'find_module', 'get_module_constant', 'extract_constant'13]141516class Require:17"""A prerequisite to building or installing a distribution"""1819def __init__(20self, name, requested_version, module, homepage='',21attribute=None, format=None):2223if format is None and requested_version is not None:24format = version.Version2526if format is not None:27requested_version = format(requested_version)28if attribute is None:29attribute = '__version__'3031self.__dict__.update(locals())32del self.self3334def full_name(self):35"""Return full package/distribution name, w/version"""36if self.requested_version is not None:37return '%s-%s' % (self.name, self.requested_version)38return self.name3940def version_ok(self, version):41"""Is 'version' sufficiently up-to-date?"""42return self.attribute is None or self.format is None or \43str(version) != "unknown" and self.format(version) >= self.requested_version4445def get_version(self, paths=None, default="unknown"):46"""Get version number of installed module, 'None', or 'default'4748Search 'paths' for module. If not found, return 'None'. If found,49return the extracted version attribute, or 'default' if no version50attribute was specified, or the value cannot be determined without51importing the module. The version is formatted according to the52requirement's version format (if any), unless it is 'None' or the53supplied 'default'.54"""5556if self.attribute is None:57try:58f, p, i = find_module(self.module, paths)59if f:60f.close()61return default62except ImportError:63return None6465v = get_module_constant(self.module, self.attribute, default, paths)6667if v is not None and v is not default and self.format is not None:68return self.format(v)6970return v7172def is_present(self, paths=None):73"""Return true if dependency is present on 'paths'"""74return self.get_version(paths) is not None7576def is_current(self, paths=None):77"""Return true if dependency is present and up-to-date on 'paths'"""78version = self.get_version(paths)79if version is None:80return False81return self.version_ok(str(version))828384def maybe_close(f):85@contextlib.contextmanager86def empty():87yield88return89if not f:90return empty()9192return contextlib.closing(f)939495def get_module_constant(module, symbol, default=-1, paths=None):96"""Find 'module' by searching 'paths', and extract 'symbol'9798Return 'None' if 'module' does not exist on 'paths', or it does not define99'symbol'. If the module defines 'symbol' as a constant, return the100constant. Otherwise, return 'default'."""101102try:103f, path, (suffix, mode, kind) = info = find_module(module, paths)104except ImportError:105# Module doesn't exist106return None107108with maybe_close(f):109if kind == PY_COMPILED:110f.read(8) # skip magic & date111code = marshal.load(f)112elif kind == PY_FROZEN:113code = _imp.get_frozen_object(module, paths)114elif kind == PY_SOURCE:115code = compile(f.read(), path, 'exec')116else:117# Not something we can parse; we'll have to import it. :(118imported = _imp.get_module(module, paths, info)119return getattr(imported, symbol, None)120121return extract_constant(code, symbol, default)122123124def extract_constant(code, symbol, default=-1):125"""Extract the constant value of 'symbol' from 'code'126127If the name 'symbol' is bound to a constant value by the Python code128object 'code', return that value. If 'symbol' is bound to an expression,129return 'default'. Otherwise, return 'None'.130131Return value is based on the first assignment to 'symbol'. 'symbol' must132be a global, or at least a non-"fast" local in the code block. That is,133only 'STORE_NAME' and 'STORE_GLOBAL' opcodes are checked, and 'symbol'134must be present in 'code.co_names'.135"""136if symbol not in code.co_names:137# name's not there, can't possibly be an assignment138return None139140name_idx = list(code.co_names).index(symbol)141142STORE_NAME = 90143STORE_GLOBAL = 97144LOAD_CONST = 100145146const = default147148for byte_code in dis.Bytecode(code):149op = byte_code.opcode150arg = byte_code.arg151152if op == LOAD_CONST:153const = code.co_consts[arg]154elif arg == name_idx and (op == STORE_NAME or op == STORE_GLOBAL):155return const156else:157const = default158159160def _update_globals():161"""162Patch the globals to remove the objects not available on some platforms.163164XXX it'd be better to test assertions about bytecode instead.165"""166167if not sys.platform.startswith('java') and sys.platform != 'cli':168return169incompatible = 'extract_constant', 'get_module_constant'170for name in incompatible:171del globals()[name]172__all__.remove(name)173174175_update_globals()176177178