我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用sys.__dict__()。
def finalize_options(self): self.set_undefined_options('install_lib', ('install_dir', 'install_dir')) self.set_undefined_options('install',('install_layout','install_layout')) if sys.hexversion > 0x2060000: self.set_undefined_options('install',('prefix_option','prefix_option')) ei_cmd = self.get_finalized_command("egg_info") basename = pkg_resources.Distribution( None, None, ei_cmd.egg_name, ei_cmd.egg_version ).egg_name() + '.egg-info' if self.install_layout: if not self.install_layout.lower() in ['deb']: raise DistutilsOptionError( "unknown value for --install-layout") basename = basename.replace('-py%s' % pkg_resources.PY_MAJOR, '') elif self.prefix_option or 'real_prefix' in sys.__dict__: # don't modify for virtualenv pass else: basename = basename.replace('-py%s' % pkg_resources.PY_MAJOR, '') self.source = ei_cmd.egg_info self.target = os.path.join(self.install_dir, basename) self.outputs = [self.target]
def finalize_options(self): self.set_undefined_options('install_lib', ('install_dir', 'install_dir')) self.set_undefined_options('install',('install_layout','install_layout')) if sys.hexversion > 0x2060000: self.set_undefined_options('install',('prefix_option','prefix_option')) ei_cmd = self.get_finalized_command("egg_info") basename = pkg_resources.Distribution( None, None, ei_cmd.egg_name, ei_cmd.egg_version ).egg_name() + '.egg-info' if self.install_layout: if not self.install_layout.lower() in ['deb']: raise DistutilsOptionError("unknown value for --install-layout") self.install_layout = self.install_layout.lower() basename = basename.replace('-py%s' % pkg_resources.PY_MAJOR, '') elif self.prefix_option or 'real_prefix' in sys.__dict__: # don't modify for virtualenv pass else: basename = basename.replace('-py%s' % pkg_resources.PY_MAJOR, '') self.source = ei_cmd.egg_info self.target = os.path.join(self.install_dir, basename) self.outputs = []
def warn(message, category=None, stacklevel=1): """Issue a warning, or maybe ignore it or raise an exception.""" # Check if message is already a Warning object if isinstance(message, Warning): category = message.__class__ # Check category argument if category is None: category = UserWarning assert issubclass(category, Warning) # Get context information try: caller = sys._getframe(stacklevel) except ValueError: globals = sys.__dict__ lineno = 1 else: globals = caller.f_globals lineno = caller.f_lineno if '__name__' in globals: module = globals['__name__'] else: module = "<string>" filename = globals.get('__file__') if filename: fnl = filename.lower() if fnl.endswith((".pyc", ".pyo")): filename = filename[:-1] else: if module == "__main__": try: filename = sys.argv[0] except AttributeError: # embedded interpreters don't have sys.argv, see bug #839151 filename = '__main__' if not filename: filename = module registry = globals.setdefault("__warningregistry__", {}) warn_explicit(message, category, filename, lineno, module, registry, globals)
def warn(msg, stacklevel=1, function=None): if function is not None: filename = py.std.inspect.getfile(function) lineno = py.code.getrawcode(function).co_firstlineno else: try: caller = sys._getframe(stacklevel) except ValueError: globals = sys.__dict__ lineno = 1 else: globals = caller.f_globals lineno = caller.f_lineno if '__name__' in globals: module = globals['__name__'] else: module = "<string>" filename = globals.get('__file__') if filename: fnl = filename.lower() if fnl.endswith(".pyc") or fnl.endswith(".pyo"): filename = filename[:-1] elif fnl.endswith("$py.class"): filename = filename.replace('$py.class', '.py') else: if module == "__main__": try: filename = sys.argv[0] except AttributeError: # embedded interpreters don't have sys.argv, see bug #839151 filename = '__main__' if not filename: filename = module path = py.path.local(filename) warning = DeprecationWarning(msg, path, lineno) py.std.warnings.warn_explicit(warning, category=Warning, filename=str(warning.path), lineno=warning.lineno, registry=py.std.warnings.__dict__.setdefault( "__warningsregistry__", {}) )
def warn(self, message, category=None, caller_id=0, severity=1, stacklevel=1): # 1 = serious # 2 = invalid data_def # 4 = invalid data with self.warn_lock: # Check if message is already a Warning object if isinstance(message, Warning): category = message.__class__ # Check category argument if category is None: category = UserWarning assert issubclass(category, Warning) # Get context information try: caller = sys._getframe(stacklevel) except ValueError: globals = sys.__dict__ lineno = 1 else: globals = caller.f_globals lineno = caller.f_lineno if '__name__' in globals: module = globals['__name__'] else: module = "<string>" filename = globals.get('__file__') if filename: fnl = filename.lower() if fnl.endswith((".pyc", ".pyo")): filename = filename[:-1] else: if module == "__main__": try: filename = sys.argv[0] except AttributeError: # embedded interpreters don't have sys.argv, see bug #839151 filename = '__main__' if not filename: filename = module registry = globals.setdefault("__warningregistry__", {}) self.warn_explicit(message, category, filename, lineno, caller_id, severity, module, registry, globals)
def fix_subprocess(override_debug=False, override_exception=False): """Activate the subprocess compatibility.""" import subprocess # Exceptions if subprocess.__dict__.get("SubprocessError") is None: subprocess.SubprocessError = _Internal.SubprocessError if _InternalReferences.UsedCalledProcessError is None: if "CalledProcessError" in subprocess.__dict__: _subprocess_called_process_error(True, subprocess) else: _subprocess_called_process_error(False, subprocess) subprocess.CalledProcessError = _InternalReferences.UsedCalledProcessError def _check_output(*args, **kwargs): if "stdout" in kwargs: raise ValueError("stdout argument not allowed, " "it will be overridden.") process = subprocess.Popen(stdout=subprocess.PIPE, *args, **kwargs) stdout_data, __ = process.communicate() ret_code = process.poll() if ret_code is None: raise RuntimeWarning("The process is not yet terminated.") if ret_code: cmd = kwargs.get("args") if cmd is None: cmd = args[0] raise _InternalReferences.UsedCalledProcessError(returncode=ret_code, cmd=cmd, output=stdout_data) return stdout_data try: subprocess.check_output except AttributeError: subprocess.check_output = _check_output
def _expand(self, *attrs): config_vars = self.get_finalized_command('install').config_vars if self.prefix or self.install_layout: if self.install_layout and self.install_layout.lower() in ['deb']: scheme_name = "deb_system" self.prefix = '/usr' elif self.prefix or 'real_prefix' in sys.__dict__: scheme_name = os.name else: scheme_name = "posix_local" # Set default install_dir/scripts from --prefix config_vars = config_vars.copy() config_vars['base'] = self.prefix scheme = self.INSTALL_SCHEMES.get(scheme_name,self.DEFAULT_SCHEME) for attr, val in scheme.items(): if getattr(self, attr, None) is None: setattr(self, attr, val) from distutils.util import subst_vars for attr in attrs: val = getattr(self, attr) if val is not None: val = subst_vars(val, config_vars) if os.name == 'posix': val = os.path.expanduser(val) setattr(self, attr, val)