def _sem_timedwait_save(sem, deadline): delay = 0 void = lltype.nullptr(rffi.VOIDP.TO) with lltype.scoped_alloc(TIMEVALP.TO, 1) as tvdeadline: while True: # poll if _sem_trywait(sem) == 0: return 0 elif rposix.get_errno() != errno.EAGAIN: return -1 now = gettimeofday() c_tv_sec = rffi.getintfield(deadline[0], 'c_tv_sec') c_tv_nsec = rffi.getintfield(deadline[0], 'c_tv_nsec') if (c_tv_sec < now[0] or (c_tv_sec == now[0] and c_tv_nsec <= now[1])): rposix.set_errno(errno.ETIMEDOUT) return -1 # calculate how much time is left difference = ((c_tv_sec - now[0]) * 1000000 + (c_tv_nsec - now[1])) # check delay not too long -- maximum is 20 msecs if delay > 20000: delay = 20000 if delay > difference: delay = difference delay += 1000 # sleep rffi.setintfield(tvdeadline[0], 'c_tv_sec', delay / 1000000) rffi.setintfield(tvdeadline[0], 'c_tv_usec', delay % 1000000) if _select(0, void, void, void, tvdeadline) < 0: return -1
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 os_listdir_llimpl(path): dirp = os_opendir(path) if not dirp: raise OSError(rposix.get_errno(), "os_opendir failed") rposix.set_errno(0) result = [] while True: direntp = os_readdir(dirp) if not direntp: error = rposix.get_errno() break namep = rffi.cast(rffi.CCHARP, direntp.c_d_name) name = rffi.charp2str(namep) if name != '.' and name != '..': result.append(name) os_closedir(dirp) if error: raise OSError(error, "os_readdir failed") return result
def f(): set_errno(12) return get_errno()
def _error_reset(): rposix.set_errno(0)
def set_errno(space, w_errno): rposix.set_errno(space.int_w(w_errno))
def after_external_call(): e = get_errno() spacestate.GIL.acquire(True) set_errno(e)
def before_external_call(): # this function must not raise, in such a way that the exception # transformer knows that it cannot raise! e = get_errno() thread.release_NOAUTO(spacestate.ll_GIL) set_errno(e)
def after_external_call(): e = get_errno() thread.acquire_NOAUTO(spacestate.ll_GIL, True) thread.gc_thread_run() spacestate.after_thread_switch() set_errno(e)
def after_external_call(): e = get_errno() thread.gil_acquire() thread.gc_thread_run() spacestate.after_thread_switch() set_errno(e)
def before_external_call(): # this function must not raise, in such a way that the exception # transformer knows that it cannot raise! e = get_errno() thread.gil_release() set_errno(e)