Example #1
0
 def readdir(dirp):
     rposix.set_saved_errno(0)
     direntp = os_readdir(dirp)
     if not direntp:
         if rposix.get_saved_errno() == 0:
             return None
         else:
             raise OSError(rposix.get_saved_errno(), "error in readdir")
     namep = rffi.cast(rffi.CCHARP, direntp.c_d_name)
     return rffi.charp2str(namep)
Example #2
0
def exception_from_saved_errno(space, w_type):
    from rpython.rlib.rposix import get_saved_errno

    errno = get_saved_errno()
    msg = os.strerror(errno)
    w_error = space.call_function(w_type, space.wrap(errno), space.wrap(msg))
    return OperationError(w_type, w_error)
Example #3
0
 def startup(self):
     if not OPROFILE_AVAILABLE:
         return
     agent = op_open_agent()
     if not agent:
         raise OProfileError(rposix.get_saved_errno(), "startup")
     self.agent = agent
Example #4
0
 def enable(self, space, fileno, period_usec):
     if self.is_enabled:
         raise oefmt(space.w_ValueError, "_vmprof already enabled")
     self.fileno = fileno
     self.is_enabled = True
     self.write_header(fileno, period_usec)
     if not self.ever_enabled:
         if we_are_translated():
             res = pypy_vmprof_init()
             if res:
                 raise OperationError(
                     space.w_IOError,
                     space.wrap(rffi.charp2str(vmprof_get_error())))
         self.ever_enabled = True
     self.gather_all_code_objs(space)
     space.register_code_callback(vmprof_register_code)
     if we_are_translated():
         # does not work untranslated
         res = vmprof_enable(fileno, period_usec, 0,
                             lltype.nullptr(rffi.CCHARP.TO), 0)
     else:
         res = 0
     if res == -1:
         raise wrap_oserror(space, OSError(rposix.get_saved_errno(),
                                           "_vmprof.enable"))
Example #5
0
    def close(self):
        """Closes the described file.

        Attention! Unlike Python semantics, `close' does not return `None' upon
        success but `0', to be able to return an exit code for popen'ed files.

        The actual return value may be determined with os.WEXITSTATUS.
        """
        res = 0
        ll_file = self._ll_file
        if ll_file:
            # double close is allowed
            self._ll_file = lltype.nullptr(FILEP.TO)
            do_close = self._close2[0]
            try:
                if do_close:
                    res = do_close(ll_file)
                    if res == -1:
                        errno = rposix.get_saved_errno()
                        raise IOError(errno, os.strerror(errno))
            finally:
                if self._setbuf:
                    lltype.free(self._setbuf, flavor='raw')
                    self._setbuf = lltype.nullptr(rffi.CCHARP.TO)
        return res
Example #6
0
    def bindtextdomain(space, domain, w_dir):
        """bindtextdomain(domain, dir) -> string
        Bind the C library's domain to dir."""

        if space.is_w(w_dir, space.w_None):
            dir = None
            domain_c = rffi.str2charp(domain)
            try:
                dirname = _bindtextdomain(domain_c, dir)
            finally:
                rffi.free_charp(domain_c)
        else:
            dir = space.str_w(w_dir)
            domain_c = rffi.str2charp(domain)
            dir_c = rffi.str2charp(dir)
            try:
                dirname = _bindtextdomain(domain_c, dir_c)
            finally:
                rffi.free_charp(domain_c)
                rffi.free_charp(dir_c)

        if not dirname:
            errno = rposix.get_saved_errno()
            raise OperationError(space.w_OSError, space.wrap(errno))
        return space.wrap(rffi.charp2str(dirname))
Example #7
0
    def alloc(map_size):
        """Allocate memory.  This is intended to be used by the JIT,
        so the memory has the executable bit set and gets allocated
        internally in case of a sandboxed process.
        """
        from errno import ENOMEM
        from rpython.rlib import debug

        if _CYGWIN:
            # XXX: JIT memory should be using mmap MAP_PRIVATE with
            #      PROT_EXEC but Cygwin's fork() fails.  mprotect()
            #      cannot be used, but seems to be unnecessary there.
            res = c_malloc_safe(map_size)
            if res == rffi.cast(PTR, 0):
                raise MemoryError
            return res
        res = alloc_hinted(rffi.cast(PTR, hint.pos), map_size)
        if res == rffi.cast(PTR, -1):
            # some systems (some versions of OS/X?) complain if they
            # are passed a non-zero address.  Try again.
            res = alloc_hinted(rffi.cast(PTR, 0), map_size)
            if res == rffi.cast(PTR, -1):
                # ENOMEM simply raises MemoryError, but other errors are fatal
                if rposix.get_saved_errno() != ENOMEM:
                    debug.fatalerror_notb(
                        "Got an unexpected error trying to allocate some "
                        "memory for the JIT (tried to do mmap() with "
                        "PROT_EXEC|PROT_READ|PROT_WRITE).  This can be caused "
                        "by a system policy like PAX.  You need to find how "
                        "to work around the policy on your system.")
                raise MemoryError
        else:
            hint.pos += map_size
        return res
