Beispiel #1
0
    def method_kill(self, space, w_signal, args_w):
        if not args_w:
            raise space.error(space.w_ArgumentError,
                "wrong number of arguments (%d for at least 2)" % (len(args_w) + 1)
            )
        if space.is_kind_of(w_signal, space.w_fixnum):
            sig = space.int_w(w_signal)
        else:
            s = Coerce.str(space, w_signal)
            if s.startswith("SIG"):
                s = s[len("SIG"):]
            try:
                sig = SIGNALS[s]
            except KeyError:
                raise space.error(space.w_ArgumentError,
                    "unsupported name `SIG%s'" % s
                )

        if sig < 0:
            for w_arg in args_w:
                pid = Coerce.int(space, w_arg)
                try:
                    killpg(pid, -sig)
                except OSError as e:
                    raise error_for_oserror(space, e)
        else:
            for w_arg in args_w:
                pid = Coerce.int(space, w_arg)
                try:
                    kill(pid, sig)
                except OSError as e:
                    raise error_for_oserror(space, e)
        return space.newint(len(args_w))
Beispiel #2
0
    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)
Beispiel #3
0
 def method_read(self, space, w_length=None, w_str=None):
     self.ensure_not_closed(space)
     if w_length:
         length = space.int_w(w_length)
         if length < 0:
             raise space.error(space.w_ArgumentError,
                 "negative length %d given" % length
             )
     else:
         length = -1
     read_bytes = 0
     read_chunks = []
     while length < 0 or read_bytes < length:
         if length > 0:
             max_read = int(length - read_bytes)
         else:
             max_read = 8192
         try:
             current_read = os.read(self.fd, max_read)
         except OSError as e:
             raise error_for_oserror(space, e)
         if len(current_read) == 0:
             break
         read_bytes += len(current_read)
         read_chunks += current_read
     # Return nil on EOF if length is given
     if read_bytes == 0:
         return space.w_nil
     w_read_str = space.newstr_fromchars(read_chunks)
     if w_str is not None:
         w_str.clear(space)
         w_str.extend(space, w_read_str)
         return w_str
     else:
         return w_read_str
Beispiel #4
0
 def method_initialize(self, space, path):
     self.path = path
     try:
         self.dirp = opendir(path)
     except OSError as e:
         raise error_for_oserror(space, e)
     self.open = True
Beispiel #5
0
 def method_truncate(self, space, length):
     self.ensure_not_closed(space)
     try:
         ftruncate(self.fd, length)
     except OSError as e:
         raise error_for_oserror(space, e)
     return space.newint(0)
Beispiel #6
0
 def method_write(self, space, w_str):
     self.ensure_not_closed(space)
     string = space.str_w(space.send(w_str, "to_s"))
     try:
         bytes_written = os.write(self.fd, string)
     except OSError as e:
         raise error_for_oserror(space, e)
     return space.newint(bytes_written)
Beispiel #7
0
 def method_stat(self, space):
     try:
         stat_val = os.fstat(self.fd)
     except OSError as e:
         raise error_for_oserror(space, e)
     stat_obj = W_FileStatObject(space)
     stat_obj.set_stat(stat_val)
     return stat_obj
Beispiel #8
0
 def singleton_method_chmod(self, space, mode, args_w):
     for arg_w in args_w:
         path = Coerce.path(space, arg_w)
         try:
             os.chmod(path, mode)
         except OSError as e:
             raise error_for_oserror(space, e)
     return space.newint(len(args_w))
Beispiel #9
0
 def singleton_method_lstat(self, space, filename):
     try:
         stat_val = os.lstat(filename)
     except OSError as e:
         raise error_for_oserror(space, e)
     stat_obj = W_FileStatObject(space)
     stat_obj.set_stat(stat_val)
     return stat_obj
Beispiel #10
0
 def singleton_method_delete(self, space, args_w):
     for w_path in args_w:
         path = Coerce.path(space, w_path)
         try:
             os.unlink(path)
         except OSError as e:
             raise error_for_oserror(space, e)
     return space.newint(len(args_w))
