def readdir(dirp): rposix.set_errno(0) direntp = os_readdir(dirp) if not direntp: if rposix.get_errno() == 0: return None else: raise OSError(rposix.get_errno(), "error in readdir") namep = rffi.cast(rffi.CCHARP, direntp.c_d_name) return rffi.charp2str(namep)
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_errno() if errno != EINTR: raise OSError(rposix.get_errno(), "Select failed") finally: lltype.free(t, flavor='raw')
def exception_from_errno(space, w_type): from rpython.rlib.rposix import get_errno errno = get_errno() msg = os.strerror(errno) w_error = space.call_function(w_type, space.wrap(errno), space.wrap(msg)) return OperationError(w_type, w_error)
def startup(self): if not OPROFILE_AVAILABLE: return agent = op_open_agent() if not agent: raise OProfileError(get_errno(), "startup") self.agent = agent
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_errno() raise OperationError(space.w_OSError, space.wrap(errno)) return space.wrap(rffi.charp2str(dirname))
def f(i, j): for param, _ in unroll_parameters: defl = PARAMETERS[param] set_param(jitdriver, param, defl) set_param(jitdriver, "threshold", 3) set_param(jitdriver, "trace_eagerness", 2) total = 0 frame = Frame(i) j = float(j) while frame.i > 3: jitdriver.can_enter_jit(frame=frame, total=total, j=j) jitdriver.jit_merge_point(frame=frame, total=total, j=j) total += frame.i if frame.i >= 20: frame.i -= 2 frame.i -= 1 j *= -0.712 if j + (-j): raise ValueError k = myabs1(myabs2(j)) if k - abs(j): raise ValueError if k - abs(-j): raise ValueError if t.get().nine != 9: raise ValueError rposix.set_errno(total) if rposix.get_errno() != total: raise ValueError return chr(total % 253)
def flush(self): if self.ll_file: res = c_fflush(self.ll_file) if res != 0: errno = rposix.get_errno() raise OSError(errno, os.strerror(errno)) return raise ValueError("I/O operation on closed file")
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(get_errno(), "write")
def seek(self, pos, whence=0): ll_file = self.ll_file if not ll_file: raise ValueError("I/O operation on closed file") res = c_fseek(ll_file, pos, whence) if res == -1: errno = rposix.get_errno() raise OSError(errno, os.strerror(errno))
def make_write_blocking_error(space, written): w_type = space.gettypeobject(W_BlockingIOError.typedef) w_value = space.call_function( w_type, space.wrap(rposix.get_errno()), space.wrap("write could not complete without blocking"), space.wrap(written)) return OperationError(w_type, w_value)
def tell(self): if self.ll_file: res = intmask(c_ftell(self.ll_file)) if res == -1: errno = rposix.get_errno() raise OSError(errno, os.strerror(errno)) return res raise ValueError("I/O operation on closed file")
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 _error_reset() r = math_pow(x, y) errno = rposix.get_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 flush(self): if self.buf_count > 0: bytes = self.buf_count * rffi.sizeof(rffi.LONG) count = raw_os_write(self.fd, rffi.cast(llmemory.Address, self.writebuffer), rffi.cast(rffi.SIZE_T, bytes)) if rffi.cast(lltype.Signed, count) != bytes: raise OSError(rposix.get_errno(), "raw_os_write failed") self.buf_count = 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_errno(), "sem_getvalue failed") return rffi.cast(lltype.Signed, sval_ptr[0]) finally: lltype.free(sval_ptr, flavor='raw')
def PyOS_string_to_double(space, s, endptr, w_overflow_exception): """Convert a string s to a double, raising a Python exception on failure. The set of accepted strings corresponds to the set of strings accepted by Python's float() constructor, except that s must not have leading or trailing whitespace. The conversion is independent of the current locale. If endptr is NULL, convert the whole string. Raise ValueError and return -1.0 if the string is not a valid representation of a floating-point number. If endptr is not NULL, convert as much of the string as possible and set *endptr to point to the first unconverted character. If no initial segment of the string is the valid representation of a floating-point number, set *endptr to point to the beginning of the string, raise ValueError, and return -1.0. If s represents a value that is too large to store in a float (for example, "1e500" is such a string on many platforms) then if overflow_exception is NULL return Py_HUGE_VAL (with an appropriate sign) and don't set any exception. Otherwise, overflow_exception must point to a Python exception object; raise that exception and return -1.0. In both cases, set *endptr to point to the first character after the converted value. If any other error occurs during the conversion (for example an out-of-memory error), set the appropriate Python exception and return -1.0. """ user_endptr = True try: if not endptr: endptr = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw') user_endptr = False result = rdtoa.dg_strtod(s, endptr) endpos = (rffi.cast(rffi.LONG, endptr[0]) - rffi.cast(rffi.LONG, s)) if endpos == 0 or (not user_endptr and not endptr[0][0] == '\0'): raise OperationError( space.w_ValueError, space.wrap('invalid input at position %s' % endpos)) if rposix.get_errno() == errno.ERANGE: rposix.set_errno(0) if w_overflow_exception is None: if result > 0: return rfloat.INFINITY else: return -rfloat.INFINITY else: raise OperationError(w_overflow_exception, space.wrap('value too large')) return result finally: if not user_endptr: lltype.free(endptr, flavor='raw')
def truncate(self, arg=-1): if self.ll_file: if arg == -1: arg = self.tell() res = c_ftruncate(self.fileno(), arg) if res == -1: errno = rposix.get_errno() raise OSError(errno, os.strerror(errno)) return raise ValueError("I/O operation on closed file")
def gettimeofday(): now = lltype.malloc(TIMEVALP.TO, 1, flavor='raw') try: res = _gettimeofday(now, None) if res < 0: raise OSError(rposix.get_errno(), "gettimeofday failed") return (rffi.getintfield(now[0], 'c_tv_sec'), rffi.getintfield(now[0], 'c_tv_usec')) finally: lltype.free(now, flavor='raw')
def tcsetattr_llimpl(fd, when, attributes): c_struct = lltype.malloc(TERMIOSP.TO, flavor='raw') try: c_struct.c_c_iflag = r_uint(attributes[0]) c_struct.c_c_oflag = r_uint(attributes[1]) c_struct.c_c_cflag = r_uint(attributes[2]) c_struct.c_c_lflag = r_uint(attributes[3]) ispeed = r_uint(attributes[4]) ospeed = r_uint(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_errno(), 'tcsetattr failed') if c_cfsetospeed(c_struct, ospeed) < 0: raise OSError(rposix.get_errno(), 'tcsetattr failed') if c_tcsetattr(fd, when, c_struct) < 0: raise OSError(rposix.get_errno(), 'tcsetattr failed') finally: lltype.free(c_struct, flavor='raw')
def create_fdopen_rfile(fd, mode="r"): assert mode is not None ll_mode = rffi.str2charp(mode) try: ll_f = c_fdopen(rffi.cast(rffi.INT, fd), ll_mode) if not ll_f: errno = rposix.get_errno() raise OSError(errno, os.strerror(errno)) finally: lltype.free(ll_mode, flavor='raw') return RFile(ll_f)
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_errno()) return space.w_False return space.w_True finally: lltype.free(buf, flavor='raw')
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_errno() == errno.EBADF: result = 0 if result < 0: raise exception_from_errno(space, space.w_IOError)
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_errno(), "os_?statvfs failed") return build_statvfs_result(stresult) finally: lltype.free(stresult, flavor='raw')
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_errno(), "os_unsetenv failed") try: l_oldstring = envkeepalive.byname[name] except KeyError: pass else: del envkeepalive.byname[name] rffi.free_charp(l_oldstring)
def create_popen_file(command, type): ll_command = rffi.str2charp(command) try: ll_type = rffi.str2charp(type) try: ll_f = c_popen(ll_command, ll_type) if not ll_f: errno = rposix.get_errno() raise OSError(errno, os.strerror(errno)) finally: lltype.free(ll_type, flavor='raw') finally: lltype.free(ll_command, flavor='raw') return RPopenFile(ll_f)
def tcgetattr_llimpl(fd): c_struct = lltype.malloc(TERMIOSP.TO, flavor='raw') try: if c_tcgetattr(fd, c_struct) < 0: raise OSError(rposix.get_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 finally: lltype.free(c_struct, flavor='raw')
def ll_math_fmod(x, y): # fmod(x, +/-Inf) returns x for finite x. if isinf(y) and isfinite(x): return x _error_reset() r = math_fmod(x, y) errno = rposix.get_errno() if isnan(r): if isnan(x) or isnan(y): errno = 0 else: errno = EDOM if errno: _likely_raise(errno, r) return r
def create_file(filename, mode="r", buffering=-1): assert buffering == -1 assert filename is not None assert mode is not None ll_name = rffi.str2charp(filename) try: ll_mode = rffi.str2charp(mode) try: ll_f = c_open(ll_name, ll_mode) if not ll_f: errno = rposix.get_errno() raise OSError(errno, os.strerror(errno)) finally: lltype.free(ll_mode, flavor='raw') finally: lltype.free(ll_name, flavor='raw') return RFile(ll_f)
def write(self, value): assert value is not None ll_file = self.ll_file if not ll_file: raise ValueError("I/O operation on closed file") assert value is not None ll_value = rffi.get_nonmovingbuffer(value) try: # 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, ll_file) if bytes != length: errno = rposix.get_errno() raise OSError(errno, os.strerror(errno)) finally: rffi.free_nonmovingbuffer(value, ll_value)
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_f = self.ll_file if ll_f: # double close is allowed self.ll_file = lltype.nullptr(FILEP.TO) res = self._do_close(ll_f) if res == -1: errno = rposix.get_errno() raise OSError(errno, os.strerror(errno)) return res
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_errno() raise OSError(errno, os.strerror(errno)) return 0
def after_external_call(): e = get_errno() rgil.gil_acquire() rthread.gc_thread_run() after_thread_switch() set_errno(e)
def opendir(path): dirp = os_opendir(path) if not dirp: raise OSError(rposix.get_errno(), "error in opendir") return dirp
def _errmsg(what): from rpython.rlib import rposix errmsg = _strerror(rposix.get_errno()) return u"%s failed" % what if errmsg is None else errmsg
def llimpl(sign, beforept, afterpt, exponent): res = ll_parts_to_float(sign, beforept, afterpt, exponent) if res == -1 and rposix.get_errno() == 42: raise ValueError("Wrong literal for float") return res
def _get_error_msg(): errno = rposix.get_errno() return os.strerror(errno)
def tcsendbreak_llimpl(fd, duration): if c_tcsendbreak(fd, duration): raise OSError(rposix.get_errno(), 'tcsendbreak failed')
def tcdrain_llimpl(fd): if c_tcdrain(fd) < 0: raise OSError(rposix.get_errno(), 'tcdrain failed')
def tcflush_llimpl(fd, queue_selector): if c_tcflush(fd, queue_selector) < 0: raise OSError(rposix.get_errno(), 'tcflush failed')
def tcflow_llimpl(fd, action): if c_tcflow(fd, action) < 0: raise OSError(rposix.get_errno(), 'tcflow failed')