Example #8
0
 def seek(self, pos, whence=0):
     self._check_closed()
     res = c_fseek(self._ll_file, pos, whence)
     if res == -1:
         errno = rposix.get_saved_errno()
         raise IOError(errno, os.strerror(errno))
     self._skipnextlf = False
Example #9
0
def tcsetattr(fd, when, attributes):
    with lltype.scoped_alloc(TERMIOSP.TO) as c_struct:
        rffi.setintfield(c_struct, "c_c_iflag", attributes[0])
        rffi.setintfield(c_struct, "c_c_oflag", attributes[1])
        rffi.setintfield(c_struct, "c_c_cflag", attributes[2])
        rffi.setintfield(c_struct, "c_c_lflag", attributes[3])
        ispeed = attributes[4]
        ospeed = attributes[5]
        cc = attributes[6]
        for i in range(NCCS):
            c_struct.c_c_cc[i] = rffi.r_uchar(ord(cc[i][0]))
        if c_cfsetispeed(c_struct, ispeed) < 0:
            raise OSError(rposix.get_saved_errno(), "tcsetattr failed")
        if c_cfsetospeed(c_struct, ospeed) < 0:
            raise OSError(rposix.get_saved_errno(), "tcsetattr failed")
        if c_tcsetattr(fd, when, c_struct) < 0:
            raise OSError(rposix.get_saved_errno(), "tcsetattr failed")
Example #10
0
 def f(n, x, *args):
     rposix.set_saved_errno(24)
     result1 = c_test()
     result2 = rposix.get_saved_errno()
     assert result1 == 24
     assert result2 == 42
     n -= 1
     return (n, x) + args
Example #11
0
 def native_code_written(self, name, address, size):
     assert size > 0
     if not OPROFILE_AVAILABLE:
         return
     uaddress = rffi.cast(rffi.ULONG, address)
     success = op_write_native_code(self.agent, name, uaddress, rffi.cast(rffi.VOIDP, 0), size)
     if success != 0:
         raise OProfileError(rposix.get_saved_errno(), "write")
Example #12
0
 def truncate(self, arg=-1):
     self._check_closed()
     if arg == -1:
         arg = self.tell()
     self.flush()
     res = c_ftruncate(self.fileno(), arg)
     if res == -1:
         errno = rposix.get_saved_errno()
         raise IOError(errno, os.strerror(errno))
Example #13
0
 def truncate(self, arg=-1):
     self._check_closed()
     if arg == -1:
         arg = self.tell()
     self.flush()
     res = c_ftruncate(self.fileno(), arg)
     if res == -1:
         errno = rposix.get_saved_errno()
         raise IOError(errno, os.strerror(errno))
Example #14
0
 def sem_getvalue(sem):
     sval_ptr = lltype.malloc(rffi.INTP.TO, 1, flavor='raw')
     try:
         res = _sem_getvalue(sem, sval_ptr)
         if res < 0:
             raise OSError(rposix.get_saved_errno(), "sem_getvalue failed")
         return rffi.cast(lltype.Signed, sval_ptr[0])
     finally:
         lltype.free(sval_ptr, flavor='raw')
Example #15
0
def sh_cmd(vm):
    (cmd_o,),_ = vm.decode_args("S")
    assert isinstance(cmd_o, Con_String)

    r = system(cmd_o.v)
    if r == -1:
        vm.raise_helper("Exception", [Con_String(vm, os.strerror(rposix.get_saved_errno()))])

    return Con_Int(vm, os.WEXITSTATUS(r))
Example #16
0
def ll_math_pow(x, y):
    # deal directly with IEEE specials, to cope with problems on various
    # platforms whose semantics don't exactly match C99

    if math.isnan(y):
        if x == 1.0:
            return 1.0  # 1**Nan = 1
        return y

    if not isfinite(x):
        if math.isnan(x):
            if y == 0.0:
                return 1.0  # NaN**0 = 1
            return x
        else:  # isinf(x)
            odd_y = not math.isinf(y) and math_fmod(math_fabs(y), 2.0) == 1.0
            if y > 0.0:
                if odd_y:
                    return x
                return math_fabs(x)
            elif y == 0.0:
                return 1.0
            else:  # y < 0.0
                if odd_y:
                    return math_copysign(0.0, x)
                return 0.0

    if math.isinf(y):
        if math_fabs(x) == 1.0:
            return 1.0
        elif y > 0.0 and math_fabs(x) > 1.0:
            return y
        elif y < 0.0 and math_fabs(x) < 1.0:
            if x == 0.0:
                raise ValueError("0**-inf: divide by zero")
            return -y  # result is +inf
        else:
            return 0.0

    r = math_pow(x, y)
    errno = rposix.get_saved_errno()
    if not isfinite(r):
        if math.isnan(r):
            # a NaN result should arise only from (-ve)**(finite non-integer)
            errno = EDOM
        else:  # isinf(r)
            # an infinite result here arises either from:
            # (A) (+/-0.)**negative (-> divide-by-zero)
            # (B) overflow of x**y with x and y finite
            if x == 0.0:
                errno = EDOM
            else:
                errno = ERANGE
    if errno:
        _likely_raise(errno, r)
    return r
