"""Provide access to Python's configuration information. The specificconfiguration variables available depend heavily on the platform andconfiguration. The values may be retrieved usingget_config_var(name), and the list of variables is available viaget_config_vars().keys(). Additional convenience functions are alsoavailable.Written by: Fred L. Drake, Jr.Email: <fdrake@acm.org>"""import _impimport osimport reimport sysfrom .errors import DistutilsPlatformErrorfrom .util import get_platform, get_host_platform# These are needed in a couple of spots, so just compute them once.PREFIX = os.path.normpath(sys.prefix)EXEC_PREFIX = os.path.normpath(sys.exec_prefix)BASE_PREFIX = os.path.normpath(sys.base_prefix)BASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix)# Path to the base directory of the project. On Windows the binary may# live in project/PCbuild/win32 or project/PCbuild/amd64.# set for cross buildsif "_PYTHON_PROJECT_BASE" in os.environ:project_base = os.path.abspath(os.environ["_PYTHON_PROJECT_BASE"])else:if sys.executable:project_base = os.path.dirname(os.path.abspath(sys.executable))else:# sys.executable can be empty if argv[0] has been changed and Python is# unable to retrieve the real program nameproject_base = os.getcwd()# python_build: (Boolean) if true, we're either building Python or# building an extension with an un-installed Python, so we use# different (hard-wired) directories.def _is_python_source_dir(d):for fn in ("Setup", "Setup.local"):if os.path.isfile(os.path.join(d, "Modules", fn)):return Truereturn False_sys_home = getattr(sys, '_home', None)if os.name == 'nt':def _fix_pcbuild(d):if d and os.path.normcase(d).startswith(os.path.normcase(os.path.join(PREFIX, "PCbuild"))):return PREFIXreturn dproject_base = _fix_pcbuild(project_base)_sys_home = _fix_pcbuild(_sys_home)def _python_build():if _sys_home:return _is_python_source_dir(_sys_home)return _is_python_source_dir(project_base)python_build = _python_build()# Calculate the build qualifier flags if they are defined. Adding the flags# to the include and lib directories only makes sense for an installation, not# an in-source build.build_flags = ''try:if not python_build:build_flags = sys.abiflagsexcept AttributeError:# It's not a configure-based build, so the sys module doesn't have# this attribute, which is fine.passdef get_python_version():"""Return a string containing the major and minor Python version,leaving off the patchlevel. Sample return values could be '1.5'or '2.2'."""return '%d.%d' % sys.version_info[:2]def get_python_inc(plat_specific=0, prefix=None):"""Return the directory containing installed Python header files.If 'plat_specific' is false (the default), this is the path to thenon-platform-specific header files, i.e. Python.h and so on;otherwise, this is the path to platform-specific header files(namely pyconfig.h).If 'prefix' is supplied, use it instead of sys.base_prefix orsys.base_exec_prefix -- i.e., ignore 'plat_specific'."""if prefix is None:prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIXif os.name == "posix":if python_build:# Assume the executable is in the build directory. The# pyconfig.h file should be in the same directory. Since# the build directory may not be the source directory, we# must use "srcdir" from the makefile to find the "Include"# directory.if plat_specific:return _sys_home or project_baseelse:incdir = os.path.join(get_config_var('srcdir'), 'Include')return os.path.normpath(incdir)python_dir = 'python' + get_python_version() + build_flagsreturn os.path.join(prefix, "include", python_dir)elif os.name == "nt":if python_build:# Include both the include and PC dir to ensure we can find# pyconfig.hreturn (os.path.join(prefix, "include") + os.path.pathsep +os.path.join(prefix, "PC"))return os.path.join(prefix, "include")else:raise DistutilsPlatformError("I don't know where Python installs its C header files ""on platform '%s'" % os.name)def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):"""Return the directory containing the Python library (standard orsite additions).If 'plat_specific' is true, return the directory containingplatform-specific modules, i.e. any module from a non-pure-Pythonmodule distribution; otherwise, return the platform-shared librarydirectory. If 'standard_lib' is true, return the directorycontaining standard Python library modules; otherwise, return thedirectory for site-specific modules.If 'prefix' is supplied, use it instead of sys.base_prefix orsys.base_exec_prefix -- i.e., ignore 'plat_specific'."""if prefix is None:if standard_lib:prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIXelse:prefix = plat_specific and EXEC_PREFIX or PREFIXif os.name == "posix":libpython = os.path.join(prefix,"lib", "python" + get_python_version())if standard_lib:return libpythonelse:return os.path.join(libpython, "site-packages")elif os.name == "nt":if standard_lib:return os.path.join(prefix, "Lib")else:return os.path.join(prefix, "Lib", "site-packages")else:raise DistutilsPlatformError("I don't know where Python installs its library ""on platform '%s'" % os.name)def customize_compiler(compiler):"""Do any platform-specific customization of a CCompiler instance.Mainly needed on Unix, so we can plug in the information thatvaries across Unices and is stored in Python's Makefile."""if compiler.compiler_type == "unix":if sys.platform == "darwin":# Perform first-time customization of compiler-related# config vars on OS X now that we know we need a compiler.# This is primarily to support Pythons from binary# installers. The kind and paths to build tools on# the user system may vary significantly from the system# that Python itself was built on. Also the user OS# version and build tools may not support the same set# of CPU architectures for universal builds.global _config_vars# Use get_config_var() to ensure _config_vars is initialized.if not get_config_var('CUSTOMIZED_OSX_COMPILER'):import _osx_support_osx_support.customize_compiler(_config_vars)_config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'(cc, cxx, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \get_config_vars('CC', 'CXX', 'CFLAGS','CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS')if 'CC' in os.environ:newcc = os.environ['CC']if (sys.platform == 'darwin'and 'LDSHARED' not in os.environand ldshared.startswith(cc)):# On OS X, if CC is overridden, use that as the default# command for LDSHARED as wellldshared = newcc + ldshared[len(cc):]cc = newccif 'CXX' in os.environ:cxx = os.environ['CXX']if 'LDSHARED' in os.environ:ldshared = os.environ['LDSHARED']if 'CPP' in os.environ:cpp = os.environ['CPP']else:cpp = cc + " -E" # not alwaysif 'LDFLAGS' in os.environ:ldshared = ldshared + ' ' + os.environ['LDFLAGS']if 'CFLAGS' in os.environ:cflags = cflags + ' ' + os.environ['CFLAGS']ldshared = ldshared + ' ' + os.environ['CFLAGS']if 'CPPFLAGS' in os.environ:cpp = cpp + ' ' + os.environ['CPPFLAGS']cflags = cflags + ' ' + os.environ['CPPFLAGS']ldshared = ldshared + ' ' + os.environ['CPPFLAGS']if 'AR' in os.environ:ar = os.environ['AR']if 'ARFLAGS' in os.environ:archiver = ar + ' ' + os.environ['ARFLAGS']else:archiver = ar + ' ' + ar_flagscc_cmd = cc + ' ' + cflagscompiler.set_executables(preprocessor=cpp,compiler=cc_cmd,compiler_so=cc_cmd + ' ' + ccshared,compiler_cxx=cxx,linker_so=ldshared,linker_exe=cc,archiver=archiver)compiler.shared_lib_extension = shlib_suffixdef get_config_h_filename():"""Return full pathname of installed pyconfig.h file."""if python_build:if os.name == "nt":inc_dir = os.path.join(_sys_home or project_base, "PC")else:inc_dir = _sys_home or project_baseelse:inc_dir = get_python_inc(plat_specific=1)return os.path.join(inc_dir, 'pyconfig.h')def get_makefile_filename():"""Return full pathname of installed Makefile from the Python build."""if python_build:return os.path.join(_sys_home or project_base, "Makefile")lib_dir = get_python_lib(plat_specific=0, standard_lib=1)config_file = 'config-{}{}'.format(get_python_version(), build_flags)if hasattr(sys.implementation, '_multiarch'):config_file += '-%s' % sys.implementation._multiarchreturn os.path.join(lib_dir, config_file, 'Makefile')def parse_config_h(fp, g=None):"""Parse a config.h-style file.A dictionary containing name/value pairs is returned. If anoptional dictionary is passed in as the second argument, it isused instead of a new dictionary."""if g is None:g = {}define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")#while True:line = fp.readline()if not line:breakm = define_rx.match(line)if m:n, v = m.group(1, 2)try: v = int(v)except ValueError: passg[n] = velse:m = undef_rx.match(line)if m:g[m.group(1)] = 0return g# Regexes needed for parsing Makefile (and similar syntaxes,# like old-style Setup files)._variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")def parse_makefile(fn, g=None):"""Parse a Makefile-style file.A dictionary containing name/value pairs is returned. If anoptional dictionary is passed in as the second argument, it isused instead of a new dictionary."""from distutils.text_file import TextFilefp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")if g is None:g = {}done = {}notdone = {}while True:line = fp.readline()if line is None: # eofbreakm = _variable_rx.match(line)if m:n, v = m.group(1, 2)v = v.strip()# `$$' is a literal `$' in maketmpv = v.replace('$$', '')if "$" in tmpv:notdone[n] = velse:try:v = int(v)except ValueError:# insert literal `$'done[n] = v.replace('$$', '$')else:done[n] = v# Variables with a 'PY_' prefix in the makefile. These need to# be made available without that prefix through sysconfig.# Special care is needed to ensure that variable expansion works, even# if the expansion uses the name without a prefix.renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')# do variable interpolation herewhile notdone:for name in list(notdone):value = notdone[name]m = _findvar1_rx.search(value) or _findvar2_rx.search(value)if m:n = m.group(1)found = Trueif n in done:item = str(done[n])elif n in notdone:# get it on a subsequent roundfound = Falseelif n in os.environ:# do it like make: fall back to environmentitem = os.environ[n]elif n in renamed_variables:if name.startswith('PY_') and name[3:] in renamed_variables:item = ""elif 'PY_' + n in notdone:found = Falseelse:item = str(done['PY_' + n])else:done[n] = item = ""if found:after = value[m.end():]value = value[:m.start()] + item + afterif "$" in after:notdone[name] = valueelse:try: value = int(value)except ValueError:done[name] = value.strip()else:done[name] = valuedel notdone[name]if name.startswith('PY_') \and name[3:] in renamed_variables:name = name[3:]if name not in done:done[name] = valueelse:# bogus variable reference; just drop it since we can't dealdel notdone[name]fp.close()# strip spurious spacesfor k, v in done.items():if isinstance(v, str):done[k] = v.strip()# save the results in the global dictionaryg.update(done)return gdef expand_makefile_vars(s, vars):"""Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in'string' according to 'vars' (a dictionary mapping variable names tovalues). Variables not present in 'vars' are silently expanded to theempty string. The variable values in 'vars' should not contain furthervariable expansions; if 'vars' is the output of 'parse_makefile()',you're fine. Returns a variable-expanded version of 's'."""# This algorithm does multiple expansion, so if vars['foo'] contains# "${bar}", it will expand ${foo} to ${bar}, and then expand# ${bar}... and so forth. This is fine as long as 'vars' comes from# 'parse_makefile()', which takes care of such expansions eagerly,# according to make's variable expansion semantics.while True:m = _findvar1_rx.search(s) or _findvar2_rx.search(s)if m:(beg, end) = m.span()s = s[0:beg] + vars.get(m.group(1)) + s[end:]else:breakreturn s_config_vars = Nonedef _init_posix():"""Initialize the module as appropriate for POSIX systems."""# _sysconfigdata is generated at build time, see the sysconfig modulename = os.environ.get('_PYTHON_SYSCONFIGDATA_NAME','_sysconfigdata_{abi}_{platform}_{multiarch}'.format(abi=sys.abiflags,platform=sys.platform,multiarch=getattr(sys.implementation, '_multiarch', ''),))_temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)build_time_vars = _temp.build_time_varsglobal _config_vars_config_vars = {}_config_vars.update(build_time_vars)def _init_nt():"""Initialize the module as appropriate for NT"""g = {}# set basic install directoriesg['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)# XXX hmmm.. a normal install puts include files hereg['INCLUDEPY'] = get_python_inc(plat_specific=0)g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]g['EXE'] = ".exe"g['VERSION'] = get_python_version().replace(".", "")g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))global _config_vars_config_vars = gdef get_config_vars(*args):"""With no arguments, return a dictionary of all configurationvariables relevant for the current platform. Generally this includeseverything needed to build extensions and install both pure modules andextensions. On Unix, this means every variable defined in Python'sinstalled Makefile; on Windows it's a much smaller set.With arguments, return a list of values that result from looking upeach argument in the configuration variable dictionary."""global _config_varsif _config_vars is None:func = globals().get("_init_" + os.name)if func:func()else:_config_vars = {}# Normalized versions of prefix and exec_prefix are handy to have;# in fact, these are the standard versions used most places in the# Distutils._config_vars['prefix'] = PREFIX_config_vars['exec_prefix'] = EXEC_PREFIX# For backward compatibility, see issue19555SO = _config_vars.get('EXT_SUFFIX')if SO is not None:_config_vars['SO'] = SO# Always convert srcdir to an absolute pathsrcdir = _config_vars.get('srcdir', project_base)if os.name == 'posix':if python_build:# If srcdir is a relative path (typically '.' or '..')# then it should be interpreted relative to the directory# containing Makefile.base = os.path.dirname(get_makefile_filename())srcdir = os.path.join(base, srcdir)else:# srcdir is not meaningful since the installation is# spread about the filesystem. We choose the# directory containing the Makefile since we know it# exists.srcdir = os.path.dirname(get_makefile_filename())_config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir))# Convert srcdir into an absolute path if it appears necessary.# Normally it is relative to the build directory. However, during# testing, for example, we might be running a non-installed python# from a different directory.if python_build and os.name == "posix":base = project_baseif (not os.path.isabs(_config_vars['srcdir']) andbase != os.getcwd()):# srcdir is relative and we are not in the same directory# as the executable. Assume executable is in the build# directory and make srcdir absolute.srcdir = os.path.join(base, _config_vars['srcdir'])_config_vars['srcdir'] = os.path.normpath(srcdir)# OS X platforms require special customization to handle# multi-architecture, multi-os-version installersif sys.platform == 'darwin':import _osx_support_osx_support.customize_config_vars(_config_vars)if args:vals = []for name in args:vals.append(_config_vars.get(name))return valselse:return _config_varsdef get_config_var(name):"""Return the value of a single variable using the dictionaryreturned by 'get_config_vars()'. Equivalent toget_config_vars().get(name)"""if name == 'SO':import warningswarnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2)return get_config_vars().get(name)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。