Beispiel #11
0
 def method_getc(self, space):
     self.ensure_not_closed(space)
     try:
         c = os.read(self.fd, 1)
     except OSError as e:
         raise error_for_oserror(space, e)
     if not c:
         return space.w_nil
     return space.newstr_fromstr(c)
Beispiel #12
0
 def method_chdir(self, space, path=None, block=None):
     if path is None:
         path = os.environ["HOME"]
     current_dir = os.getcwd()
     try:
         os.chdir(path)
     except OSError as e:
         raise error_for_oserror(space, e)
     if block is not None:
         try:
             return space.invoke_block(block, [space.newstr_fromstr(path)])
         finally:
             try:
                 os.chdir(current_dir)
             except OSError as e:
                 raise error_for_oserror(space, e)
     else:
         return space.newint(0)
Beispiel #13
0
 def method_clock_gettime(self, space, clockid, args_w):
     if len(args_w) > 1:
         raise space.error(space.w_ArgumentError,
             "wrong number of arguments (given %d, expected 1..2)"
         )
     if len(args_w) == 1:
         unit = Coerce.symbol(space, args_w[0])
     else:
         unit = "float_second"
     if rtime.HAS_CLOCK_GETTIME:
         with lltype.scoped_alloc(rtime.TIMESPEC) as a:
             if rtime.c_clock_gettime(clockid, a) == 0:
                 sec = rffi.getintfield(a, 'c_tv_sec')
                 nsec = rffi.getintfield(a, 'c_tv_nsec')
             else:
                 raise error_for_oserror(space, OSError(
                     errno.EINVAL, "clock_gettime")
                 )
     elif clockid == CLOCK_PROCESS_CPUTIME_ID:
         r = rtime.clock()
         sec = int(r)
         nsec = r * 1000000000
     else:
         raise error_for_oserror(space, OSError(
             errno.EINVAL, "clock_gettime")
         )
     if unit == "float_second":
         return space.newfloat(sec + nsec * 0.000000001)
     elif unit == "float_millisecond":
         return space.newfloat(sec * 1000 + nsec * 0.000001)
     elif unit == "float_microsecond":
         return space.newfloat(sec * 1000000 + nsec * 0.001)
     elif unit == "second":
         return space.newint(int(sec))
     elif unit == "millisecond":
         return space.newint(int(sec) * 1000)
     elif unit == "microsecond":
         return space.newint(sec * 1000000)
     elif unit == "nanosecond":
         return space.newint(sec * 1000000000 + int(nsec))
     else:
         raise space.error(space.w_ArgumentError,
             "unexpected unit: %s" % unit
         )
Beispiel #14
0
 def method_read(self, space, args_w):
     self.ensure_open(space)
     try:
         filename = readdir(self.dirp)
     except OSError as e:
         raise error_for_oserror(space, e)
     if filename is None:
         return space.w_nil
     else:
         return space.newstr_fromstr(filename)
Beispiel #15
0
 def method_waitpid(self, space, pid=-1):
     try:
         pid, status = os.waitpid(pid, 0)
     except OSError as e:
         raise error_for_oserror(space, e)
     status = WEXITSTATUS(status)
     w_status = space.send(
         space.find_const(self, "Status"),
         "new",
         [space.newint(pid), space.newint(status)]
     )
     space.globals.set(space, "$?", w_status)
     return space.newint(pid)
Beispiel #16
0
    def load_feature(space, path, orig_path):
        if not os.path.exists(path):
            raise space.error(space.w_LoadError, orig_path)

        try:
            f = open_file_as_stream(path, buffering=0)
            try:
                contents = f.readall()
            finally:
                f.close()
        except OSError as e:
            raise error_for_oserror(space, e)

        space.execute(contents, filepath=path)
Beispiel #17
0
 def method_sysopen(self, space, w_path, w_mode_str_or_int=None, w_perm=None):
     perm = 0666
     mode = os.O_RDONLY
     if w_mode_str_or_int is not None:
         mode = map_filemode(space, w_mode_str_or_int)
     if w_perm is not None and w_perm is not space.w_nil:
         perm = space.int_w(w_perm)
     path = Coerce.path(space, w_path)
     try:
         fd = os.open(path, mode, perm)
     except OSError as e:
         raise error_for_oserror(space, e)
     else:
         return space.newint(fd)