Example #17
0
def ll_math_pow(x, y):
    # deal directly with IEEE specials, to cope with problems on various
    # platforms whose semantics don't exactly match C99

    if isnan(y):
        if x == 1.0:
            return 1.0   # 1**Nan = 1
        return y

    if not isfinite(x):
        if isnan(x):
            if y == 0.0:
                return 1.0   # NaN**0 = 1
            return x
        else:   # isinf(x)
            odd_y = not isinf(y) and math_fmod(math_fabs(y), 2.0) == 1.0
            if y > 0.0:
                if odd_y:
                    return x
                return math_fabs(x)
            elif y == 0.0:
                return 1.0
            else:   # y < 0.0
                if odd_y:
                    return math_copysign(0.0, x)
                return 0.0

    if isinf(y):
        if math_fabs(x) == 1.0:
            return 1.0
        elif y > 0.0 and math_fabs(x) > 1.0:
            return y
        elif y < 0.0 and math_fabs(x) < 1.0:
            if x == 0.0:
                raise ValueError("0**-inf: divide by zero")
            return -y    # result is +inf
        else:
            return 0.0

    r = math_pow(x, y)
    errno = rposix.get_saved_errno()
    if not isfinite(r):
        if isnan(r):
            # a NaN result should arise only from (-ve)**(finite non-integer)
            errno = EDOM
        else:   # isinf(r)
            # an infinite result here arises either from:
            # (A) (+/-0.)**negative (-> divide-by-zero)
            # (B) overflow of x**y with x and y finite
            if x == 0.0:
                errno = EDOM
            else:
                errno = ERANGE
    if errno:
        _likely_raise(errno, r)
    return r
 def gettimeofday():
     now = lltype.malloc(TIMEVALP.TO, 1, flavor='raw')
     try:
         res = _gettimeofday(now, None)
         if res < 0:
             raise OSError(rposix.get_saved_errno(), "gettimeofday failed")
         return (rffi.getintfield(now[0], 'c_tv_sec'),
                 rffi.getintfield(now[0], 'c_tv_usec'))
     finally:
         lltype.free(now, flavor='raw')
Example #19
0
 def disable(self):
     """Disable vmprof.
     Raises VMProfError if something goes wrong.
     """
     if not self.is_enabled:
         raise VMProfError("vmprof is not enabled")
     self.is_enabled = False
     res = self.cintf.vmprof_disable()
     if res < 0:
         raise VMProfError(os.strerror(rposix.get_saved_errno()))
Example #20
0
 def disable(self):
     """Disable vmprof.
     Raises VMProfError if something goes wrong.
     """
     if not self.is_enabled:
         raise VMProfError("vmprof is not enabled")
     self.is_enabled = False
     res = self.cintf.vmprof_disable()
     if res < 0:
         raise VMProfError(os.strerror(rposix.get_saved_errno()))
Example #21
0
 def gettimeofday():
     now = lltype.malloc(TIMEVALP.TO, 1, flavor='raw')
     try:
         res = _gettimeofday(now, None)
         if res < 0:
             raise OSError(rposix.get_saved_errno(), "gettimeofday failed")
         return (rffi.getintfield(now[0], 'c_tv_sec'),
                 rffi.getintfield(now[0], 'c_tv_usec'))
     finally:
         lltype.free(now, flavor='raw')
Example #22
0
def posix_initgroups(space, name, base_group_id):
    """ posix_initgroups - Calculate the group access list """
    buf = rffi.str2charp(name)
    try:
        res = initgroups(buf, base_group_id)
        if res == -1:
            space.set_errno(rposix.get_saved_errno())
            return space.w_False
        return space.w_True
    finally:
        lltype.free(buf, flavor='raw')
