import os from pypy.rpython.lltypesystem.rffi import CConstant, CExternVariable, INT from pypy.rpython.lltypesystem import lltype, ll2ctypes, rffi from pypy.translator.tool.cbuild import ExternalCompilationInfo from pypy.rlib.rarithmetic import intmask class CConstantErrno(CConstant): # these accessors are used when calling get_errno() or set_errno() # on top of CPython def __getitem__(self, index): assert index == 0 try: return ll2ctypes.TLS.errno except AttributeError: raise ValueError("no C function call occurred so far, " "errno is undefined") def __setitem__(self, index, value): assert index == 0 ll2ctypes.TLS.errno = value errno_eci = ExternalCompilationInfo( includes=['errno.h'] ) _get_errno, _set_errno = CExternVariable(INT, 'errno', errno_eci, CConstantErrno, sandboxsafe=True, _nowrapper=True, c_type='int') # the default wrapper for set_errno is not suitable for use in critical places # like around GIL handling logic, so we provide our own wrappers. def get_errno(): return intmask(_get_errno()) def set_errno(errno): _set_errno(rffi.cast(INT, errno)) def closerange(fd_low, fd_high): # this behaves like os.closerange() from Python 2.6. for fd in xrange(fd_low, fd_high): try: os.close(fd) except OSError: pass