Beispiel #18
0
 def method_tcgetattr(self, space, fd):
     try:
         mode = tcgetattr(fd)
     except OSError as e:
         raise error_for_oserror(space, e)
     mode_w = [
         space.newint(mode[0]),  # iflag
         space.newint(mode[1]),  # oflag
         space.newint(mode[2]),  # cflag
         space.newint(mode[3]),  # lflag
         space.newint(mode[4]),  # ispeed
         space.newint(mode[5]),  # ospeed
         space.newarray([space.newstr_fromstr(cc) for cc in mode[6]])
     ]
     return space.newarray(mode_w)
Beispiel #19
0
 def method_tcsetattr(self, space, fd, when, mode_w):
     cc = [space.str_w(w_char) for w_char in space.listview(mode_w[6])]
     mode = (
         space.int_w(mode_w[0]),  # iflag
         space.int_w(mode_w[1]),  # oflag
         space.int_w(mode_w[2]),  # cflag
         space.int_w(mode_w[3]),  # lflag
         space.int_w(mode_w[4]),  # ispeed
         space.int_w(mode_w[5]),  # ospeed
         cc
     )
     try:
         tcsetattr(fd, when, mode)
     except OSError as e:
         raise error_for_oserror(space, e)
     return self
Beispiel #20
0
    def load_feature(space, path, orig_path, wrap=False):
        if not os.path.exists(path):
            raise space.error(space.w_LoadError, orig_path)

        try:
            f = open_file_as_stream(path, buffering=0)
            try:
                contents = f.readall()
            finally:
                f.close()
        except OSError as e:
            raise error_for_oserror(space, e)

        if wrap:
            lexical_scope = StaticScope(space.newmodule("Anonymous"), None)
        else:
            lexical_scope = None
        space.execute(contents, filepath=path, lexical_scope=lexical_scope)
Beispiel #21
0
 def method_initialize(self, space, filename, w_mode=None, w_perm_or_opt=None, w_opt=None):
     if w_mode is None:
         w_mode = space.w_nil
     if w_perm_or_opt is None:
         w_perm_or_opt = space.w_nil
     if w_opt is None:
         w_opt = space.w_nil
     if isinstance(w_perm_or_opt, W_HashObject):
         assert w_opt is space.w_nil
         perm = 0665
         w_opt = w_perm_or_opt
     elif w_opt is not space.w_nil:
         perm = space.int_w(w_perm_or_opt)
     else:
         perm = 0665
     mode = map_filemode(space, w_mode)
     if w_perm_or_opt is not space.w_nil or w_opt is not space.w_nil:
         raise space.error(space.w_NotImplementedError, "options hash or permissions for File.new")
     try:
         self.fd = os.open(filename, mode, perm)
     except OSError as e:
         raise error_for_oserror(space, e)
     return self
Beispiel #22
0
 def method_delete(self, space, path):
     try:
         os.rmdir(path if path else "")
     except OSError as e:
         raise error_for_oserror(space, e)
     return space.newint(0)
 def method_ctime(self, space):
     try:
         stat_val = os.stat(self.filename)
     except OSError as e:
         raise error_for_oserror(space, e)
     return self._time_at(space, stat_val.st_ctime)
Beispiel #24
0
 def method_chmod(self, space, mode):
     try:
         fchmod(self.fd, mode)
     except OSError as e:
         raise error_for_oserror(space, e)
     return space.newint(0)
Beispiel #25
0
 def method_ctime(self, space):
     try:
         stat_val = os.stat(self.filename)
     except OSError as e:
         raise error_for_oserror(space, e)
     return self._time_at(space, stat_val.st_ctime)
Beispiel #26
0
 def method_chmod(self, space, mode):
     try:
         fchmod(self.fd, mode)
     except OSError as e:
         raise error_for_oserror(space, e)
     return space.newint(0)