Example #23
0
def tcgetattr(fd):
    with lltype.scoped_alloc(TERMIOSP.TO) as c_struct:
        if c_tcgetattr(fd, c_struct) < 0:
            raise OSError(rposix.get_saved_errno(), 'tcgetattr failed')
        cc = [chr(c_struct.c_c_cc[i]) for i in range(NCCS)]
        ispeed = c_cfgetispeed(c_struct)
        ospeed = c_cfgetospeed(c_struct)
        result = (intmask(c_struct.c_c_iflag), intmask(c_struct.c_c_oflag),
                  intmask(c_struct.c_c_cflag), intmask(c_struct.c_c_lflag),
                  intmask(ispeed), intmask(ospeed), cc)
        return result
Example #24
0
def siginterrupt(space, signum, flag):
    """siginterrupt(sig, flag) -> None

    change system call restart behaviour: if flag is False, system calls
    will be restarted when interrupted by signal sig, else system calls
    will be interrupted.
    """
    check_signum_in_range(space, signum)
    if rffi.cast(lltype.Signed, c_siginterrupt(signum, flag)) < 0:
        errno = rposix.get_saved_errno()
        raise OperationError(space.w_RuntimeError, space.wrap(errno))
Example #25
0
def siginterrupt(space, signum, flag):
    """siginterrupt(sig, flag) -> None

    change system call restart behaviour: if flag is False, system calls
    will be restarted when interrupted by signal sig, else system calls
    will be interrupted.
    """
    check_signum_in_range(space, signum)
    if rffi.cast(lltype.Signed, c_siginterrupt(signum, flag)) < 0:
        errno = rposix.get_saved_errno()
        raise OperationError(space.w_RuntimeError, space.newint(errno))
Example #26
0
 def write(self, value):
     self._check_closed()
     with rffi.scoped_nonmovingbuffer(value) as ll_value:
         # note that since we got a nonmoving buffer, it is either raw
         # or already cannot move, so the arithmetics below are fine
         length = len(value)
         bytes = c_fwrite(ll_value, 1, length, self._ll_file)
         if bytes != length:
             errno = rposix.get_saved_errno()
             c_clearerr(self._ll_file)
             raise IOError(errno, os.strerror(errno))
Example #27
0
def posix_initgroups(space, name, base_group_id):
    """ posix_initgroups - Calculate the group access list """
    buf = rffi.str2charp(name)
    try:
        res = initgroups(buf, base_group_id)
        if res == -1:
            space.set_errno(rposix.get_saved_errno())
            return space.w_False
        return space.w_True
    finally:
        lltype.free(buf, flavor='raw')
def sh_cmd(vm):
    (cmd_o, ), _ = vm.decode_args("S")
    assert isinstance(cmd_o, Con_String)

    r = system(cmd_o.v)
    if r == -1:
        vm.raise_helper(
            "Exception",
            [Con_String(vm, os.strerror(rposix.get_saved_errno()))])

    return Con_Int(vm, os.WEXITSTATUS(r))
Example #29
0
 def write(self, value):
     self._check_closed()
     with rffi.scoped_nonmovingbuffer(value) as ll_value:
         # note that since we got a nonmoving buffer, it is either raw
         # or already cannot move, so the arithmetics below are fine
         length = len(value)
         bytes = c_fwrite(ll_value, 1, length, self._ll_file)
         if bytes != length:
             errno = rposix.get_saved_errno()
             c_clearerr(self._ll_file)
             raise IOError(errno, os.strerror(errno))
Example #30
0
    def epoll_ctl(self, space, ctl, w_fd, eventmask, ignore_ebadf=False):
        fd = space.c_filedescriptor_w(w_fd)
        with lltype.scoped_alloc(epoll_event) as ev:
            ev.c_events = rffi.cast(rffi.UINT, eventmask)
            rffi.setintfield(ev.c_data, "c_fd", fd)

            result = epoll_ctl(self.epfd, ctl, fd, ev)
            if ignore_ebadf and get_saved_errno() == errno.EBADF:
                result = 0
            if result < 0:
                raise exception_from_saved_errno(space, space.w_IOError)
Example #31
0
    def epoll_ctl(self, space, ctl, w_fd, eventmask, ignore_ebadf=False):
        fd = space.c_filedescriptor_w(w_fd)
        with lltype.scoped_alloc(epoll_event) as ev:
            ev.c_events = rffi.cast(rffi.UINT, eventmask)
            rffi.setintfield(ev.c_data, 'c_fd', fd)

            result = epoll_ctl(self.epfd, ctl, fd, ev)
            if ignore_ebadf and get_saved_errno() == errno.EBADF:
                result = 0
            if result < 0:
                raise exception_from_saved_errno(space, space.w_IOError)
Example #32
0
 def unsetenv_llimpl(name):
     with rffi.scoped_str2charp(name) as l_name:
         error = rffi.cast(lltype.Signed, os_unsetenv(l_name))
     if error:
         from rpython.rlib import rposix
         raise OSError(rposix.get_saved_errno(), "os_unsetenv failed")
     try:
         l_oldstring = envkeepalive.byname[name]
     except KeyError:
         pass
     else:
         del envkeepalive.byname[name]
         rffi.free_charp(l_oldstring)
