import sys from rpython.rtyper.lltypesystem import lltype, llmemory, rffi from rpython.rtyper.lltypesystem.lloperation import llop from rpython.rlib import clibffi, jit from rpython.rlib.rarithmetic import r_longlong, r_singlefloat from rpython.rlib.unroll import unrolling_iterable FFI_CIF = clibffi.FFI_CIFP.TO FFI_TYPE = clibffi.FFI_TYPE_P.TO FFI_TYPE_P = clibffi.FFI_TYPE_P FFI_TYPE_PP = clibffi.FFI_TYPE_PP FFI_ABI = clibffi.FFI_ABI FFI_TYPE_STRUCT = clibffi.FFI_TYPE_STRUCT SIZE_OF_FFI_ARG = rffi.sizeof(clibffi.ffi_arg) SIZE_OF_SIGNED = rffi.sizeof(lltype.Signed) FFI_ARG_P = rffi.CArrayPtr(clibffi.ffi_arg) # Usage: for each C function, make one CIF_DESCRIPTION block of raw # memory. Initialize it by filling all its fields apart from 'cif'. # The 'atypes' points to an array of ffi_type pointers; a reasonable # place to locate this array's memory is in the same block of raw # memory, by allocating more than sizeof(CIF_DESCRIPTION). # # The four fields 'abi', 'nargs', 'rtype', 'atypes' are the same as # the arguments to ffi_prep_cif(). # # Following this, we find jit_libffi-specific information: # # - 'exchange_size': an integer that tells how big a buffer we must # allocate to do the call; this buffer should have enough room at the # beginning for an array of NARGS pointers which is initialized
defines += ' PROCESS_QUERY_LIMITED_INFORMATION' for name in defines.split(): locals()[name] = rffi_platform.ConstantInteger(name) for k, v in rffi_platform.configure(CConfig).items(): globals()[k] = v def winexternal(name, args, result, **kwds): return rffi.llexternal(name, args, result, compilation_info=eci, calling_conv='win', **kwds) if WIN32: HANDLE = rffi.COpaquePtr(typedef='HANDLE') assert rffi.cast(HANDLE, -1) == rffi.cast(HANDLE, -1) LPHANDLE = rffi.CArrayPtr(HANDLE) HMODULE = HANDLE NULL_HANDLE = rffi.cast(HANDLE, 0) INVALID_HANDLE_VALUE = rffi.cast(HANDLE, -1) GENERIC_READ = rffi.cast(DWORD, r_longlong(0x80000000)) GENERIC_WRITE = rffi.cast(DWORD, r_longlong(0x40000000)) GENERIC_EXECUTE = rffi.cast(DWORD, r_longlong(0x20000000)) GENERIC_ALL = rffi.cast(DWORD, r_longlong(0x10000000)) FILE_SHARE_READ = rffi.cast(DWORD, r_longlong(0x00000001)) FILE_SHARE_WRITE = rffi.cast(DWORD, r_longlong(0x00000002)) ALL_READ_WRITE = rffi.cast(DWORD, r_longlong(0xC0000003)) SHARE_READ_WRITE = rffi.cast(DWORD, r_longlong(0x00000003)) PFILETIME = rffi.CArrayPtr(FILETIME) _GetLastError = winexternal('GetLastError', [], DWORD,
class CConfig: _compilation_info_ = eci TIMEVAL = platform.Struct('struct timeval', [('tv_sec', rffi.LONG), ('tv_usec', rffi.LONG)]) TIMESPEC = platform.Struct('struct timespec', [('tv_sec', rffi.TIME_T), ('tv_nsec', rffi.LONG)]) SEM_FAILED = platform.ConstantInteger('SEM_FAILED') SEM_VALUE_MAX = platform.DefinedConstantInteger('SEM_VALUE_MAX') SEM_TIMED_WAIT = platform.Has('sem_timedwait') SEM_T_SIZE = platform.SizeOf('sem_t') config = platform.configure(CConfig) TIMEVAL = config['TIMEVAL'] TIMESPEC = config['TIMESPEC'] TIMEVALP = rffi.CArrayPtr(TIMEVAL) TIMESPECP = rffi.CArrayPtr(TIMESPEC) SEM_T = rffi.COpaquePtr('sem_t', compilation_info=eci) # rffi.cast(SEM_T, config['SEM_FAILED']) SEM_FAILED = config['SEM_FAILED'] SEM_VALUE_MAX = config['SEM_VALUE_MAX'] if SEM_VALUE_MAX is None: # on Hurd SEM_VALUE_MAX = sys.maxint SEM_TIMED_WAIT = config['SEM_TIMED_WAIT'] SEM_T_SIZE = config['SEM_T_SIZE'] if sys.platform == 'darwin': HAVE_BROKEN_SEM_GETVALUE = True else: HAVE_BROKEN_SEM_GETVALUE = False def external(name, args, result, **kwargs):
'b': rffi.SIGNEDCHAR, 'B': rffi.UCHAR, 'h': rffi.SHORT, 'H': rffi.USHORT, 'i': rffi.INT, 'I': rffi.UINT, 'l': rffi.LONG, 'L': rffi.ULONG, 'q': rffi.LONGLONG, 'Q': rffi.ULONGLONG, 'f': rffi.FLOAT, 'd': rffi.DOUBLE, 'g': rffi.LONGDOUBLE, 's': rffi.CCHARP, 'z': rffi.CCHARP, 'Z': rffi.CArrayPtr(lltype.UniChar), 'O': rffi.VOIDP, 'P': rffi.VOIDP, '?': lltype.Bool, } if _MS_WINDOWS: LL_TYPEMAP['X'] = rffi.CCHARP LL_TYPEMAP['v'] = rffi.SHORT def letter2tp(space, key): from pypy.module._rawffi.array import PRIMITIVE_ARRAY_TYPES try: return PRIMITIVE_ARRAY_TYPES[key] except KeyError:
asm_ifc = 'win64.asm' eci = ExternalCompilationInfo( includes=['ffi.h', 'windows.h'], libraries=['kernel32'], include_dirs=[libffidir, cdir], separate_module_sources=separate_module_sources, separate_module_files=[ libffidir.join('ffi.c'), libffidir.join('prep_cif.c'), libffidir.join(asm_ifc), libffidir.join('pypy_ffi.c'), ], ) FFI_TYPE_P = lltype.Ptr(lltype.ForwardReference()) FFI_TYPE_PP = rffi.CArrayPtr(FFI_TYPE_P) FFI_TYPE_NULL = lltype.nullptr(FFI_TYPE_P.TO) class CConfig: _compilation_info_ = eci FFI_OK = rffi_platform.ConstantInteger('FFI_OK') FFI_BAD_TYPEDEF = rffi_platform.ConstantInteger('FFI_BAD_TYPEDEF') FFI_DEFAULT_ABI = rffi_platform.ConstantInteger('FFI_DEFAULT_ABI') if _WIN32 and not _WIN64: FFI_STDCALL = rffi_platform.ConstantInteger('FFI_STDCALL') if _ARM: FFI_SYSV = rffi_platform.ConstantInteger('FFI_SYSV') FFI_VFP = rffi_platform.ConstantInteger('FFI_VFP')
class CConfig(object): timelib_tzdb = platform.Struct('timelib_tzdb', ([ ('version', rffi.CCHARP), ('index_size', rffi.INT), ('index', timelib_tzdb_index_entryP), ('data', rffi.CArrayPtr(rffi.UCHAR)), ])) timelib_error_container = platform.Struct( 'timelib_error_container', [('error_count', lltype.Signed), ('error_messages', rffi.CArrayPtr(timelib_error_message)), ('warning_count', lltype.Signed)]) timelib_time = platform.Struct('timelib_time', ([ ('y', lltype.Signed), ('m', lltype.Signed), ('d', lltype.Signed), ('h', lltype.Signed), ('i', lltype.Signed), ('s', lltype.Signed), ('f', lltype.Float), ('z', lltype.Signed), ('tz_abbr', rffi.CCHARP), ('tz_info', timelib_tzinfo), ('dst', lltype.Signed), ('relative', timelib_rel_timeP.TO), ('sse', lltype.Signed), ('have_time', lltype.Unsigned), ('have_date', lltype.Unsigned), ('have_relative', lltype.Unsigned), ('have_weeknr_day', lltype.Unsigned), ('sse_uptodate', lltype.Unsigned), ('tim_uptodate', lltype.Unsigned), ('zone_type', lltype.Signed), ('is_localtime', lltype.Signed), ])) timelib_time_offset = platform.Struct( 'timelib_time_offset', [('offset', lltype.Signed), ('leap_secs', rffi.UINT), ('is_dst', rffi.UINT), ('abbr', rffi.CCHARP), ('transistion_time', lltype.Signed)]) timelib_tz_lookup_table = platform.Struct('timelib_tz_lookup_table', [ ('name', rffi.CCHARP), ('type', lltype.Signed), ('gmtoffset', lltype.SingleFloat), ('full_tz_name', rffi.CCHARP), ]) timelib_tz_lookup_table = platform.Struct('timelib_tz_lookup_table', [ ('name', rffi.CCHARP), ('type', lltype.Signed), ('gmtoffset', lltype.SingleFloat), ('full_tz_name', rffi.CCHARP), ]) TIMELIB_NO_CLONE = platform.DefinedConstantInteger('TIMELIB_NO_CLONE') TIMELIB_ZONETYPE_ID = platform.DefinedConstantInteger( 'TIMELIB_ZONETYPE_ID') TIMELIB_ZONETYPE_ABBR = platform.DefinedConstantInteger( 'TIMELIB_ZONETYPE_ABBR') TIMELIB_ZONETYPE_OFFSET = platform.DefinedConstantInteger( 'TIMELIB_ZONETYPE_OFFSET') _compilation_info_ = eci
class Win32Traits: apisuffix = suffix for name in '''WIN32_FIND_DATA WIN32_FILE_ATTRIBUTE_DATA BY_HANDLE_FILE_INFORMATION GetFileExInfoStandard FILE_ATTRIBUTE_DIRECTORY FILE_ATTRIBUTE_READONLY INVALID_FILE_ATTRIBUTES _S_IFDIR _S_IFREG _S_IFCHR _S_IFIFO FILE_TYPE_UNKNOWN FILE_TYPE_CHAR FILE_TYPE_PIPE FILE_READ_ATTRIBUTES FILE_ATTRIBUTE_NORMAL FILE_WRITE_ATTRIBUTES OPEN_EXISTING FILE_FLAG_BACKUP_SEMANTICS VOLUME_NAME_DOS VOLUME_NAME_NT ERROR_FILE_NOT_FOUND ERROR_NO_MORE_FILES ERROR_SHARING_VIOLATION MOVEFILE_REPLACE_EXISTING ERROR_ACCESS_DENIED '''.split(): locals()[name] = config[name] LPWIN32_FIND_DATA = lltype.Ptr(WIN32_FIND_DATA) GET_FILEEX_INFO_LEVELS = rffi.ULONG # an enumeration FindFirstFile = external('FindFirstFile' + suffix, [traits.CCHARP, LPWIN32_FIND_DATA], rwin32.HANDLE, save_err=rffi.RFFI_SAVE_LASTERROR) FindNextFile = external('FindNextFile' + suffix, [rwin32.HANDLE, LPWIN32_FIND_DATA], rwin32.BOOL, save_err=rffi.RFFI_SAVE_LASTERROR) FindClose = external('FindClose', [rwin32.HANDLE], rwin32.BOOL, releasegil=False) GetFileAttributes = external('GetFileAttributes' + suffix, [traits.CCHARP], rwin32.DWORD, save_err=rffi.RFFI_SAVE_LASTERROR) SetFileAttributes = external('SetFileAttributes' + suffix, [traits.CCHARP, rwin32.DWORD], rwin32.BOOL, save_err=rffi.RFFI_SAVE_LASTERROR) GetFileAttributesEx = external('GetFileAttributesEx' + suffix, [ traits.CCHARP, GET_FILEEX_INFO_LEVELS, lltype.Ptr(WIN32_FILE_ATTRIBUTE_DATA) ], rwin32.BOOL, save_err=rffi.RFFI_SAVE_LASTERROR) GetFileInformationByHandle = external( 'GetFileInformationByHandle', [rwin32.HANDLE, lltype.Ptr(BY_HANDLE_FILE_INFORMATION)], rwin32.BOOL, save_err=rffi.RFFI_SAVE_LASTERROR) GetFileType = external('GetFileType', [rwin32.HANDLE], rwin32.DWORD, save_err=rffi.RFFI_SAVE_LASTERROR) LPSTRP = rffi.CArrayPtr(traits.CCHARP) GetFullPathName = external( 'GetFullPathName' + suffix, [traits.CCHARP, rwin32.DWORD, traits.CCHARP, LPSTRP], rwin32.DWORD, save_err=rffi.RFFI_SAVE_LASTERROR) GetCurrentDirectory = external('GetCurrentDirectory' + suffix, [rwin32.DWORD, traits.CCHARP], rwin32.DWORD, save_err=rffi.RFFI_SAVE_LASTERROR) SetCurrentDirectory = external('SetCurrentDirectory' + suffix, [traits.CCHARP], rwin32.BOOL, save_err=rffi.RFFI_SAVE_LASTERROR) CreateDirectory = external('CreateDirectory' + suffix, [traits.CCHARP, rffi.VOIDP], rwin32.BOOL, save_err=rffi.RFFI_SAVE_LASTERROR) SetEnvironmentVariable = external('SetEnvironmentVariable' + suffix, [traits.CCHARP, traits.CCHARP], rwin32.BOOL, save_err=rffi.RFFI_SAVE_LASTERROR) CreateFile = external('CreateFile' + apisuffix, [ traits.CCHARP, rwin32.DWORD, rwin32.DWORD, rwin32.LPSECURITY_ATTRIBUTES, rwin32.DWORD, rwin32.DWORD, rwin32.HANDLE ], rwin32.HANDLE, save_err=rffi.RFFI_SAVE_LASTERROR) DeleteFile = external('DeleteFile' + suffix, [traits.CCHARP], rwin32.BOOL, save_err=rffi.RFFI_SAVE_LASTERROR) MoveFileEx = external('MoveFileEx' + suffix, [traits.CCHARP, traits.CCHARP, rwin32.DWORD], rwin32.BOOL, save_err=rffi.RFFI_SAVE_LASTERROR) CreateHardLink = external( 'CreateHardLink' + suffix, [traits.CCHARP, traits.CCHARP, rwin32.LPSECURITY_ATTRIBUTES], rwin32.BOOL, save_err=rffi.RFFI_SAVE_LASTERROR)
def test_elidable_kinds(): from rpython.jit.backend.llgraph.runner import LLGraphCPU from rpython.rlib.objectmodel import compute_hash from rpython.rlib.rsiphash import enable_siphash24 @jit.elidable def f1(n, m): return n + m @jit.elidable def f2(n, m): return [n, m] # may raise MemoryError @jit.elidable def f3(n, m): if n > m: raise ValueError return n + m @jit.elidable def f4(n, m): return compute_hash(str(n) + str(m)) T = rffi.CArrayPtr(rffi.TIME_T) external = rffi.llexternal("time", [T], rffi.TIME_T, releasegil=True) def effect(): return external(lltype.nullptr(T.TO)) @jit.elidable def f5(n, m): effect() return 1 def f(n, m): a = f1(n, m) b = f2(n, m) c = f3(n, m) d = f4(n, m) f5(n, m) enable_siphash24() return a + len(b) + c + d rtyper = support.annotate(f, [7, 9]) jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0]) cc = CallControl(LLGraphCPU(rtyper), jitdrivers_sd=[jitdriver_sd]) res = cc.find_all_graphs(FakePolicy()) [f_graph] = [x for x in res if x.func is f] for index, expected in [(0, EffectInfo.EF_ELIDABLE_CANNOT_RAISE), (1, EffectInfo.EF_ELIDABLE_OR_MEMORYERROR), (2, EffectInfo.EF_ELIDABLE_CAN_RAISE), (3, EffectInfo.EF_ELIDABLE_OR_MEMORYERROR)]: call_op = f_graph.startblock.operations[index] assert call_op.opname == 'direct_call' call_descr = cc.getcalldescr(call_op) assert call_descr.extrainfo.extraeffect == expected call_op = f_graph.startblock.operations[4] assert call_op.opname == 'direct_call' excinfo = py.test.raises(Exception, cc.getcalldescr, call_op) lines = excinfo.value.args[0].splitlines() assert "f5" in lines[2] assert "effect" in lines[3] assert "random effects" in lines[-1]
def op_raw_store(p, ofs, newvalue): from rpython.rtyper.lltypesystem import rffi p = rffi.cast(llmemory.Address, p) TVAL = lltype.typeOf(newvalue) p = rffi.cast(rffi.CArrayPtr(TVAL), p + ofs) p[0] = newvalue
public_symbols["EPOLLPRI"]) epoll_create1 = rffi.llexternal( "epoll_create1", [rffi.INT], rffi.INT, compilation_info=eci, save_err=rffi.RFFI_SAVE_ERRNO ) epoll_ctl = rffi.llexternal( "epoll_ctl", [rffi.INT, rffi.INT, rffi.INT, lltype.Ptr(epoll_event)], rffi.INT, compilation_info=eci, save_err=rffi.RFFI_SAVE_ERRNO ) epoll_wait = rffi.llexternal( "epoll_wait", [rffi.INT, rffi.CArrayPtr(epoll_event), rffi.INT, rffi.INT], rffi.INT, compilation_info=eci, save_err=rffi.RFFI_SAVE_ERRNO ) class W_Epoll(W_Root): def __init__(self, space, epfd): self.space = space self.epfd = epfd self.register_finalizer(space) @unwrap_spec(sizehint=int, flags=int) def descr__new__(space, w_subtype, sizehint=-1, flags=0): if sizehint == -1:
def convert_to_adr(self, c): assert isinstance(c, ConstFloat) adr = self.assembler.datablockwrapper.malloc_aligned(8, 8) x = c.getfloatstorage() rffi.cast(rffi.CArrayPtr(longlong.FLOATSTORAGE), adr)[0] = x return adr
def unpack_ptr(self, w_ctypeptr, ptr, length): s = rffi.charpsize2str(ptr, length) return self.space.newbytes(s) # XXX explicitly use an integer type instead of lltype.UniChar here, # because for now the latter is defined as unsigned by RPython (even # though it may be signed when 'wchar_t' is written to C). WCHAR_INT = { (2, False): rffi.USHORT, (4, False): rffi.UINT, (4, True): rffi.INT }[rffi.sizeof(lltype.UniChar), rfficache.signof_c_type('wchar_t')] WCHAR_INTP = rffi.CArrayPtr(WCHAR_INT) class W_CTypePrimitiveUniChar(W_CTypePrimitiveCharOrUniChar): _attrs_ = [] if rffi.r_wchar_t.SIGN: def write_raw_integer_data(self, w_cdata, value): w_cdata.write_raw_signed_data(value) def cast_to_int(self, cdata): unichardata = rffi.cast(WCHAR_INTP, cdata) return self.space.newint(unichardata[0]) def convert_to_object(self, cdata):
errors = rffi.charp2str(llerrors) else: errors = None state = space.fromcache(CodecState) result = runicode.unicode_encode_decimal(u, length, errors, state.encode_error_handler) i = len(result) output[i] = '\0' i -= 1 while i >= 0: output[i] = result[i] i -= 1 return 0 @cpython_api([rffi.CArrayPtr(Py_UNICODE), Py_ssize_t], PyObject) def PyUnicode_TransformDecimalToASCII(space, s, size): """Create a Unicode object by replacing all decimal digits in Py_UNICODE buffer of the given size by ASCII digits 0--9 according to their decimal value. Return NULL if an exception occurs.""" result = rstring.UnicodeBuilder(size) for i in range(size): ch = s[i] if ord(ch) > 127: decimal = Py_UNICODE_TODECIMAL(space, ch) decimal = rffi.cast(lltype.Signed, decimal) if decimal >= 0: ch = unichr(ord('0') + decimal) result.append(ch) return space.wrap(result.build())
ACCESS_DESCRIPTION_st = rffi_platform.Struct( 'struct ACCESS_DESCRIPTION_st', [('method', ASN1_OBJECT), ('location', GENERAL_NAME),]) for k, v in rffi_platform.configure(CConfig).items(): globals()[k] = v # opaque structures SSL_METHOD = rffi.COpaquePtr('SSL_METHOD') SSL_CTX = rffi.COpaquePtr('SSL_CTX') SSL_CIPHER = rffi.COpaquePtr('SSL_CIPHER') SSL = rffi.COpaquePtr('SSL') BIO = rffi.COpaquePtr('BIO') if OPENSSL_VERSION_NUMBER >= 0x10001000: X509 = rffi.CArrayPtr(X509_st) else: X509 = rffi.COpaquePtr('X509') X509_NAME_ENTRY = rffi.CArrayPtr(X509_name_entry_st) X509_EXTENSION = rffi.CArrayPtr(X509_extension_st) X509_STORE = rffi.CArrayPtr(x509_store_st) X509_OBJECT = lltype.Ptr(x509_object_st) X509V3_EXT_METHOD = rffi.CArrayPtr(v3_ext_method) ASN1_STRING.TO.become(asn1_string_st) ASN1_TIME = rffi.COpaquePtr('ASN1_TIME') ASN1_INTEGER = rffi.COpaquePtr('ASN1_INTEGER') GENERAL_NAMES = rffi.COpaquePtr('GENERAL_NAMES') GENERAL_NAME.TO.become(GENERAL_NAME_st) OBJ_NAME = rffi.CArrayPtr(OBJ_NAME_st) COMP_METHOD = rffi.CArrayPtr(COMP_METHOD_st) ACCESS_DESCRIPTION = rffi.CArrayPtr(ACCESS_DESCRIPTION_st)
def fork_exec(space, w_process_args, w_executable_list, w_close_fds, w_fds_to_keep, w_cwd, w_env_list, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, errpipe_read, errpipe_write, restore_signals, call_setsid, w_preexec_fn): """\ fork_exec(args, executable_list, close_fds, cwd, env, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, errpipe_read, errpipe_write, restore_signals, call_setsid, preexec_fn) Forks a child process, closes parent file descriptors as appropriate in the child and dups the few that are needed before calling exec() in the child process. The preexec_fn, if supplied, will be called immediately before exec. WARNING: preexec_fn is NOT SAFE if your application uses threads. It may trigger infrequent, difficult to debug deadlocks. If an error occurs in the child process before the exec, it is serialized and written to the errpipe_write fd per subprocess.py. Returns: the child process's PID. Raises: Only on an error in the parent process. """ close_fds = space.is_true(w_close_fds) if close_fds and errpipe_write < 3: # precondition raise oefmt(space.w_ValueError, "errpipe_write must be >= 3") fds_to_keep = build_fd_sequence(space, w_fds_to_keep) # No need to disable GC in PyPy: # - gc.disable() only disables __del__ anyway. # - appelvel __del__ are only called at specific points of the # interpreter. l_exec_array = lltype.nullptr(rffi.CCHARPP.TO) l_argv = lltype.nullptr(rffi.CCHARPP.TO) l_envp = lltype.nullptr(rffi.CCHARPP.TO) l_cwd = lltype.nullptr(rffi.CCHARP.TO) l_fds_to_keep = lltype.nullptr(rffi.CArrayPtr(rffi.LONG).TO) # Convert args and env into appropriate arguments for exec() # These conversions are done in the parent process to avoid allocating # or freeing memory in the child process. try: l_exec_array = seqstr2charpp(space, w_executable_list) if not space.is_none(w_process_args): w_iter = space.iter(w_process_args) argv = [ space.fsencode_w(space.next(w_iter)) for i in range(space.len_w(w_process_args)) ] l_argv = rffi.liststr2charpp(argv) if not space.is_none(w_env_list): l_envp = seqstr2charpp(space, w_env_list) l_fds_to_keep = lltype.malloc(rffi.CArrayPtr(rffi.LONG).TO, len(fds_to_keep) + 1, flavor='raw') for i in range(len(fds_to_keep)): l_fds_to_keep[i] = fds_to_keep[i] if not space.is_none(w_preexec_fn): preexec.space = space preexec.w_preexec_fn = w_preexec_fn else: preexec.w_preexec_fn = None if not space.is_none(w_cwd): cwd = space.fsencode_w(w_cwd) l_cwd = rffi.str2charp(cwd) run_fork_hooks('before', space) try: try: pid = os.fork() except OSError as e: raise wrap_oserror(space, e) if pid == 0: # Child process # Code from here to _exit() must only use # async-signal-safe functions, listed at `man 7 signal` # http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. if not space.is_none(w_preexec_fn): # We'll be calling back into Python later so we need # to do this. This call may not be async-signal-safe # but neither is calling back into Python. The user # asked us to use hope as a strategy to avoid # deadlock... run_fork_hooks('child', space) c_child_exec(l_exec_array, l_argv, l_envp, l_cwd, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, errpipe_read, errpipe_write, close_fds, restore_signals, call_setsid, l_fds_to_keep, len(fds_to_keep), PreexecCallback.run_function, None) os._exit(255) finally: # parent process run_fork_hooks('parent', space) finally: preexec.w_preexec_fn = None if l_cwd: rffi.free_charp(l_cwd) if l_envp: rffi.free_charpp(l_envp) if l_argv: rffi.free_charpp(l_argv) if l_exec_array: rffi.free_charpp(l_exec_array) if l_fds_to_keep: lltype.free(l_fds_to_keep, flavor='raw') return space.newint(pid)
def op_raw_load(TVAL, p, ofs): from rpython.rtyper.lltypesystem import rffi p = rffi.cast(llmemory.Address, p) p = rffi.cast(rffi.CArrayPtr(TVAL), p + ofs) return p[0]
("tm_year", rffi.INT), ("tm_wday", rffi.INT), ("tm_yday", rffi.INT), ("tm_isdst", rffi.INT), ("tm_gmtoff", rffi.LONG), ("tm_zone", rffi.CCHARP)]) timeval = platform.Struct("struct timeval", [("tv_sec", rffi.INT), ("tv_usec", rffi.INT)]) _compilation_info_ = eci conf = platform.configure(CConfig) tzinfo = rffi.CArrayPtr(conf['ttinfo']) tm = conf['tm'] tmP = lltype.Ptr(tm) timeval = conf['timeval'] timevalP = lltype.Ptr(timeval) c_strftime = external('strftime', [rffi.CCHARP, rffi.SIZE_T, rffi.CCHARP, tmP], rffi.SIZE_T) c_gettimeofday = external('gettimeofday', [rffi.VOIDP, rffi.VOIDP], rffi.INT) class CConfig(object): timelib_special = platform.Struct('timelib_special', [ ('type', lltype.Unsigned), ('amount', lltype.Signed),
import os from rpython.rtyper.lltypesystem import rffi, lltype from rpython.translator.tool.cbuild import ExternalCompilationInfo from sast.tokens import Token #pylint: disable=invalid-name CTOKEN = rffi.CStruct('CToken', ('start', rffi.LONG), ('end', rffi.LONG), ('kind', rffi.LONG), ('line', rffi.LONG), ('pos', rffi.LONG), ('skip', rffi.LONG)) CTOKENP = rffi.CArrayPtr(CTOKEN) CTOKENPP = rffi.CArrayPtr(CTOKENP) def deref(obj): return obj[0] def escape(old): new = '' for char in old: if char == '\n': new += '\\' new += 'n' else: new += char return new
def date_format(format_string, t): week_year_set = False isoweek = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw') isoyear = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw') offset = lltype.nullptr(timelib_time_offset.TO) zone_type = rffi.cast(lltype.Signed, t.c_zone_type) if zone_type == ZONETYPE_ABBR: z = rffi.cast(lltype.Signed, t.c_z) offset = timelib_time_offset_ctor() offset.c_offset = rffi.cast( rffi.INT, (z - (rffi.cast(lltype.Signed, t.c_dst) * 60)) * -60) offset.c_leap_secs = rffi.cast(rffi.UINT, 0) offset.c_is_dst = rffi.cast(rffi.UINT, t.c_dst) elif zone_type == ZONETYPE_OFFSET: z = rffi.cast(lltype.Signed, t.c_z) offset = timelib_time_offset_ctor() offset.c_offset = rffi.cast(rffi.INT, z * -60) offset.c_leap_secs = rffi.cast(rffi.UINT, 0) offset.c_is_dst = rffi.cast(rffi.UINT, 0) offset.c_abbr = rffi.str2charp("GMT+2000") # not really else: offset = timelib_get_time_zone_info(t.c_sse, t.c_tz_info) date_string = [] i = 0 d = rffi.cast(lltype.Signed, t.c_d) i_offset = rffi.cast(lltype.Signed, offset.c_offset) while i < len(format_string): element = format_string[i] i += 1 if element == 'd': date_string.append(format_to(2, d)) elif element == 'D': date_string.append(short_day_name(t.c_y, t.c_m, d)) elif element == 'j': date_string.append("%d" % d) elif element == 'l': date_string.append(full_day_name(t.c_y, t.c_m, d)) elif element == 'S': date_string.append(english_suffix(d)) elif element == 'w': date_string.append("%d" % timelib_day_of_week(t.c_y, t.c_m, d)) elif element == 'N': date_string.append("%d" % timelib_iso_day_of_week(t.c_y, t.c_m, d)) elif element == 'z': date_string.append("%d" % timelib_day_of_year(t.c_y, t.c_m, d)) elif element == 'W': if not week_year_set: timelib_isoweek_from_date(t.c_y, t.c_m, t.c_d, isoweek, isoyear) week_year_set = True date_string.append( format_to(2, rffi.cast(lltype.Signed, isoweek[0]))) elif element == 'o': if not week_year_set: timelib_isoweek_from_date(t.c_y, t.c_m, t.c_d, isoweek, isoyear) week_year_set = True date_string.append("%d" % rffi.cast(lltype.Signed, isoyear[0])) elif element == 'F': date_string.append(full_month_names[t.c_m - 1]) elif element == 'm': date_string.append(format_to(2, t.c_m)) elif element == 'M': date_string.append(short_month_names[t.c_m - 1]) elif element == 'n': date_string.append("%d" % t.c_m) elif element == 't': date_string.append("%d" % timelib_days_in_month(t.c_y, t.c_m)) elif element == 'L': date_string.append("%d" % is_leap(t.c_y)) elif element == 'Y': date_string.append(format_to(4, t.c_y)) elif element == 'y': y = '%d' % t.c_y stop = len(y) - 2 if stop < 0: stop = 0 date_string.append(y[stop:]) elif element == 'a': date_string.append("pm" if t.c_h >= 12 else "am") elif element == 'A': date_string.append("PM" if t.c_h >= 12 else "AM") elif element == 'B': retval = ((((t.c_sse) - ((t.c_sse) - (((t.c_sse) % 86400) + 3600))) * 10) / 864) while retval < 0: retval += 1000 retval = retval % 1000 date_string.append(format_to(3, retval)) elif element == 'g': if t.c_h % 12: date_string.append("%d" % (t.c_h % 12)) else: date_string.append("12") elif element == 'h': if t.c_h % 12: date_string.append(format_to(2, t.c_h % 12)) else: date_string.append("12") elif element == 'G': date_string.append("%d" % t.c_h) elif element == 'H': date_string.append(format_to(2, t.c_h)) elif element == 'i': date_string.append(format_to(2, t.c_i)) elif element == 's': date_string.append(format_to(2, t.c_s)) elif element == 'u': date_string.append(element) # raise NotImplementedError(element) elif element == 'I': date_string.append("%d" % rffi.cast(lltype.Signed, offset.c_is_dst)) elif element == 'P': date_string.append("%s%s:%s" % (('-' if i_offset < 0 else '+'), format_to(2, abs(i_offset / 3600)), format_to(2, abs(i_offset % 3600) / 60))) elif element == 'O': date_string.append("%s%s%s" % (('-' if i_offset < 0 else '+'), format_to(2, abs(i_offset / 3600)), format_to(2, abs(i_offset % 3600) / 60))) elif element == 'T': if t.c_tz_abbr: date_string.append(rffi.charp2str(t.c_tz_abbr)) elif element == 'e': if zone_type == ZONETYPE_ID: date_string.append(rffi.charp2str(t.c_tz_info.c_name)) else: raise NotImplementedError() elif element == 'Z': date_string.append("%d" % rffi.cast(lltype.Signed, i_offset)) elif element == 'c': date_string.append( "%s-%s-%sT%s:%s:%s%s%s:%s" % (format_to(4, rffi.cast(lltype.Signed, t.c_y)), format_to(2, rffi.cast(lltype.Signed, t.c_m)), format_to(2, t.c_d), format_to(2, rffi.cast(lltype.Signed, t.c_h)), format_to(2, rffi.cast(lltype.Signed, t.c_i)), format_to(2, rffi.cast(lltype.Signed, t.c_s)), "-" if i_offset < 0 else "+", format_to(2, abs( i_offset / 3600)), format_to(2, abs(i_offset % 3600) / 60))) elif element == 'r': i_offset = rffi.cast(lltype.Signed, offset.c_offset) date_string.append("%s, %s %s %s %s:%s:%s %s%s%s" % ( format_str_to(3, short_day_name(t.c_y, t.c_m, d), " "), format_to(2, d), format_str_to(3, short_month_names[t.c_m - 1], " "), format_to(4, t.c_y), format_to(2, t.c_h), format_to(2, t.c_i), format_to(2, t.c_s), '-' if i_offset < 0 else '+', format_to(2, abs(i_offset / 3600)), format_to(2, abs(i_offset % 3600)), )) elif element == 'U': date_string.append("%d" % t.c_sse) elif element == '\\': if i < len(format_string): date_string.append(format_string[i]) i += 1 else: date_string.append(element) lltype.free(isoyear, flavor='raw') lltype.free(isoweek, flavor='raw') return "".join(date_string)
if e.match(space, space.w_ValueError): e.w_type = space.w_OverflowError raise @cpython_api([PyObject], rffi.ULONGLONG, error=-1) def PyLong_AsUnsignedLongLongMask(space, w_long): """Will first attempt to cast the object to a PyIntObject or PyLongObject, if it is not already one, and then return its value as unsigned long long, without checking for overflow. """ num = space.bigint_w(w_long) return num.ulonglongmask() @cpython_api([PyObject, rffi.CArrayPtr(rffi.INT_real)], lltype.Signed, error=-1) def PyLong_AsLongAndOverflow(space, w_long, overflow_ptr): """ Return a C long representation of the contents of pylong. If pylong is greater than LONG_MAX or less than LONG_MIN, set *overflow to 1 or -1, respectively, and return -1; otherwise, set *overflow to 0. If any other exception occurs (for example a TypeError or MemoryError), then -1 will be returned and *overflow will be 0.""" overflow_ptr[0] = rffi.cast(rffi.INT_real, 0) try: return space.int_w(w_long) except OperationError, e: if not e.match(space, space.w_OverflowError): raise
def convert_to_imm(self, c): adr = self.assembler.datablockwrapper.malloc_aligned(8, 8) x = c.getfloatstorage() rffi.cast(rffi.CArrayPtr(longlong.FLOATSTORAGE), adr)[0] = x return locations.ConstFloatLoc(adr)
cConfig = platform.configure(CConfig) constants.update(cConfig) globals().update(cConfig) def external(name, args, result, **kwds): return rffi.llexternal(name, args, result, compilation_info=eci, calling_conv='win', **kwds) HKEY = rwin32.HANDLE PHKEY = rffi.CArrayPtr(HKEY) REGSAM = rwin32.DWORD def get_traits(suffix): RegSetValue = external( 'RegSetValue' + suffix, [HKEY, rffi.CCHARP, rwin32.DWORD, rffi.CCHARP, rwin32.DWORD], rffi.LONG) RegSetValueEx = external('RegSetValueEx' + suffix, [ HKEY, rffi.CCHARP, rwin32.DWORD, rwin32.DWORD, rffi.CCHARP, rwin32.DWORD ], rffi.LONG) RegQueryValue = external('RegQueryValue' + suffix,
def PyType_FromSpecWithBases(space, spec, bases): from pypy.module.cpyext.unicodeobject import PyUnicode_FromString state = space.fromcache(State) p_type = cts.cast('PyTypeObject*', make_ref(space, space.w_type)) res = state.ccall("PyType_GenericAlloc", p_type, 0) res = cts.cast('PyHeapTypeObject *', res) typ = res.c_ht_type typ.c_tp_flags = rffi.cast(lltype.Unsigned, spec.c_flags) typ.c_tp_flags |= Py_TPFLAGS_HEAPTYPE specname = rffi.charp2str(cts.cast('char*', spec.c_name)) dotpos = specname.rfind('.') if dotpos < 0: name = specname else: name = specname[dotpos + 1:] res.c_ht_name = make_ref(space, space.newtext(name)) res.c_ht_qualname = res.c_ht_name incref(space, res.c_ht_qualname) typ.c_tp_name = spec.c_name slotdefs = rffi.cast(rffi.CArrayPtr(cts.gettype('PyType_Slot')), spec.c_slots) if not bases: w_base = space.w_object bases_w = [] i = 0 while True: slotdef = slotdefs[i] slotnum = rffi.cast(lltype.Signed, slotdef.c_slot) if slotnum == 0: break elif slotnum == cts.macros['Py_tp_base']: w_base = from_ref(space, cts.cast('PyObject*', slotdef.c_pfunc)) elif slotnum == cts.macros['Py_tp_bases']: bases = cts.cast('PyObject*', slotdef.c_pfunc) bases_w = space.fixedview(from_ref(space, bases)) i += 1 if not bases_w: bases_w = [w_base] else: bases_w = space.fixedview(from_ref(space, bases)) w_base = best_base(space, bases_w) base = cts.cast('PyTypeObject*', make_ref(space, w_base)) if False: # not base.c_tp_flags & Py_TPFLAGS_BASETYPE: raise oefmt(space.w_TypeError, "type '%s' is not an acceptable base type", rffi.charp2str(base.c_tp_name)) typ.c_tp_as_async = res.c_as_async typ.c_tp_as_number = res.c_as_number typ.c_tp_as_sequence = res.c_as_sequence typ.c_tp_as_mapping = res.c_as_mapping typ.c_tp_as_buffer = res.c_as_buffer typ.c_tp_bases = bases typ.c_tp_base = base typ.c_tp_basicsize = cts.cast('Py_ssize_t', spec.c_basicsize) typ.c_tp_itemsize = cts.cast('Py_ssize_t', spec.c_itemsize) i = 0 while True: slotdef = slotdefs[i] slot = rffi.cast(lltype.Signed, slotdef.c_slot) if slot == 0: break if slot < 0: # or slot > len(slotoffsets): raise oefmt(space.w_RuntimeError, "invalid slot offset") if slot in (cts.macros['Py_tp_base'], cts.macros['Py_tp_bases']): # Processed above i += 1 continue fill_ht_slot(res, slot, slotdef.c_pfunc) # XXX: need to make a copy of the docstring slot, which usually # points to a static string literal i += 1 if not typ.c_tp_dealloc: typ.c_tp_dealloc = state.C._PyPy_subtype_dealloc py_type_ready(space, typ) return cts.cast('PyObject*', res)
def array_getitem_T(TYPE, width, addr, index, offset): addr = rffi.ptradd(addr, index * width) addr = rffi.ptradd(addr, offset) return rffi.cast(rffi.CArrayPtr(TYPE), addr)[0]
return ref_str.c_ob_sval @cpython_api([rffi.VOIDP], rffi.CCHARP, error=0) def PyString_AS_STRING(space, void_ref): ref = rffi.cast(PyObject, void_ref) # if no w_str is associated with this ref, # return the c-level ptr as RW if not pyobj_has_w_obj(ref): py_str = rffi.cast(PyBytesObject, ref) return py_str.c_ob_sval return _PyString_AsString(space, ref) @cpython_api([PyObject, rffi.CCHARPP, rffi.CArrayPtr(Py_ssize_t)], rffi.INT_real, error=-1) def PyString_AsStringAndSize(space, ref, data, length): if not PyString_Check(space, ref): from pypy.module.cpyext.unicodeobject import ( PyUnicode_Check, _PyUnicode_AsDefaultEncodedString) if PyUnicode_Check(space, ref): ref = _PyUnicode_AsDefaultEncodedString( space, ref, lltype.nullptr(rffi.CCHARP.TO)) else: raise oefmt(space.w_TypeError, "expected string or Unicode object, %T found", from_ref(space, ref)) ref_str = rffi.cast(PyBytesObject, ref) data[0] = ref_str.c_ob_sval
def array_setitem_T(TYPE, width, addr, index, offset, value): addr = rffi.ptradd(addr, index * width) addr = rffi.ptradd(addr, offset) rffi.cast(rffi.CArrayPtr(TYPE), addr)[0] = value
pypysig_getaddr_occurred = external('pypysig_getaddr_occurred', [], lltype.Ptr(LONG_STRUCT), _nowrapper=True, elidable_function=True) pypysig_check_and_reset = external('pypysig_check_and_reset', [], lltype.Bool, _nowrapper=True) c_alarm = external('alarm', [rffi.INT], rffi.INT) c_pause = external('pause', [], rffi.INT, releasegil=True) c_siginterrupt = external('siginterrupt', [rffi.INT, rffi.INT], rffi.INT, save_err=rffi.RFFI_SAVE_ERRNO) if sys.platform != 'win32': itimervalP = rffi.CArrayPtr(itimerval) c_setitimer = external('setitimer', [rffi.INT, itimervalP, itimervalP], rffi.INT, save_err=rffi.RFFI_SAVE_ERRNO) c_getitimer = external('getitimer', [rffi.INT, itimervalP], rffi.INT) c_pthread_kill = external('pthread_kill', [lltype.Signed, rffi.INT], rffi.INT, save_err=rffi.RFFI_SAVE_ERRNO) if sys.platform != 'win32': c_sigset_t = rffi.COpaquePtr('sigset_t', compilation_info=eci) c_sigemptyset = external('sigemptyset', [c_sigset_t], rffi.INT) c_sigaddset = external('sigaddset', [c_sigset_t, rffi.INT], rffi.INT) c_sigismember = external('sigismember', [c_sigset_t, rffi.INT], rffi.INT) c_sigwait = external('sigwait', [c_sigset_t, rffi.INTP],
HAVE_DIRENT_H = platform.Has("opendir") config = platform.configure(CConfig) if config['HAVE_DIRENT_H']: compile_extra.append("-DHAVE_DIRENT_H") eci = eci.merge(rposix.eci_inheritable, ExternalCompilationInfo(compile_extra=compile_extra)) c_child_exec = rffi.llexternal('pypy_subprocess_child_exec', [ rffi.CCHARPP, rffi.CCHARPP, rffi.CCHARPP, rffi.CCHARP, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.CArrayPtr(rffi.LONG), lltype.Signed, lltype.Ptr(lltype.FuncType([rffi.VOIDP], rffi.INT)), rffi.VOIDP ], lltype.Void, compilation_info=eci, releasegil=True) c_init = rffi.llexternal('pypy_subprocess_init', [], lltype.Void, compilation_info=eci, releasegil=True) class PreexecCallback: def __init__(self): self.space = None self.w_preexec_fn = None
for k, v in rffi_platform.configure(CConfig).items(): globals()[k] = v # opaque structures SSL_METHOD = rffi.COpaquePtr('SSL_METHOD') SSL_CTX = rffi.COpaquePtr('SSL_CTX') SSL_CIPHER = rffi.COpaquePtr('SSL_CIPHER') SSL = rffi.COpaquePtr('SSL') BIO = rffi.COpaquePtr('BIO') X509 = rffi.COpaquePtr('X509') X509_OBJECT = rffi.COpaquePtr('X509_OBJECT') COMP_METHOD = rffi.COpaquePtr('COMP_METHOD') X509_NAME_ENTRY = rffi.COpaquePtr('X509_NAME_ENTRY') X509_EXTENSION = rffi.COpaquePtr('X509_EXTENSION') X509_STORE = rffi.COpaquePtr('X509_STORE') X509V3_EXT_METHOD = rffi.CArrayPtr(v3_ext_method) ASN1_STRING.TO.become(asn1_string_st) ASN1_TIME = rffi.COpaquePtr('ASN1_TIME') ASN1_INTEGER = rffi.COpaquePtr('ASN1_INTEGER') GENERAL_NAMES = rffi.COpaquePtr('GENERAL_NAMES') GENERAL_NAME.TO.become(GENERAL_NAME_st) OBJ_NAME = rffi.CArrayPtr(OBJ_NAME_st) ACCESS_DESCRIPTION = rffi.CArrayPtr(ACCESS_DESCRIPTION_st) HAVE_OPENSSL_RAND = OPENSSL_VERSION_NUMBER >= 0x0090500f HAVE_OPENSSL_FINISHED = OPENSSL_VERSION_NUMBER >= 0x0090500f HAVE_SSL_CTX_CLEAR_OPTIONS = OPENSSL_VERSION_NUMBER >= 0x009080df and \ OPENSSL_VERSION_NUMBER != 0x00909000 if OPENSSL_VERSION_NUMBER < 0x0090800f and not OPENSSL_NO_ECDH: OPENSSL_NO_ECDH = True HAS_ALPN = OPENSSL_VERSION_NUMBER >= 0x1000200fL and not OPENSSL_NO_TLSEXT
return simple_new(space, nd, dims, typenum, order=order, owning=owning, w_subtype=w_subtype) gufunctype = lltype.Ptr(ufuncs.GenericUfunc) # XXX single rffi.CArrayPtr(gufunctype) does not work, this does, is there # a problem with casting function pointers? @cpython_api([ rffi.CArrayPtr(rffi.CArrayPtr(gufunctype)), rffi.VOIDP, rffi.CCHARP, Py_ssize_t, Py_ssize_t, Py_ssize_t, Py_ssize_t, rffi.CCHARP, rffi.CCHARP, Py_ssize_t, rffi.CCHARP ], PyObject) def PyUFunc_FromFuncAndDataAndSignature(space, funcs, data, types, ntypes, nin, nout, identity, name, doc, check_return, signature): w_signature = rffi.charp2str(signature) return do_ufunc(space, funcs, data, types, ntypes, nin, nout, identity, name, doc, check_return, w_signature) def do_ufunc(space, funcs, data, types, ntypes, nin, nout, identity, name, doc, check_return, w_signature): funcs_w = [None] * ntypes dtypes_w = [None] * ntypes * (nin + nout)