Beispiel #27
0
 def method_entries(self, space, dirname):
     try:
         return space.newarray([space.newstr_fromstr(d) for d in os.listdir(dirname)])
     except OSError as e:
         raise error_for_oserror(space, e)
Beispiel #28
0
 def method_entries(self, space, dirname):
     try:
         return space.newarray([space.newstr_fromstr(d) for d in os.listdir(dirname)])
     except OSError as e:
         raise error_for_oserror(space, e)
Beispiel #29
0
 def method_initialize(self, space, filename):
     try:
         self.set_stat(os.stat(filename))
     except OSError as e:
         raise error_for_oserror(space, e)
Beispiel #30
0
    def method_initialize(self, space, filename, w_mode=None, w_perm_or_opt=None, w_opt=None):
        if w_mode is None:
            w_mode = space.w_nil
        if w_perm_or_opt is None:
            w_perm_or_opt = space.w_nil
        if w_opt is None:
            w_opt = space.w_nil
        if isinstance(w_perm_or_opt, W_HashObject):
            assert w_opt is space.w_nil
            perm = 0665
            w_opt = w_perm_or_opt
        elif w_opt is not space.w_nil:
            perm = space.int_w(w_perm_or_opt)
        else:
            perm = 0665
        if w_mode is space.w_nil:
            mode = os.O_RDONLY
        elif isinstance(w_mode, W_StringObject):
            mode_str = space.str_w(w_mode)
            mode = 0
            invalid_error = space.error(space.w_ArgumentError,
                "invalid access mode %s" % mode_str
            )
            major_mode_seen = False
            readable = writeable = append = False

            for ch in mode_str:
                if ch == "b":
                    mode |= O_BINARY
                elif ch == "+":
                    readable = writeable = True
                elif ch == "r":
                    if major_mode_seen:
                        raise invalid_error
                    major_mode_seen = True
                    readable = True
                elif ch == "a":
                    if major_mode_seen:
                        raise invalid_error
                    major_mode_seen = True
                    mode |= os.O_CREAT
                    append = writeable = True
                elif ch == "w":
                    if major_mode_seen:
                        raise invalid_error
                    major_mode_seen = True
                    mode |= os.O_TRUNC | os.O_CREAT
                    writeable = True
                else:
                    raise invalid_error
            if readable and writeable:
                mode |= os.O_RDWR
            elif readable:
                mode |= os.O_RDONLY
            elif writeable:
                mode |= os.O_WRONLY
            if append:
                mode |= os.O_APPEND
        else:
            mode = space.int_w(w_mode)
        if w_perm_or_opt is not space.w_nil or w_opt is not space.w_nil:
            raise NotImplementedError("options hash or permissions for File.new")
        try:
            self.fd = os.open(filename, mode, perm)
        except OSError as e:
            raise error_for_oserror(space, e)
        return self
Beispiel #31
0
 def method_mkdir(self, space, path, mode=0777):
     try:
         os.mkdir(path, mode)
     except OSError as e:
         raise error_for_oserror(space, e)
     return space.newint(0)
Beispiel #32
0
 def singleton_method_link(self, space, old_name, new_name):
     try:
         os.link(old_name, new_name)
     except OSError as e:
         raise error_for_oserror(space, e)
     return space.newint(0)
Beispiel #33
0
 def method_initialize(self, space, filename):
     try:
         self.set_stat(os.stat(filename))
     except OSError as e:
         raise error_for_oserror(space, e)
Beispiel #34
0
 def method_delete(self, space, path):
     try:
         os.rmdir(path if path else "")
     except OSError as e:
         raise error_for_oserror(space, e)
     return space.newint(0)
Beispiel #35
0
 def singleton_method_link(self, space, old_name, new_name):
     try:
         os.link(old_name, new_name)
     except OSError as e:
         raise error_for_oserror(space, e)
     return space.newint(0)
Beispiel #36
0
 def method_mkdir(self, space, path, mode=0777):
     try:
         os.mkdir(path, mode)
     except OSError as e:
         raise error_for_oserror(space, e)
     return space.newint(0)