Example #33
0
 def posix_statvfs_llimpl(arg):
     stresult = lltype.malloc(STATVFS_STRUCT.TO, flavor='raw')
     try:
         if arg_is_path:
             arg = traits.str2charp(arg)
         error = rffi.cast(rffi.LONG, posix_mystatvfs(arg, stresult))
         if arg_is_path:
             traits.free_charp(arg)
         if error != 0:
             raise OSError(rposix.get_saved_errno(), "os_?statvfs failed")
         return build_statvfs_result(stresult)
     finally:
         lltype.free(stresult, flavor='raw')
Example #34
0
def make_write_blocking_error(space, written):
    # XXX CPython reads 'errno' here.  I *think* it doesn't make sense,
    # because we might reach this point after calling a write() method
    # that may be overridden by the user, if that method returns None.
    # In that case what we get is a potentially nonsense errno.  But
    # we'll use get_saved_errno() anyway, and hope (like CPython does)
    # that we're getting a reasonable value at this point.
    w_type = space.gettypeobject(W_BlockingIOError.typedef)
    w_value = space.call_function(
        w_type, space.newint(rposix.get_saved_errno()),
        space.newtext("write could not complete without blocking"),
        space.newint(written))
    return OperationError(w_type, w_value)
Example #35
0
 def unsetenv_llimpl(name):
     with rffi.scoped_str2charp(name) as l_name:
         error = rffi.cast(lltype.Signed, os_unsetenv(l_name))
     if error:
         from rpython.rlib import rposix
         raise OSError(rposix.get_saved_errno(), "os_unsetenv failed")
     try:
         l_oldstring = envkeepalive.byname[name]
     except KeyError:
         pass
     else:
         del envkeepalive.byname[name]
         rffi.free_charp(l_oldstring)
Example #36
0
 def posix_statvfs_llimpl(arg):
     stresult = lltype.malloc(STATVFS_STRUCT.TO, flavor='raw')
     try:
         if arg_is_path:
             arg = traits.str2charp(arg)
         error = rffi.cast(rffi.LONG, posix_mystatvfs(arg, stresult))
         if arg_is_path:
             traits.free_charp(arg)
         if error != 0:
             raise OSError(rposix.get_saved_errno(), "os_?statvfs failed")
         return build_statvfs_result(stresult)
     finally:
         lltype.free(stresult, flavor='raw')
Example #37
0
            def time_sleep_llimpl(secs):
                void = lltype.nullptr(rffi.VOIDP.TO)
                t = lltype.malloc(self.TIMEVAL, flavor='raw')
                try:
                    frac = math.fmod(secs, 1.0)
                    rffi.setintfield(t, 'c_tv_sec', int(secs))
                    rffi.setintfield(t, 'c_tv_usec', int(frac*1000000.0))

                    if rffi.cast(rffi.LONG, c_select(0, void, void, void, t)) != 0:
                        errno = rposix.get_saved_errno()
                        if errno != EINTR:
                            raise OSError(errno, "Select failed")
                finally:
                    lltype.free(t, flavor='raw')
Example #38
0
def create_popen_file(command, type):
    ll_command = rffi.str2charp(command)
    try:
        ll_type = rffi.str2charp(type)
        try:
            ll_file = c_popen(ll_command, ll_type)
            if not ll_file:
                errno = rposix.get_saved_errno()
                raise OSError(errno, os.strerror(errno))
        finally:
            lltype.free(ll_type, flavor='raw')
    finally:
        lltype.free(ll_command, flavor='raw')
    return RFile(ll_file, close2=_pclose2)
Example #39
0
 def nextentry(dirp):
     """Read the next entry and returns an opaque object.
     Use the methods has_xxx() and get_xxx() to read from that
     opaque object.  The opaque object is valid until the next
     time nextentry() or closedir() is called.  This may raise
     OSError, or return a NULL pointer when exhausted.  Note
     that this doesn't filter out the "." and ".." entries.
     """
     direntp = rposix.c_readdir(dirp)
     if direntp:
         error = rposix.get_saved_errno()
         if error:
             raise OSError(error, "readdir failed")
     return direntp
Example #40
0
def make_write_blocking_error(space, written):
    # XXX CPython reads 'errno' here.  I *think* it doesn't make sense,
    # because we might reach this point after calling a write() method
    # that may be overridden by the user, if that method returns None.
    # In that case what we get is a potentially nonsense errno.  But
    # we'll use get_saved_errno() anyway, and hope (like CPython does)
    # that we're getting a reasonable value at this point.
    w_type = space.gettypeobject(W_BlockingIOError.typedef)
    w_value = space.call_function(
        w_type,
        space.wrap(rposix.get_saved_errno()),
        space.wrap("write could not complete without blocking"),
        space.wrap(written))
    return OperationError(w_type, w_value)
