def method_exec(self, space, args_w): if len(args_w) > 1 and space.respond_to(args_w[0], "to_hash"): raise space.error(space.w_NotImplementedError, "exec with environment") if len(args_w) > 1 and space.respond_to(args_w[-1], "to_hash"): raise space.error(space.w_NotImplementedError, "exec with options") if space.respond_to(args_w[0], "to_ary"): w_cmd = space.convert_type(args_w[0], space.w_array, "to_ary") cmd_w = space.listview(w_cmd) if len(cmd_w) != 2: raise space.error(space.w_ArgumentError, "wrong first argument") cmd, argv0 = [ space.str0_w(space.convert_type(w_e, space.w_string, "to_str")) for w_e in cmd_w ] else: w_cmd = space.convert_type(args_w[0], space.w_string, "to_str") cmd = space.str0_w(w_cmd) argv0 = None if len(args_w) > 1 or argv0 is not None: if argv0 is None: sepidx = cmd.rfind(os.sep) + 1 if sepidx > 0: argv0 = cmd[sepidx:] else: argv0 = cmd args = [argv0] args += [ space.str0_w( space.convert_type(w_arg, space.w_string, "to_str")) for w_arg in args_w[1:] ] try: os.execv(cmd, args) except OSError as e: raise error_for_oserror(space, e) else: if not cmd: raise error_for_errno(space, errno.ENOENT) shell = os.environ.get("RUBYSHELL") or os.environ.get( "COMSPEC") or "/bin/sh" sepidx = shell.rfind(os.sep) + 1 if sepidx > 0: argv0 = shell[sepidx:] else: argv0 = shell try: os.execv(shell, [argv0, "-c", cmd]) except OSError as e: raise error_for_oserror(space, e)
def method_exec(self, space, args_w): if len(args_w) > 1 and space.respond_to(args_w[0], "to_hash"): raise space.error(space.w_NotImplementedError, "exec with environment") if len(args_w) > 1 and space.respond_to(args_w[-1], "to_hash"): raise space.error(space.w_NotImplementedError, "exec with options") if space.respond_to(args_w[0], "to_ary"): w_cmd = space.convert_type(args_w[0], space.w_array, "to_ary") cmd_w = space.listview(w_cmd) if len(cmd_w) != 2: raise space.error(space.w_ArgumentError, "wrong first argument") cmd, argv0 = [ space.str0_w(space.convert_type( w_e, space.w_string, "to_str" )) for w_e in cmd_w ] else: w_cmd = space.convert_type(args_w[0], space.w_string, "to_str") cmd = space.str0_w(w_cmd) argv0 = None if len(args_w) > 1 or argv0 is not None: if argv0 is None: sepidx = cmd.rfind(os.sep) + 1 if sepidx > 0: argv0 = cmd[sepidx:] else: argv0 = cmd args = [argv0] args += [ space.str0_w(space.convert_type( w_arg, space.w_string, "to_str" )) for w_arg in args_w[1:] ] try: os.execv(cmd, args) except OSError as e: raise error_for_oserror(space, e) else: if not cmd: raise error_for_errno(space, errno.ENOENT) shell = os.environ.get("RUBYSHELL") or os.environ.get("COMSPEC") or "/bin/sh" sepidx = shell.rfind(os.sep) + 1 if sepidx > 0: argv0 = shell[sepidx:] else: argv0 = shell try: os.execv(shell, [argv0, "-c", cmd]) except OSError as e: raise error_for_oserror(space, e)
def method_subscript_assign(self, space, key, w_value): if "\0" in key: raise space.error(space.w_ArgumentError, "bad environment variable name") if w_value is space.w_nil: try: del os.environ[key] except (KeyError, OSError): pass return space.w_nil if "=" in key or key == "": raise error_for_errno(space, errno.EINVAL) value = Coerce.str(space, w_value) if "\0" in value: raise space.error(space.w_ArgumentError, "bad environment variable value") os.environ[key] = value return w_value