我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用os.exec()。
def test_exec(self): '''Test the `exec` example.''' filename = 'exec-{}.py' if six.PY2: filename = filename.format('py2') expect = { 'SEVERITY': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 2, 'HIGH': 0}, 'CONFIDENCE': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 0, 'HIGH': 2} } else: filename = filename.format('py3') expect = { 'SEVERITY': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 1, 'HIGH': 0}, 'CONFIDENCE': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 0, 'HIGH': 1} } self.check_example(filename, expect)
def test_exec_as_root(self): '''Test for the `run_as_root=True` keyword argument.''' expect = { 'SEVERITY': {'UNDEFINED': 0, 'LOW': 5, 'MEDIUM': 0, 'HIGH': 0}, 'CONFIDENCE': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 5, 'HIGH': 0} } self.check_example('exec-as-root.py', expect)
def test_os_exec(self): '''Test for `os.exec*`.''' expect = { 'SEVERITY': {'UNDEFINED': 0, 'LOW': 8, 'MEDIUM': 0, 'HIGH': 0}, 'CONFIDENCE': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 8, 'HIGH': 0} } self.check_example('os-exec.py', expect)
def encode_to_py3bytes_or_py2str(s): """ takes anything and attempts to return a py2 string or py3 bytes. this is typically used when creating command + arguments to be executed via os.exec* """ fallback_encoding = "utf8" if IS_PY3: # if we're already bytes, do nothing if isinstance(s, bytes): pass else: s = str(s) try: s = bytes(s, DEFAULT_ENCODING) except UnicodeEncodeError: s = bytes(s, fallback_encoding) else: # attempt to convert the thing to unicode from the system's encoding try: s = unicode(s, DEFAULT_ENCODING) # if the thing is already unicode, or it's a number, it can't be # coerced to unicode with an encoding argument, but if we leave out # the encoding argument, it will convert it to a string, then to unicode except TypeError: s = unicode(s) # now that we have guaranteed unicode, encode to our system encoding, # but attempt to fall back to something try: s = s.encode(DEFAULT_ENCODING) except: s = s.encode(fallback_encoding, "replace") return s
def compile_args(args, kwargs, sep, prefix): """ takes args and kwargs, as they were passed into the command instance being executed with __call__, and compose them into a flat list that will eventually be fed into exec. example: with this call: sh.ls("-l", "/tmp", color="never") this function receives args = ['-l', '/tmp'] kwargs = {'color': 'never'} and produces ['-l', '/tmp', '--color=never'] """ processed_args = [] encode = encode_to_py3bytes_or_py2str # aggregate positional args for arg in args: if isinstance(arg, (list, tuple)): if isinstance(arg, GlobResults) and not arg: arg = [arg.path] for sub_arg in arg: processed_args.append(encode(sub_arg)) elif isinstance(arg, dict): processed_args += aggregate_keywords(arg, sep, prefix, raw=True) else: processed_args.append(encode(arg)) # aggregate the keyword arguments processed_args += aggregate_keywords(kwargs, sep, prefix) return processed_args
def run_repl(env): # pragma: no cover banner = "\n>> sh v{version}\n>> https://github.com/amoffat/sh\n" print(banner.format(version=__version__)) while True: try: line = raw_input("sh> ") except (ValueError, EOFError): break try: exec(compile(line, "<dummy>", "single"), env, env) except SystemExit: break except: print(traceback.format_exc()) # cleans up our last line print("") # this is a thin wrapper around THIS module (we patch sys.modules[__name__]). # this is in the case that the user does a "from sh import whatever" # in other words, they only want to import certain programs, not the whole # system PATH worth of commands. in this case, we just proxy the # import lookup to our Environment class