Example #41
0
def nextentry(dirp):
    """Read the next entry and returns an opaque object.
    Use the methods has_xxx() and get_xxx() to read from that
    opaque object.  The opaque object is valid until the next
    time nextentry() or closedir() is called.  This may raise
    OSError, or return a NULL pointer when exhausted.  Note
    that this doesn't filter out the "." and ".." entries.
    """
    direntp = rposix.c_readdir(dirp)
    if direntp:
        error = rposix.get_saved_errno()
        if error:
            raise OSError(error, "readdir failed")
    return direntp
Example #42
0
def create_popen_file(command, type):
    ll_command = rffi.str2charp(command)
    try:
        ll_type = rffi.str2charp(type)
        try:
            ll_file = c_popen(ll_command, ll_type)
            if not ll_file:
                errno = rposix.get_saved_errno()
                raise OSError(errno, os.strerror(errno))
        finally:
            lltype.free(ll_type, flavor='raw')
    finally:
        lltype.free(ll_command, flavor='raw')
    return RFile(ll_file, close2=_pclose2)
 def disable(self, space):
     if not self.is_enabled:
         raise oefmt(space.w_ValueError, "_vmprof not enabled")
     self.is_enabled = False
     space.register_code_callback(None)
     self._flush_codes(space)
     self.fileno = -1
     if we_are_translated():
         # does not work untranslated
         res = vmprof_disable()
     else:
         res = 0
     if res == -1:
         raise wrap_oserror(
             space, OSError(rposix.get_saved_errno(), "_vmprof.disable"))
Example #44
0
def ll_math_fmod(x, y):
    # fmod(x, +/-Inf) returns x for finite x.
    if isinf(y) and isfinite(x):
        return x

    r = math_fmod(x, y)
    errno = rposix.get_saved_errno()
    if isnan(r):
        if isnan(x) or isnan(y):
            errno = 0
        else:
            errno = EDOM
    if errno:
        _likely_raise(errno, r)
    return r
Example #45
0
def ll_math_fmod(x, y):
    # fmod(x, +/-Inf) returns x for finite x.
    if math.isinf(y) and isfinite(x):
        return x

    r = math_fmod(x, y)
    errno = rposix.get_saved_errno()
    if math.isnan(r):
        if math.isnan(x) or math.isnan(y):
            errno = 0
        else:
            errno = EDOM
    if errno:
        _likely_raise(errno, r)
    return r
Example #46
0
 def tell(self):
     self._check_closed()
     res = intmask(c_ftell(self._ll_file))
     if res == -1:
         errno = rposix.get_saved_errno()
         raise IOError(errno, os.strerror(errno))
     if self._skipnextlf:
         c = c_getc(self._ll_file)
         if c == ord('\n'):
             self._newlinetypes |= NEWLINE_CRLF
             res += 1
             self._skipnextlf = False
         elif c != EOF:
             c_ungetc(c, self._ll_file)
     return res
Example #47
0
def create_fdopen_rfile(fd, mode="r", buffering=-1):
    newmode = _sanitize_mode(mode)
    rposix.validate_fd(fd)
    ll_mode = rffi.str2charp(newmode)
    try:
        ll_file = c_fdopen(fd, ll_mode)
        if not ll_file:
            errno = rposix.get_saved_errno()
            raise OSError(errno, os.strerror(errno))
    finally:
        lltype.free(ll_mode, flavor='raw')
    _dircheck(ll_file)
    f = RFile(ll_file, mode)
    f._setbufsize(buffering)
    return f
Example #48
0
 def disable(self, space):
     if not self.is_enabled:
         raise oefmt(space.w_ValueError, "_vmprof not enabled")
     self.is_enabled = False
     space.register_code_callback(None)
     self._flush_codes(space)
     self.fileno = -1
     if we_are_translated():
        # does not work untranslated
         res = vmprof_disable()
     else:
         res = 0
     if res == -1:
         raise wrap_oserror(space, OSError(rposix.get_saved_errno(),
                                           "_vmprof.disable"))
Example #49
0
 def tell(self):
     self._check_closed()
     res = intmask(c_ftell(self._ll_file))
     if res == -1:
         errno = rposix.get_saved_errno()
         raise IOError(errno, os.strerror(errno))
     if self._skipnextlf:
         c = c_getc(self._ll_file)
         if c == ord('\n'):
             self._newlinetypes |= NEWLINE_CRLF
             res += 1
             self._skipnextlf = False
         elif c != EOF:
             c_ungetc(c, self._ll_file)
     return res
    def readinto_w(self, space, w_buffer):
        self._check_closed(space)
        self._check_readable(space)
        rwbuffer = space.writebuf_w(w_buffer)
        length = rwbuffer.getlength()

        target_address = lltype.nullptr(rffi.CCHARP.TO)
        if length > 64:
            try:
                target_address = rwbuffer.get_raw_address()
            except ValueError:
                pass

        if not target_address:
            # unoptimized case
            while True:
                try:
                    buf = os.read(self.fd, length)
                    break
                except OSError as e:
                    if e.errno == errno.EAGAIN:
                        return space.w_None
                    wrap_oserror(space,
                                 e,
                                 exception_name='w_IOError',
                                 eintr_retry=True)
            rwbuffer.setslice(0, buf)
            return space.newint(len(buf))
        else:
            # optimized case: reading more than 64 bytes into a rwbuffer
            # with a valid raw address
            while True:
                got = c_read(self.fd, target_address, length)
                keepalive_until_here(rwbuffer)
                got = rffi.cast(lltype.Signed, got)
                if got >= 0:
                    return space.newint(got)
                else:
                    err = get_saved_errno()
                    if err == errno.EAGAIN:
                        return space.w_None
                    e = OSError(err, "read failed")
                    wrap_oserror(space,
                                 e,
                                 exception_name='w_IOError',
                                 eintr_retry=True)
Example #51
0
def sleep(secs):
    if _WIN32:
        millisecs = secs * 1000.0
        while millisecs > UINT_MAX:
            Sleep(UINT_MAX)
            millisecs -= UINT_MAX
        Sleep(rffi.cast(rffi.ULONG, int(millisecs)))
    else:
        void = lltype.nullptr(rffi.VOIDP.TO)
        with lltype.scoped_alloc(TIMEVAL) as t:
            frac = math.fmod(secs, 1.0)
            rffi.setintfield(t, 'c_tv_sec', int(secs))
            rffi.setintfield(t, 'c_tv_usec', int(frac*1000000.0))

            if rffi.cast(rffi.LONG, c_select(0, void, void, void, t)) != 0:
                errno = rposix.get_saved_errno()
                if errno != EINTR:
                    raise OSError(errno, "Select failed")
Example #52
0
def ll_math_ldexp(x, exp):
    if x == 0.0 or not isfinite(x):
        return x  # NaNs, zeros and infinities are returned unchanged
    if exp > INT_MAX:
        # overflow (64-bit platforms only)
        r = math_copysign(INFINITY, x)
        errno = ERANGE
    elif exp < INT_MIN:
        # underflow to +-0 (64-bit platforms only)
        r = math_copysign(0.0, x)
        errno = 0
    else:
        r = math_ldexp(x, exp)
        errno = rposix.get_saved_errno()
        if math.isinf(r):
            errno = ERANGE
    if errno:
        _likely_raise(errno, r)
    return r
Example #53
0
    def enable(self, fileno, interval):
        """Enable vmprof.  Writes go to the given 'fileno'.
        The sampling interval is given by 'interval' as a number of
        seconds, as a float which must be smaller than 1.0.
        Raises VMProfError if something goes wrong.
        """
        assert fileno >= 0
        if self.is_enabled:
            raise VMProfError("vmprof is already enabled")

        p_error = self.cintf.vmprof_init(fileno, interval, "pypy")
        if p_error:
            raise VMProfError(rffi.charp2str(p_error))

        self._gather_all_code_objs()
        res = self.cintf.vmprof_enable()
        if res < 0:
            raise VMProfError(os.strerror(rposix.get_saved_errno()))
        self.is_enabled = True
Example #54
0
    def flush(self, offset=0, size=0):
        if size == 0:
            size = self.size
        if offset < 0 or size < 0 or offset + size > self.size:
            raise RValueError("flush values out of range")
        else:
            start = self.getptr(offset)
            if _MS_WINDOWS:
                res = FlushViewOfFile(start, size)
                # XXX res == 0 means that an error occurred, but in CPython
                # this is not checked
                return res
            elif _POSIX:
                res = c_msync(start, size, MS_SYNC)
                if res == -1:
                    errno = rposix.get_saved_errno()
                    raise OSError(errno, os.strerror(errno))

        return 0
Example #55
0
 def ll_math(x):
     r = c_func(x)
     # Error checking fun.  Copied from CPython 2.6
     errno = rposix.get_saved_errno()
     if not isfinite(r):
         if math.isnan(r):
             if math.isnan(x):
                 errno = 0
             else:
                 errno = EDOM
         else:  # isinf(r)
             if not isfinite(x):
                 errno = 0
             elif can_overflow:
                 errno = ERANGE
             else:
                 errno = EDOM
     if errno:
         _likely_raise(errno, r)
     return r
Example #56
0
 def enable(self, space, fileno, period_usec):
     if self.is_enabled:
         raise oefmt(space.w_ValueError, "_vmprof already enabled")
     self.fileno = fileno
     self.is_enabled = True
     self.write_header(fileno, period_usec)
     if not self.ever_enabled:
         if we_are_translated():
             pypy_vmprof_init()
         self.ever_enabled = True
     self.gather_all_code_objs(space)
     space.register_code_callback(vmprof_register_code)
     if we_are_translated():
         # does not work untranslated
         res = vmprof_enable(fileno, period_usec, 0,
                             lltype.nullptr(rffi.CCHARP.TO), 0)
     else:
         res = 0
     if res == -1:
         raise wrap_oserror(
             space, OSError(rposix.get_saved_errno(), "_vmprof.enable"))
Example #57
0
    def alloc(map_size):
        """Allocate memory.  This is intended to be used by the JIT,
        so the memory has the executable bit set and gets allocated
        internally in case of a sandboxed process.
        """
        from errno import ENOMEM
        from rpython.rlib import debug

        if _CYGWIN:
            # XXX: JIT memory should be using mmap MAP_PRIVATE with
            #      PROT_EXEC but Cygwin's fork() fails.  mprotect()
            #      cannot be used, but seems to be unnecessary there.
            res = c_malloc_safe(map_size)
            if res == rffi.cast(PTR, 0):
                raise MemoryError
            return res
        # Berkin: first try with hugetlb.
        res = alloc_hinted(rffi.cast(PTR, hint.pos), map_size, hugetlb=True)
        if res == rffi.cast(PTR, -1):
            # Berkin: if it fails, hugetlb might be disabled, try without
            # it.
            res = alloc_hinted(rffi.cast(PTR, hint.pos), map_size)
        if res == rffi.cast(PTR, -1):
            # some systems (some versions of OS/X?) complain if they
            # are passed a non-zero address.  Try again.
            res = alloc_hinted(rffi.cast(PTR, 0), map_size)
            if res == rffi.cast(PTR, -1):
                # ENOMEM simply raises MemoryError, but other errors are fatal
                if rposix.get_saved_errno() != ENOMEM:
                    debug.fatalerror_notb(
                        "Got an unexpected error trying to allocate some "
                        "memory for the JIT (tried to do mmap() with "
                        "PROT_EXEC|PROT_READ|PROT_WRITE).  This can be caused "
                        "by a system policy like PAX.  You need to find how "
                        "to work around the policy on your system.")
                raise MemoryError
        else:
            hint.pos += map_size
        return res
Example #58
0
def ll_math_hypot(x, y):
    # hypot(x, +/-Inf) returns Inf, even if x is a NaN.
    if math.isinf(x):
        return math_fabs(x)
    if math.isinf(y):
        return math_fabs(y)

    r = math_hypot(x, y)
    errno = rposix.get_saved_errno()
    if not isfinite(r):
        if math.isnan(r):
            if math.isnan(x) or math.isnan(y):
                errno = 0
            else:
                errno = EDOM
        else:  # isinf(r)
            if isfinite(x) and isfinite(y):
                errno = ERANGE
            else:
                errno = 0
    if errno:
        _likely_raise(errno, r)
    return r
Example #59
0
def initialize_from_env():
    # This uses the same algorithms as CPython 3.5.  The environment
    # variable we read also defaults to "PYTHONHASHSEED".  If needed,
    # a different RPython interpreter can patch the value of the
    # global variable 'env_var_name', or just patch the whole
    # initialize_from_env() function.
    value = os.environ.get(env_var_name)
    if value and value != "random":
        with rffi.scoped_view_charp(value) as ptr:
            with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as endptr:
                endptr[0] = ptr
                seed = strtoul(ptr, endptr, 10)
                full = endptr[0][0] == '\x00'
        seed = lltype.cast_primitive(lltype.Unsigned, seed)
        if not full or seed > r_uint(4294967295) or (
                rposix.get_saved_errno() == errno.ERANGE
                and seed == lltype.cast_primitive(lltype.Unsigned,
                                                  rffi.cast(rffi.ULONG, -1))):
            os.write(
                2, "%s must be \"random\" or an integer "
                "in range [0; 4294967295]\n" % (env_var_name, ))
            os._exit(1)
        if not seed:
            # disable the randomized hash
            s = '\x00' * 16
        else:
            s = lcg_urandom(seed)
    else:
        try:
            s = rurandom.urandom(random_ctx, 16)
        except Exception as e:
            os.write(
                2, "%s: failed to get random numbers to initialize Python\n" %
                (str(e), ))
            os._exit(1)
            raise  # makes the annotator happy
    select_random_seed(s)