コード例 #1
0
 def test_fsconverter(self, space):
     # Input is bytes
     w_input = space.newbytes("test")
     with lltype.scoped_alloc(PyObjectP.TO, 1) as result:
         # Decoder
         ret = PyUnicode_FSDecoder(space, w_input, result)
         assert ret == Py_CLEANUP_SUPPORTED
         assert space.isinstance_w(from_ref(space, result[0]),
                                   space.w_unicode)
         assert PyUnicode_FSDecoder(space, None, result) == 1
         # Converter
         ret = PyUnicode_FSConverter(space, w_input, result)
         assert ret == Py_CLEANUP_SUPPORTED
         assert space.eq_w(from_ref(space, result[0]), w_input)
         assert PyUnicode_FSDecoder(space, None, result) == 1
     # Input is unicode
     w_input = space.wrap("test")
     with lltype.scoped_alloc(PyObjectP.TO, 1) as result:
         # Decoder
         ret = PyUnicode_FSDecoder(space, w_input, result)
         assert ret == Py_CLEANUP_SUPPORTED
         assert space.eq_w(from_ref(space, result[0]), w_input)
         assert PyUnicode_FSDecoder(space, None, result) == 1
         # Converter
         ret = PyUnicode_FSConverter(space, w_input, result)
         assert ret == Py_CLEANUP_SUPPORTED
         assert space.isinstance_w(from_ref(space, result[0]),
                                   space.w_bytes)
         assert PyUnicode_FSDecoder(space, None, result) == 1
     # Input is invalid
     w_input = space.newint(42)
     with lltype.scoped_alloc(PyObjectP.TO, 1) as result:
         with pytest.raises(OperationError):
             PyUnicode_FSConverter(space, w_input, result)
コード例 #2
0
def ConnectRegistry(space, w_machine, w_hkey):
    """
key = ConnectRegistry(computer_name, key)

Establishes a connection to a predefined registry handle on another computer.

computer_name is the name of the remote computer, of the form \\\\computername.
 If None, the local computer is used.
key is the predefined handle to connect to.

The return value is the handle of the opened key.
If the function fails, an EnvironmentError exception is raised."""
    hkey = hkey_w(w_hkey, space)
    if space.is_none(w_machine):
        with lltype.scoped_alloc(rwinreg.PHKEY.TO, 1) as rethkey:
            ret = rwinreg.RegConnectRegistryW(None, hkey, rethkey)
            if ret != 0:
                raiseWindowsError(space, ret, 'RegConnectRegistry')
            return W_HKEY(space, rethkey[0])
    else:
        utf8 = space.utf8_w(w_machine)
        state = space.fromcache(CodecState)
        errh = state.encode_error_handler
        machineW = utf8_encode_utf_16(utf8 + '\x00',
                                      'strict',
                                      errh,
                                      allow_surrogates=False)
        with rffi.scoped_nonmovingbuffer(machineW) as machineP0:
            machineP = rffi.cast(rwin32.LPWSTR, rffi.ptradd(machineP0, 2))
            with lltype.scoped_alloc(rwinreg.PHKEY.TO, 1) as rethkey:
                ret = rwinreg.RegConnectRegistryW(machineP, hkey, rethkey)
            if ret != 0:
                raiseWindowsError(space, ret, 'RegConnectRegistry')
            return W_HKEY(space, rethkey[0])
コード例 #3
0
ファイル: interp_winreg.py プロジェクト: tools-env/mesapy
def EnumKey(space, w_hkey, index):
    """string = EnumKey(key, index) - Enumerates subkeys of an open registry key.

key is an already open key, or any one of the predefined HKEY_* constants.
index is an integer that identifies the index of the key to retrieve.

The function retrieves the name of one subkey each time it is called.
It is typically called repeatedly until an EnvironmentError exception is
raised, indicating no more values are available."""
    hkey = hkey_w(w_hkey, space)
    null_dword = lltype.nullptr(rwin32.LPDWORD.TO)

    # The Windows docs claim that the max key name length is 255
    # characters, plus a terminating nul character.  However,
    # empirical testing demonstrates that it is possible to
    # create a 256 character key that is missing the terminating
    # nul.  RegEnumKeyEx requires a 257 character buffer to
    # retrieve such a key name.
    with lltype.scoped_alloc(rffi.CCHARP.TO, 257) as buf:
        with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as retValueSize:
            retValueSize[0] = r_uint(257)  # includes NULL terminator
            ret = rwinreg.RegEnumKeyExA(hkey, index, buf, retValueSize,
                                        null_dword, None, null_dword,
                                        lltype.nullptr(rwin32.PFILETIME.TO))
            if ret != 0:
                raiseWindowsError(space, ret, 'RegEnumKeyEx')
            return space.newtext(rffi.charp2str(buf))
コード例 #4
0
ファイル: interpreter.py プロジェクト: softdevteam/llrm
def create_module(filename):
    ''' Returns the W_Module representing an LLVM module created with the
        contents of the given file. '''

    module = llwrap.LLVMModuleCreateWithName("module")
    with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as out_message:
        with lltype.scoped_alloc(rffi.VOIDP.TO, 1) as mem_buff:
            with lltype.scoped_alloc(rffi.VOIDPP.TO, 1) as mem_buff_ptr:
                mem_buff_ptr[0] = mem_buff
                rc = llwrap.LLVMCreateMemoryBufferWithContentsOfFile(filename,
                                                                     mem_buff_ptr,
                                                                     out_message)
                if rc != 0:
                    print "[ERROR]: Cannot create memory buffer with contents of"\
                          " %s: %s.\n" % (filename, rffi.charp2str(out_message[0]))
                    raise InvalidFileException(filename)
                mem_buff = mem_buff_ptr[0]
            with lltype.scoped_alloc(rffi.VOIDPP.TO, 1) as module_ptr:
                module_ptr[0] = module
                rc = llwrap.LLVMParseBitcode(mem_buff, module_ptr, out_message)
                if rc != 0:
                    print "[ERROR]: Cannot parse %s: %s.\n" % (filename,
                                                               rffi.charp2str(out_message[0]))
                    raise UnparsableBitcodeException(filename)
                module = module_ptr[0]
    return W_Module(module)
コード例 #5
0
def create_module(filename):
    ''' Returns the W_Module representing an LLVM module created with the
        contents of the given file. '''

    module = llwrap.LLVMModuleCreateWithName("module")
    with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as out_message:
        with lltype.scoped_alloc(rffi.VOIDP.TO, 1) as mem_buff:
            with lltype.scoped_alloc(rffi.VOIDPP.TO, 1) as mem_buff_ptr:
                mem_buff_ptr[0] = mem_buff
                rc = llwrap.LLVMCreateMemoryBufferWithContentsOfFile(
                    filename, mem_buff_ptr, out_message)
                if rc != 0:
                    print "[ERROR]: Cannot create memory buffer with contents of"\
                          " %s: %s.\n" % (filename, rffi.charp2str(out_message[0]))
                    raise InvalidFileException(filename)
                mem_buff = mem_buff_ptr[0]
            with lltype.scoped_alloc(rffi.VOIDPP.TO, 1) as module_ptr:
                module_ptr[0] = module
                rc = llwrap.LLVMParseBitcode(mem_buff, module_ptr, out_message)
                if rc != 0:
                    print "[ERROR]: Cannot parse %s: %s.\n" % (
                        filename, rffi.charp2str(out_message[0]))
                    raise UnparsableBitcodeException(filename)
                module = module_ptr[0]
    return W_Module(module)
コード例 #6
0
ファイル: interp_win32.py プロジェクト: mozillazg/pypy
def w_parseKeyUsage(space, pCertCtx, flags):
    with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as size_ptr:
        if not CertGetEnhancedKeyUsage(pCertCtx, flags,
                             lltype.nullptr(CERT_ENHKEY_USAGE), size_ptr):
            last_error = rwin32.lastSavedWindowsError()
            if last_error.winerror == CRYPT_E_NOT_FOUND:
                return space.w_True
            raise wrap_windowserror(space, last_error)

        size = intmask(size_ptr[0])
        with lltype.scoped_alloc(rffi.CCHARP.TO, size) as buf:
            usage = rffi.cast(PCERT_ENHKEY_USAGE, buf)
            # Now get the actual enhanced usage property
            if not CertGetEnhancedKeyUsage(pCertCtx, flags, usage, size_ptr):
                last_error= rwin32.lastSavedWindowsError()
                if last_error.winerror == CRYPT_E_NOT_FOUND:
                    return space.w_True
                raise wrap_windowserror(space, last_error)

            result_w = [None] * usage.c_cUsageIdentifier
            for i in range(usage.c_cUsageIdentifier):
                if not usage.c_rgpszUsageIdentifier[i]:
                    continue
                result_w[i] = space.wrap(rffi.charp2str(
                    usage.c_rgpszUsageIdentifier[i]))
            return space.newset(result_w)
コード例 #7
0
ファイル: interp_winreg.py プロジェクト: tools-env/mesapy
def QueryInfoKey(space, w_hkey):
    """tuple = QueryInfoKey(key) - Returns information about a key.

key is an already open key, or any one of the predefined HKEY_* constants.

The result is a tuple of 3 items:
An integer that identifies the number of sub keys this key has.
An integer that identifies the number of values this key has.
A long integer that identifies when the key was last modified (if available)
 as 100's of nanoseconds since Jan 1, 1600."""
    hkey = hkey_w(w_hkey, space)
    with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as nSubKeys:
        with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as nValues:
            with lltype.scoped_alloc(rwin32.PFILETIME.TO, 1) as ft:
                null_dword = lltype.nullptr(rwin32.LPDWORD.TO)
                ret = rwinreg.RegQueryInfoKeyA(hkey, None, null_dword,
                                               null_dword, nSubKeys,
                                               null_dword, null_dword, nValues,
                                               null_dword, null_dword,
                                               null_dword, ft)
                if ret != 0:
                    raiseWindowsError(space, ret, 'RegQueryInfoKey')
                l = ((lltype.r_longlong(ft[0].c_dwHighDateTime) << 32) +
                     lltype.r_longlong(ft[0].c_dwLowDateTime))
                return space.newtuple([
                    space.newint(nSubKeys[0]),
                    space.newint(nValues[0]),
                    space.newint(l)
                ])
コード例 #8
0
ファイル: interp_epoll.py プロジェクト: Mu-L/pypy
    def descr_poll(self, space, timeout=-1.0, maxevents=-1):
        self.check_closed(space)
        if timeout < 0:
            timeout = -1.0
        else:
            timeout *= 1000.0

        if maxevents == -1:
            maxevents = FD_SETSIZE - 1
        elif maxevents < 1:
            raise oefmt(space.w_ValueError,
                        "maxevents must be greater than 0, not %d", maxevents)

        with lltype.scoped_alloc(rffi.CArray(rffi.UINT), maxevents) as fids:
            with lltype.scoped_alloc(rffi.CArray(rffi.INT),
                                     maxevents) as events:
                nfds = pypy_epoll_wait(self.epfd, fids, events, maxevents,
                                       int(timeout))
                if nfds < 0:
                    raise exception_from_saved_errno(space, space.w_IOError)

                elist_w = [None] * nfds
                for i in xrange(nfds):
                    elist_w[i] = space.newtuple(
                        [space.newint(fids[i]),
                         space.newint(events[i])])
                return space.newlist(elist_w)
コード例 #9
0
ファイル: test_rzlib.py プロジェクト: abhinavthomas/pypy
def test_deflate_set_dictionary():
    text = 'abcabc'
    zdict = 'abc'
    stream = rzlib.deflateInit()
    rzlib.deflateSetDictionary(stream, zdict)
    bytes = rzlib.compress(stream, text, rzlib.Z_FINISH)
    rzlib.deflateEnd(stream)
    
    stream2 = rzlib.inflateInit()

    from rpython.rtyper.lltypesystem import lltype, rffi, rstr
    from rpython.rtyper.annlowlevel import llstr
    from rpython.rlib.rstring import StringBuilder
    with lltype.scoped_alloc(rffi.CCHARP.TO, len(bytes)) as inbuf:
        rstr.copy_string_to_raw(llstr(bytes), inbuf, 0, len(bytes))
        stream2.c_next_in = rffi.cast(rzlib.Bytefp, inbuf)
        rffi.setintfield(stream2, 'c_avail_in', len(bytes))
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as outbuf:
            stream2.c_next_out = rffi.cast(rzlib.Bytefp, outbuf)
            bufsize = 100
            rffi.setintfield(stream2, 'c_avail_out', bufsize)
            err = rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH)
            assert err == rzlib.Z_NEED_DICT
            rzlib.inflateSetDictionary(stream2, zdict)
            rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH)
            avail_out = rffi.cast(lltype.Signed, stream2.c_avail_out)
            result = StringBuilder()
            result.append_charpsize(outbuf, bufsize - avail_out)

    rzlib.inflateEnd(stream2)
    assert result.build() == text
コード例 #10
0
    def gettimeofday(space, w_info=None):
        with lltype.scoped_alloc(rwin32.FILETIME) as system_time:
            _GetSystemTimeAsFileTime(system_time)
            quad_part = (system_time.c_dwLowDateTime |
                         (r_ulonglong(system_time.c_dwHighDateTime) << 32))
            # 11,644,473,600,000,000: number of microseconds between
            # the 1st january 1601 and the 1st january 1970 (369 years + 80 leap
            # days).

            # We can't use that big number when translating for
            # 32-bit system (which windows always is currently)
            # XXX: Need to come up with a better solution
            offset = (r_ulonglong(16384) * r_ulonglong(27) *
                      r_ulonglong(390625) * r_ulonglong(79) * r_ulonglong(853))
            microseconds = quad_part / 10 - offset
            tv_sec = microseconds / 1000000
            tv_usec = microseconds % 1000000
            if w_info:
                with lltype.scoped_alloc(LPDWORD.TO, 1) as time_adjustment, \
                     lltype.scoped_alloc(LPDWORD.TO, 1) as time_increment, \
                     lltype.scoped_alloc(rwin32.LPBOOL.TO, 1) as is_time_adjustment_disabled:
                    _GetSystemTimeAdjustment(time_adjustment, time_increment,
                                             is_time_adjustment_disabled)

                    _setinfo(space, w_info, "GetSystemTimeAsFileTime()",
                             time_increment[0] * 1e-7, False, True)
            return space.newfloat(tv_sec + tv_usec * 1e-6)
コード例 #11
0
 def gettimeofday(space, w_info=None):
     if HAVE_GETTIMEOFDAY:
         with lltype.scoped_alloc(TIMEVAL) as timeval:
             if GETTIMEOFDAY_NO_TZ:
                 errcode = c_gettimeofday(timeval)
             else:
                 void = lltype.nullptr(rffi.VOIDP.TO)
                 errcode = c_gettimeofday(timeval, void)
             if rffi.cast(rffi.LONG, errcode) == 0:
                 if w_info is not None:
                     _setinfo(space, w_info, "gettimeofday()", 1e-6, False,
                              True)
                 return space.newfloat(
                     widen(timeval.c_tv_sec) +
                     widen(timeval.c_tv_usec) * 1e-6)
     if HAVE_FTIME:
         with lltype.scoped_alloc(TIMEB) as t:
             c_ftime(t)
             result = (widen(t.c_time) + widen(t.c_millitm) * 0.001)
             if w_info is not None:
                 _setinfo(space, w_info, "ftime()", 1e-3, False, True)
         return space.newfloat(result)
     else:
         if w_info:
             _setinfo(space, w_info, "time()", 1.0, False, True)
         return space.newint(c_time(lltype.nullptr(rffi.TIME_TP.TO)))
コード例 #12
0
ファイル: interp_winreg.py プロジェクト: abhinavthomas/pypy
def QueryInfoKey(space, w_hkey):
    """tuple = QueryInfoKey(key) - Returns information about a key.

key is an already open key, or any one of the predefined HKEY_* constants.

The result is a tuple of 3 items:
An integer that identifies the number of sub keys this key has.
An integer that identifies the number of values this key has.
A long integer that identifies when the key was last modified (if available)
 as 100's of nanoseconds since Jan 1, 1600."""
    hkey = hkey_w(w_hkey, space)
    with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as nSubKeys:
        with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as nValues:
            with lltype.scoped_alloc(rwin32.PFILETIME.TO, 1) as ft:
                null_dword = lltype.nullptr(rwin32.LPDWORD.TO)
                ret = rwinreg.RegQueryInfoKey(
                    hkey, None, null_dword, null_dword,
                    nSubKeys, null_dword, null_dword,
                    nValues, null_dword, null_dword,
                    null_dword, ft)
                if ret != 0:
                    raiseWindowsError(space, ret, 'RegQueryInfoKey')
                l = ((lltype.r_longlong(ft[0].c_dwHighDateTime) << 32) +
                     lltype.r_longlong(ft[0].c_dwLowDateTime))
                return space.newtuple([space.wrap(nSubKeys[0]),
                                       space.wrap(nValues[0]),
                                       space.wrap(l)])
コード例 #13
0
 def test_fsconverter(self, space, api):
     # Input is bytes
     w_input = space.wrapbytes("test")
     with lltype.scoped_alloc(PyObjectP.TO, 1) as result:
         # Decoder
         ret = api.PyUnicode_FSDecoder(w_input, result)
         assert ret == Py_CLEANUP_SUPPORTED
         assert space.isinstance_w(from_ref(space, result[0]), space.w_unicode)
         assert api.PyUnicode_FSDecoder(None, result) == 1
         # Converter
         ret = api.PyUnicode_FSConverter(w_input, result)
         assert ret == Py_CLEANUP_SUPPORTED
         assert space.eq_w(from_ref(space, result[0]), w_input)
         assert api.PyUnicode_FSDecoder(None, result) == 1
     # Input is unicode
     w_input = space.wrap("test")
     with lltype.scoped_alloc(PyObjectP.TO, 1) as result:
         # Decoder
         ret = api.PyUnicode_FSDecoder(w_input, result)
         assert ret == Py_CLEANUP_SUPPORTED
         assert space.eq_w(from_ref(space, result[0]), w_input)
         assert api.PyUnicode_FSDecoder(None, result) == 1
         # Converter
         ret = api.PyUnicode_FSConverter(w_input, result)
         assert ret == Py_CLEANUP_SUPPORTED
         assert space.isinstance_w(from_ref(space, result[0]), space.w_bytes)
         assert api.PyUnicode_FSDecoder(None, result) == 1
コード例 #14
0
ファイル: test_rzlib.py プロジェクト: soIu/rpython
def test_deflate_set_dictionary():
    text = 'abcabc'
    zdict = 'abc'
    stream = rzlib.deflateInit()
    rzlib.deflateSetDictionary(stream, zdict)
    bytes = rzlib.compress(stream, text, rzlib.Z_FINISH)
    rzlib.deflateEnd(stream)

    stream2 = rzlib.inflateInit()

    from rpython.rtyper.lltypesystem import lltype, rffi, rstr
    from rpython.rtyper.annlowlevel import llstr
    from rpython.rlib.rstring import StringBuilder
    with lltype.scoped_alloc(rffi.CCHARP.TO, len(bytes)) as inbuf:
        rstr.copy_string_to_raw(llstr(bytes), inbuf, 0, len(bytes))
        stream2.c_next_in = rffi.cast(rzlib.Bytefp, inbuf)
        rffi.setintfield(stream2, 'c_avail_in', len(bytes))
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as outbuf:
            stream2.c_next_out = rffi.cast(rzlib.Bytefp, outbuf)
            bufsize = 100
            rffi.setintfield(stream2, 'c_avail_out', bufsize)
            err = rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH)
            assert err == rzlib.Z_NEED_DICT
            rzlib.inflateSetDictionary(stream2, zdict)
            rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH)
            avail_out = rffi.cast(lltype.Signed, stream2.c_avail_out)
            result = StringBuilder()
            result.append_charpsize(outbuf, bufsize - avail_out)

    rzlib.inflateEnd(stream2)
    assert result.build() == text
コード例 #15
0
def w_parseKeyUsage(space, pCertCtx, flags):
    with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as size_ptr:
        if not CertGetEnhancedKeyUsage(
                pCertCtx, flags, lltype.nullptr(CERT_ENHKEY_USAGE), size_ptr):
            last_error = rwin32.lastSavedWindowsError()
            if last_error.winerror == CRYPT_E_NOT_FOUND:
                return space.w_True
            raise wrap_windowserror(space, last_error)

        size = intmask(size_ptr[0])
        with lltype.scoped_alloc(rffi.CCHARP.TO, size) as buf:
            usage = rffi.cast(PCERT_ENHKEY_USAGE, buf)
            # Now get the actual enhanced usage property
            if not CertGetEnhancedKeyUsage(pCertCtx, flags, usage, size_ptr):
                last_error = rwin32.lastSavedWindowsError()
                if last_error.winerror == CRYPT_E_NOT_FOUND:
                    return space.w_True
                raise wrap_windowserror(space, last_error)

            result_w = [None] * usage.c_cUsageIdentifier
            for i in range(usage.c_cUsageIdentifier):
                if not usage.c_rgpszUsageIdentifier[i]:
                    continue
                result_w[i] = space.wrap(
                    rffi.charp2str(usage.c_rgpszUsageIdentifier[i]))
            return space.newset(result_w)
コード例 #16
0
ファイル: rzlib.py プロジェクト: charred/pypy
def _operate(stream, data, flush, max_length, cfunc, while_doing):
    """Common code for compress() and decompress().
    """
    # Prepare the input buffer for the stream
    with lltype.scoped_alloc(rffi.CCHARP.TO, len(data)) as inbuf:
        for i in xrange(len(data)):
            inbuf[i] = data[i]
        stream.c_next_in = rffi.cast(Bytefp, inbuf)
        rffi.setintfield(stream, 'c_avail_in', len(data))

        # Prepare the output buffer
        with lltype.scoped_alloc(rffi.CCHARP.TO, OUTPUT_BUFFER_SIZE) as outbuf:
            # Strategy: we call deflate() to get as much output data as fits in
            # the buffer, then accumulate all output into a StringBuffer
            # 'result'.
            result = StringBuilder()

            while True:
                stream.c_next_out = rffi.cast(Bytefp, outbuf)
                bufsize = OUTPUT_BUFFER_SIZE
                if max_length < bufsize:
                    if max_length <= 0:
                        err = Z_OK
                        break
                    bufsize = max_length
                max_length -= bufsize
                rffi.setintfield(stream, 'c_avail_out', bufsize)
                err = cfunc(stream, flush)
                if err == Z_OK or err == Z_STREAM_END:
                    # accumulate data into 'result'
                    avail_out = rffi.cast(lltype.Signed, stream.c_avail_out)
                    result.append_charpsize(outbuf, bufsize - avail_out)
                    # if the output buffer is full, there might be more data
                    # so we need to try again.  Otherwise, we're done.
                    if avail_out > 0:
                        break
                    # We're also done if we got a Z_STREAM_END (which should
                    # only occur when flush == Z_FINISH).
                    if err == Z_STREAM_END:
                        break
                    else:
                        continue
                elif err == Z_BUF_ERROR:
                    avail_out = rffi.cast(lltype.Signed, stream.c_avail_out)
                    # When compressing, we will only get Z_BUF_ERROR if
                    # the output buffer was full but there wasn't more
                    # output when we tried again, so it is not an error
                    # condition.
                    if avail_out == bufsize:
                        break

                # fallback case: report this error
                raise RZlibError.fromstream(stream, err, while_doing)

    # When decompressing, if the compressed stream of data was truncated,
    # then the zlib simply returns Z_OK and waits for more.  If it is
    # complete it returns Z_STREAM_END.
    return (result.build(),
            err,
            rffi.cast(lltype.Signed, stream.c_avail_in))
コード例 #17
0
ファイル: interp_winreg.py プロジェクト: abhinavthomas/pypy
def EnumKey(space, w_hkey, index):
    """string = EnumKey(key, index) - Enumerates subkeys of an open registry key.

key is an already open key, or any one of the predefined HKEY_* constants.
index is an integer that identifies the index of the key to retrieve.

The function retrieves the name of one subkey each time it is called.
It is typically called repeatedly until an EnvironmentError exception is
raised, indicating no more values are available."""
    hkey = hkey_w(w_hkey, space)
    null_dword = lltype.nullptr(rwin32.LPDWORD.TO)

    # The Windows docs claim that the max key name length is 255
    # characters, plus a terminating nul character.  However,
    # empirical testing demonstrates that it is possible to
    # create a 256 character key that is missing the terminating
    # nul.  RegEnumKeyEx requires a 257 character buffer to
    # retrieve such a key name.
    with lltype.scoped_alloc(rffi.CCHARP.TO, 257) as buf:
        with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as retValueSize:
            retValueSize[0] = r_uint(257) # includes NULL terminator
            ret = rwinreg.RegEnumKeyEx(hkey, index, buf, retValueSize,
                                       null_dword, None, null_dword,
                                       lltype.nullptr(rwin32.PFILETIME.TO))
            if ret != 0:
                raiseWindowsError(space, ret, 'RegEnumKeyEx')
            return space.wrap(rffi.charp2str(buf))
コード例 #18
0
ファイル: rzlib.py プロジェクト: juokaz/pypy
def _operate(stream, data, flush, max_length, cfunc, while_doing):
    """Common code for compress() and decompress().
    """
    # Prepare the input buffer for the stream
    with lltype.scoped_alloc(rffi.CCHARP.TO, len(data)) as inbuf:
        # XXX (groggi) should be possible to improve this with pinning by
        # not performing the 'copy_string_to_raw' if non-movable/pinned
        copy_string_to_raw(llstr(data), inbuf, 0, len(data))
        stream.c_next_in = rffi.cast(Bytefp, inbuf)
        rffi.setintfield(stream, 'c_avail_in', len(data))

        # Prepare the output buffer
        with lltype.scoped_alloc(rffi.CCHARP.TO, OUTPUT_BUFFER_SIZE) as outbuf:
            # Strategy: we call deflate() to get as much output data as fits in
            # the buffer, then accumulate all output into a StringBuffer
            # 'result'.
            result = StringBuilder()

            while True:
                stream.c_next_out = rffi.cast(Bytefp, outbuf)
                bufsize = OUTPUT_BUFFER_SIZE
                if max_length < bufsize:
                    if max_length <= 0:
                        err = Z_OK
                        break
                    bufsize = max_length
                max_length -= bufsize
                rffi.setintfield(stream, 'c_avail_out', bufsize)
                err = cfunc(stream, flush)
                if err == Z_OK or err == Z_STREAM_END:
                    # accumulate data into 'result'
                    avail_out = rffi.cast(lltype.Signed, stream.c_avail_out)
                    result.append_charpsize(outbuf, bufsize - avail_out)
                    # if the output buffer is full, there might be more data
                    # so we need to try again.  Otherwise, we're done.
                    if avail_out > 0:
                        break
                    # We're also done if we got a Z_STREAM_END (which should
                    # only occur when flush == Z_FINISH).
                    if err == Z_STREAM_END:
                        break
                    else:
                        continue
                elif err == Z_BUF_ERROR:
                    avail_out = rffi.cast(lltype.Signed, stream.c_avail_out)
                    # When compressing, we will only get Z_BUF_ERROR if
                    # the output buffer was full but there wasn't more
                    # output when we tried again, so it is not an error
                    # condition.
                    if avail_out == bufsize:
                        break

                # fallback case: report this error
                raise RZlibError.fromstream(stream, err, while_doing)

    # When decompressing, if the compressed stream of data was truncated,
    # then the zlib simply returns Z_OK and waits for more.  If it is
    # complete it returns Z_STREAM_END.
    return (result.build(), err, rffi.cast(lltype.Signed, stream.c_avail_in))
コード例 #19
0
ファイル: interp_ssl.py プロジェクト: charred/pypy
def _decode_certificate(space, certificate, verbose=False):
    w_retval = space.newdict()

    w_peer = _create_tuple_for_X509_NAME(
        space, libssl_X509_get_subject_name(certificate))
    space.setitem(w_retval, space.wrap("subject"), w_peer)

    if verbose:
        w_issuer = _create_tuple_for_X509_NAME(
            space, libssl_X509_get_issuer_name(certificate))
        space.setitem(w_retval, space.wrap("issuer"), w_issuer)

        space.setitem(w_retval, space.wrap("version"),
                      space.wrap(libssl_X509_get_version(certificate)))

    biobuf = libssl_BIO_new(libssl_BIO_s_mem())
    try:

        if verbose:
            libssl_BIO_reset(biobuf)
            serialNumber = libssl_X509_get_serialNumber(certificate)
            libssl_i2a_ASN1_INTEGER(biobuf, serialNumber)
            # should not exceed 20 octets, 160 bits, so buf is big enough
            with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
                length = libssl_BIO_gets(biobuf, buf, 99)
                if length < 0:
                    raise _ssl_seterror(space, None, length)

                w_serial = space.wrap(rffi.charpsize2str(buf, length))
            space.setitem(w_retval, space.wrap("serialNumber"), w_serial)

            libssl_BIO_reset(biobuf)
            notBefore = libssl_X509_get_notBefore(certificate)
            libssl_ASN1_TIME_print(biobuf, notBefore)
            with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
                length = libssl_BIO_gets(biobuf, buf, 99)
                if length < 0:
                    raise _ssl_seterror(space, None, length)
                w_date = space.wrap(rffi.charpsize2str(buf, length))
            space.setitem(w_retval, space.wrap("notBefore"), w_date)

        libssl_BIO_reset(biobuf)
        notAfter = libssl_X509_get_notAfter(certificate)
        libssl_ASN1_TIME_print(biobuf, notAfter)
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
            length = libssl_BIO_gets(biobuf, buf, 99)
            if length < 0:
                raise _ssl_seterror(space, None, length)
            w_date = space.wrap(rffi.charpsize2str(buf, length))
        space.setitem(w_retval, space.wrap("notAfter"), w_date)
    finally:
        libssl_BIO_free(biobuf)

    # Now look for subjectAltName
    w_alt_names = _get_peer_alt_names(space, certificate)
    if w_alt_names is not space.w_None:
        space.setitem(w_retval, space.wrap("subjectAltName"), w_alt_names)

    return w_retval
コード例 #20
0
def _decode_certificate(space, certificate, verbose=False):
    w_retval = space.newdict()

    w_peer = _create_tuple_for_X509_NAME(
        space, libssl_X509_get_subject_name(certificate))
    space.setitem(w_retval, space.wrap("subject"), w_peer)

    if verbose:
        w_issuer = _create_tuple_for_X509_NAME(
            space, libssl_X509_get_issuer_name(certificate))
        space.setitem(w_retval, space.wrap("issuer"), w_issuer)

        space.setitem(w_retval, space.wrap("version"),
                      space.wrap(libssl_X509_get_version(certificate)))

    biobuf = libssl_BIO_new(libssl_BIO_s_mem())
    try:

        if verbose:
            libssl_BIO_reset(biobuf)
            serialNumber = libssl_X509_get_serialNumber(certificate)
            libssl_i2a_ASN1_INTEGER(biobuf, serialNumber)
            # should not exceed 20 octets, 160 bits, so buf is big enough
            with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
                length = libssl_BIO_gets(biobuf, buf, 99)
                if length < 0:
                    raise _ssl_seterror(space, None, length)

                w_serial = space.wrap(rffi.charpsize2str(buf, length))
            space.setitem(w_retval, space.wrap("serialNumber"), w_serial)

        libssl_BIO_reset(biobuf)
        notBefore = libssl_X509_get_notBefore(certificate)
        libssl_ASN1_TIME_print(biobuf, notBefore)
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
            length = libssl_BIO_gets(biobuf, buf, 99)
            if length < 0:
                raise _ssl_seterror(space, None, length)
            w_date = space.wrap(rffi.charpsize2str(buf, length))
        space.setitem(w_retval, space.wrap("notBefore"), w_date)

        libssl_BIO_reset(biobuf)
        notAfter = libssl_X509_get_notAfter(certificate)
        libssl_ASN1_TIME_print(biobuf, notAfter)
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
            length = libssl_BIO_gets(biobuf, buf, 99)
            if length < 0:
                raise _ssl_seterror(space, None, length)
            w_date = space.wrap(rffi.charpsize2str(buf, length))
        space.setitem(w_retval, space.wrap("notAfter"), w_date)
    finally:
        libssl_BIO_free(biobuf)

    # Now look for subjectAltName
    w_alt_names = _get_peer_alt_names(space, certificate)
    if w_alt_names is not space.w_None:
        space.setitem(w_retval, space.wrap("subjectAltName"), w_alt_names)

    return w_retval
コード例 #21
0
ファイル: interp_hashlib.py プロジェクト: weijiwei/pypy
 def _digest(self, space):
     with lltype.scoped_alloc(ropenssl.EVP_MD_CTX.TO) as ctx:
         with self.lock:
             ropenssl.EVP_MD_CTX_copy(ctx, self.ctx)
         digest_size = self.digest_size
         with lltype.scoped_alloc(rffi.CCHARP.TO, digest_size) as digest:
             ropenssl.EVP_DigestFinal(ctx, digest, None)
             ropenssl.EVP_MD_CTX_cleanup(ctx)
             return rffi.charpsize2str(digest, digest_size)
コード例 #22
0
 def getWindowSize(self):
     from typhon.objects.data import wrapInt
     from typhon.objects.collections.lists import ConstList
     with scoped_alloc(INTP.TO, 1) as widthp, \
             scoped_alloc(INTP.TO, 1) as heightp:
         ruv.TTYGetWinSize(self._tty, widthp, heightp)
         width = intmask(widthp[0])
         height = intmask(heightp[0])
     return ConstList([wrapInt(width), wrapInt(height)])
コード例 #23
0
ファイル: interp_hashlib.py プロジェクト: bukzor/pypy
 def _digest(self, space):
     with lltype.scoped_alloc(ropenssl.EVP_MD_CTX.TO) as ctx:
         with self.lock:
             ropenssl.EVP_MD_CTX_copy(ctx, self.ctx)
         digest_size = self.digest_size
         with lltype.scoped_alloc(rffi.CCHARP.TO, digest_size) as digest:
             ropenssl.EVP_DigestFinal(ctx, digest, None)
             ropenssl.EVP_MD_CTX_cleanup(ctx)
             return rffi.charpsize2str(digest, digest_size)
コード例 #24
0
ファイル: streamcaps.py プロジェクト: monte-language/typhon
 def getWindowSize(self):
     from typhon.objects.data import wrapInt
     from typhon.objects.collections.lists import ConstList
     with scoped_alloc(INTP.TO, 1) as widthp, \
             scoped_alloc(INTP.TO, 1) as heightp:
         ruv.TTYGetWinSize(self._tty, widthp, heightp)
         width = intmask(widthp[0])
         height = intmask(heightp[0])
     return ConstList([wrapInt(width), wrapInt(height)])
コード例 #25
0
ファイル: rtime.py プロジェクト: sczfaker/pypy
def clock():
    if _WIN32:
        return win_perf_counter()
    elif HAS_CLOCK_GETTIME and CLOCK_PROCESS_CPUTIME_ID is not None:
        with lltype.scoped_alloc(TIMESPEC) as a:
            if c_clock_gettime(CLOCK_PROCESS_CPUTIME_ID, a) == 0:
                return (float(rffi.getintfield(a, 'c_tv_sec')) +
                        float(rffi.getintfield(a, 'c_tv_nsec')) * 0.000000001)
    with lltype.scoped_alloc(RUSAGE) as a:
        c_getrusage(RUSAGE_SELF, a)
        result = (decode_timeval(a.c_ru_utime) + decode_timeval(a.c_ru_stime))
    return result
コード例 #26
0
ファイル: rtime.py プロジェクト: mozillazg/pypy
def clock():
    if _WIN32:
        return win_perf_counter()
    elif HAS_CLOCK_GETTIME and CLOCK_PROCESS_CPUTIME_ID is not None:
        with lltype.scoped_alloc(TIMESPEC) as a:
            if c_clock_gettime(CLOCK_PROCESS_CPUTIME_ID, a) == 0:
                return (float(rffi.getintfield(a, 'c_tv_sec')) +
                        float(rffi.getintfield(a, 'c_tv_nsec')) * 0.000000001)
    with lltype.scoped_alloc(RUSAGE) as a:
        c_getrusage(RUSAGE_SELF, a)
        result = (decode_timeval(a.c_ru_utime) +
                  decode_timeval(a.c_ru_stime))
    return result
コード例 #27
0
ファイル: test_setobject.py プロジェクト: zcxowwww/pypy
 def test_pyset_next(self, space, api):
     w_set = space.call_function(space.w_set, space.newtext("ab"))
     with lltype.scoped_alloc(Py_ssize_tP.TO, 1) as pos_p:
         with lltype.scoped_alloc(PyObjectP.TO, 1) as result_p:
             pos_p[0] = 0
             res = api._PySet_Next(w_set, pos_p, result_p)
             assert res == 1
             letter1 = space.text_w(from_ref(space, result_p[0]))
             res = api._PySet_Next(w_set, pos_p, result_p)
             assert res == 1
             letter2 = space.text_w(from_ref(space, result_p[0]))
             res = api._PySet_Next(w_set, pos_p, result_p)
             assert res == 0
     assert set([letter1, letter2]) == set("ab")
コード例 #28
0
ファイル: timeobject.py プロジェクト: topazproject/topaz
def strftime(format, seconds):
    i = 1024
    while i < (256 * len(format)):
        # if the buffer is 256 times as long as the format, we're not failing
        # for lack of room (see pypy)
        with lltype.scoped_alloc(rffi.TIME_TP.TO, 1) as t_ref:
            t_ref[0] = int(seconds)
            p = c_gmtime(t_ref)
            with lltype.scoped_alloc(rffi.CCHARP.TO, i) as outbuf:
                buflen = c_strftime(outbuf, i, format, p)
                if buflen > 0:
                    return rffi.charp2strn(outbuf, intmask(buflen))
        i += i
    return ""
コード例 #29
0
ファイル: timeobject.py プロジェクト: fniephaus/topaz
def strftime(format, seconds):
    i = 1024
    while i < (256 * len(format)):
        # if the buffer is 256 times as long as the format, we're not failing
        # for lack of room (see pypy)
        with lltype.scoped_alloc(rffi.TIME_TP.TO, 1) as t_ref:
            t_ref[0] = int(seconds)
            p = c_gmtime(t_ref)
            with lltype.scoped_alloc(rffi.CCHARP.TO, i) as outbuf:
                buflen = c_strftime(outbuf, i, format, p)
                if buflen > 0:
                    return rffi.charp2strn(outbuf, intmask(buflen))
        i += i
    return ""
コード例 #30
0
    def process_time(space, w_info=None):
        if HAS_CLOCK_GETTIME and (rtime.CLOCK_PROF is not None or
                                  rtime.CLOCK_PROCESS_CPUTIME_ID is not None):
            if rtime.CLOCK_PROF is not None:
                clk_id = rtime.CLOCK_PROF
                implementation = "clock_gettime(CLOCK_PROF)"
            else:
                clk_id = rtime.CLOCK_PROCESS_CPUTIME_ID
                implementation = "clock_gettime(CLOCK_PROCESS_CPUTIME_ID)"
            with lltype.scoped_alloc(TIMESPEC) as timespec:
                ret = c_clock_gettime(clk_id, timespec)
                if ret == 0:
                    if w_info is not None:
                        with lltype.scoped_alloc(TIMESPEC) as tsres:
                            ret = c_clock_getres(clk_id, tsres)
                            if ret == 0:
                                res = _timespec_to_seconds(tsres)
                            else:
                                res = 1e-9
                        _setinfo(space, w_info, implementation, res, True,
                                 False)
                    return space.newfloat(_timespec_to_seconds(timespec))

        if True:  # XXX available except if it isn't?
            from rpython.rlib.rtime import (c_getrusage, RUSAGE, RUSAGE_SELF,
                                            decode_timeval)
            with lltype.scoped_alloc(RUSAGE) as rusage:
                ret = c_getrusage(RUSAGE_SELF, rusage)
                if ret == 0:
                    if w_info is not None:
                        _setinfo(space, w_info, "getrusage(RUSAGE_SELF)", 1e-6,
                                 True, False)
                    return space.newfloat(
                        decode_timeval(rusage.c_ru_utime) +
                        decode_timeval(rusage.c_ru_stime))
        if have_times:
            with lltype.scoped_alloc(rposix.TMS) as tms:
                ret = rposix.c_times(tms)
                if rffi.cast(lltype.Signed, ret) != -1:
                    cpu_time = float(
                        rffi.cast(lltype.Signed, tms.c_tms_utime) +
                        rffi.cast(lltype.Signed, tms.c_tms_stime))
                    if w_info is not None:
                        _setinfo(space, w_info, "times()",
                                 1.0 / rposix.CLOCK_TICKS_PER_SECOND, True,
                                 False)
                    return space.newfloat(cpu_time /
                                          rposix.CLOCK_TICKS_PER_SECOND)
        return clock(space)
コード例 #31
0
def _create_tuple_for_attribute(space, name, value):
    with lltype.scoped_alloc(rffi.CCHARP.TO, X509_NAME_MAXLEN) as buf:
        length = libssl_OBJ_obj2txt(buf, X509_NAME_MAXLEN, name, 0)
        if length < 0:
            raise _ssl_seterror(space, None, 0)
        w_name = space.wrap(rffi.charpsize2str(buf, length))

    with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as buf_ptr:
        length = libssl_ASN1_STRING_to_UTF8(buf_ptr, value)
        if length < 0:
            raise _ssl_seterror(space, None, 0)
        w_value = space.wrap(rffi.charpsize2str(buf_ptr[0], length))
        w_value = space.call_method(w_value, "decode", space.wrap("utf-8"))

    return space.newtuple([w_name, w_value])
コード例 #32
0
ファイル: interp_ssl.py プロジェクト: charred/pypy
def _create_tuple_for_attribute(space, name, value):
    with lltype.scoped_alloc(rffi.CCHARP.TO, X509_NAME_MAXLEN) as buf:
        length = libssl_OBJ_obj2txt(buf, X509_NAME_MAXLEN, name, 0)
        if length < 0:
            raise _ssl_seterror(space, None, 0)
        w_name = space.wrap(rffi.charpsize2str(buf, length))

    with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as buf_ptr:
        length = libssl_ASN1_STRING_to_UTF8(buf_ptr, value)
        if length < 0:
            raise _ssl_seterror(space, None, 0)
        w_value = space.wrap(rffi.charpsize2str(buf_ptr[0], length))
        w_value = space.call_method(w_value, "decode", space.wrap("utf-8"))

    return space.newtuple([w_name, w_value])
コード例 #33
0
ファイル: rmqueue.py プロジェクト: dckc/typhon
 def _getattr(self):
     with lltype.scoped_alloc(mq_attr) as mqstat:
         check_call(mq_getattr(self.mqd, mqstat), "mq_getattr")
         self.flags = mqstat.c_mq_flags
         self.maxmsg = mqstat.c_mq_maxmsg
         self.msgsize = mqstat.c_mq_msgsize
         self.curmsgs = mqstat.c_mq_curmsgs
コード例 #34
0
ファイル: cffi1_module.py プロジェクト: Qointum/pypy
def load_cffi1_module(space, name, path, initptr):
    # This is called from pypy.module.cpyext.api.load_extension_module()
    initfunc = rffi.cast(initfunctype, initptr)
    with lltype.scoped_alloc(rffi.VOIDPP.TO, 2) as p:
        p[0] = rffi.cast(rffi.VOIDP, VERSION_EXPORT)
        initfunc(p)
        version = rffi.cast(lltype.Signed, p[0])
        if not (VERSION_MIN <= version <= VERSION_MAX):
            raise oefmt(space.w_ImportError,
                "cffi extension module '%s' has unknown version %s",
                name, hex(version))
        src_ctx = rffi.cast(parse_c_type.PCTX, p[1])

    ffi = W_FFIObject(space, src_ctx)
    lib = W_LibObject(ffi, name)
    if src_ctx.c_includes:
        lib.make_includes_from(src_ctx.c_includes)

    w_name = space.wrap(name)
    module = Module(space, w_name)
    module.setdictvalue(space, '__file__', space.wrap(path))
    module.setdictvalue(space, 'ffi', space.wrap(ffi))
    module.setdictvalue(space, 'lib', space.wrap(lib))
    w_modules_dict = space.sys.get('modules')
    space.setitem(w_modules_dict, w_name, space.wrap(module))
    space.setitem(w_modules_dict, space.wrap(name + '.lib'), space.wrap(lib))
コード例 #35
0
ファイル: ccallback.py プロジェクト: mozillazg/pypy
 def __init__(self, space, cdata, ctype, w_callable, w_error, w_onerror):
     W_CData.__init__(self, space, cdata, ctype)
     #
     if not space.is_true(space.callable(w_callable)):
         raise oefmt(space.w_TypeError,
                     "expected a callable object, not %T", w_callable)
     self.w_callable = w_callable
     if not space.is_none(w_onerror):
         if not space.is_true(space.callable(w_onerror)):
             raise oefmt(space.w_TypeError,
                         "expected a callable object for 'onerror', not %T",
                         w_onerror)
         self.w_onerror = w_onerror
     #
     fresult = self.getfunctype().ctitem
     size = fresult.size
     if size < 0:
         size = 0
     elif fresult.is_primitive_integer and size < SIZE_OF_FFI_ARG:
         size = SIZE_OF_FFI_ARG
     with lltype.scoped_alloc(rffi.CCHARP.TO, size, zero=True) as ll_error:
         if not space.is_none(w_error):
             convert_from_object_fficallback(fresult, ll_error, w_error,
                                          self.decode_args_from_libffi)
         self.error_string = rffi.charpsize2str(ll_error, size)
     #
     # We must setup the GIL here, in case the callback is invoked in
     # some other non-Pythonic thread.  This is the same as cffi on
     # CPython, or ctypes.
     if space.config.translation.thread:
         from pypy.module.thread.os_thread import setup_threads
         setup_threads(space)
コード例 #36
0
def PyObject_AsCharBuffer(space, obj, bufferp, sizep):
    """Returns a pointer to a read-only memory location usable as
    character-based input.  The obj argument must support the single-segment
    character buffer interface.  On success, returns 0, sets buffer to the
    memory location and size to the buffer length.  Returns -1 and sets a
    TypeError on error.
    """
    pto = obj.c_ob_type
    pb = pto.c_tp_as_buffer
    if not (pb and pb.c_bf_getbuffer):
        raise oefmt(space.w_TypeError,
                    "expected an object with the buffer interface")
    with lltype.scoped_alloc(Py_buffer) as view:
        ret = generic_cpy_call(space, pb.c_bf_getbuffer, obj, view,
                               rffi.cast(rffi.INT_real, PyBUF_SIMPLE))
        if rffi.cast(lltype.Signed, ret) == -1:
            return -1

        bufferp[0] = rffi.cast(rffi.CCHARP, view.c_buf)
        sizep[0] = view.c_len

        if pb.c_bf_releasebuffer:
            generic_cpy_call(space, pb.c_bf_releasebuffer, obj, view)
        decref(space, view.c_obj)
    return 0
コード例 #37
0
ファイル: cffi1_module.py プロジェクト: abhinavthomas/pypy
def load_cffi1_module(space, name, path, initptr):
    # This is called from pypy.module.cpyext.api.load_extension_module()
    from pypy.module._cffi_backend.call_python import get_ll_cffi_call_python

    initfunc = rffi.cast(INITFUNCPTR, initptr)
    with lltype.scoped_alloc(rffi.VOIDPP.TO, 16, zero=True) as p:
        p[0] = rffi.cast(rffi.VOIDP, VERSION_EXPORT)
        p[1] = rffi.cast(rffi.VOIDP, get_ll_cffi_call_python())
        initfunc(p)
        version = rffi.cast(lltype.Signed, p[0])
        if not (VERSION_MIN <= version <= VERSION_MAX):
            raise oefmt(space.w_ImportError,
                "cffi extension module '%s' uses an unknown version tag %s. "
                "This module might need a more recent version of PyPy. "
                "The current PyPy provides CFFI %s.",
                name, hex(version), _cffi_backend.VERSION)
        src_ctx = rffi.cast(parse_c_type.PCTX, p[1])

    ffi = W_FFIObject(space, src_ctx)
    lib = W_LibObject(ffi, name)
    if src_ctx.c_includes:
        lib.make_includes_from(src_ctx.c_includes)

    w_name = space.wrap(name)
    module = Module(space, w_name)
    if path is not None:
        module.setdictvalue(space, '__file__', space.wrap(path))
    module.setdictvalue(space, 'ffi', space.wrap(ffi))
    module.setdictvalue(space, 'lib', space.wrap(lib))
    w_modules_dict = space.sys.get('modules')
    space.setitem(w_modules_dict, w_name, space.wrap(module))
    space.setitem(w_modules_dict, space.wrap(name + '.lib'), space.wrap(lib))
コード例 #38
0
def File_read(vm):
    (self, rsize_o), _ = vm.decode_args(mand="!", opt="I", self_of=File)
    assert isinstance(self, File)
    _check_open(vm, self)

    flockfile(self.filep)
    fsize = os.fstat(fileno(self.filep)).st_size
    if rsize_o is None:
        rsize = fsize
    else:
        assert isinstance(rsize_o, Con_Int)
        rsize = rsize_o.v
        if rsize < 0:
            vm.raise_helper("File_Exception", \
              [Con_String(vm, "Can not read less than 0 bytes from file.")])
        elif rsize > fsize:
            rsize = fsize

    if objectmodel.we_are_translated():
        with lltype.scoped_alloc(rffi.CCHARP.TO, rsize) as buf:
            r = fread(buf, 1, rsize, self.filep)
            if r < rffi.r_size_t(rsize) and ferror(self.filep) != 0:
                vm.raise_helper("File_Exception",
                                [Con_String(vm, "Read error.")])
            s = rffi.charpsize2str(buf, rarithmetic.intmask(r))
    else:
        # rffi.charpsize2str is so slow (taking minutes for big strings) that it's worth bypassing
        # it when things are run untranslated.
        s = os.read(fileno(self.filep), rsize)
    funlockfile(self.filep)

    return Con_String(vm, s)
コード例 #39
0
ファイル: object.py プロジェクト: Qointum/pypy
def PyObject_AsCharBuffer(space, obj, bufferp, sizep):
    """Returns a pointer to a read-only memory location usable as
    character-based input.  The obj argument must support the single-segment
    character buffer interface.  On success, returns 0, sets buffer to the
    memory location and size to the buffer length.  Returns -1 and sets a
    TypeError on error.
    """
    pto = obj.c_ob_type

    pb = pto.c_tp_as_buffer
    if not (pb and pb.c_bf_getbuffer):
        raise OperationError(space.w_TypeError, space.wrap(
            "expected an object with the buffer interface"))
    with lltype.scoped_alloc(Py_buffer) as view:
        ret = generic_cpy_call(
            space, pb.c_bf_getbuffer,
            obj, view, rffi.cast(rffi.INT_real, PyBUF_SIMPLE))
        if rffi.cast(lltype.Signed, ret) == -1:
            return -1

        bufferp[0] = rffi.cast(rffi.CCHARP, view.c_buf)
        sizep[0] = view.c_len
            
        if pb.c_bf_releasebuffer:
            generic_cpy_call(space, pb.c_bf_releasebuffer,
                             obj, view)
        Py_DecRef(space, view.c_obj)
    return 0
コード例 #40
0
ファイル: ccallback.py プロジェクト: sota/pypy-old
 def __init__(self, space, cdata, ctype, w_callable, w_error, w_onerror):
     W_CData.__init__(self, space, cdata, ctype)
     #
     if not space.is_true(space.callable(w_callable)):
         raise oefmt(space.w_TypeError,
                     "expected a callable object, not %T", w_callable)
     self.w_callable = w_callable
     if not space.is_none(w_onerror):
         if not space.is_true(space.callable(w_onerror)):
             raise oefmt(space.w_TypeError,
                         "expected a callable object for 'onerror', not %T",
                         w_onerror)
         self.w_onerror = w_onerror
     #
     fresult = self.getfunctype().ctitem
     size = fresult.size
     if size < 0:
         size = 0
     elif fresult.is_primitive_integer and size < SIZE_OF_FFI_ARG:
         size = SIZE_OF_FFI_ARG
     with lltype.scoped_alloc(rffi.CCHARP.TO, size, zero=True) as ll_error:
         if not space.is_none(w_error):
             convert_from_object_fficallback(fresult, ll_error, w_error,
                                          self.decode_args_from_libffi)
         self.error_string = rffi.charpsize2str(ll_error, size)
     #
     # We must setup the GIL here, in case the callback is invoked in
     # some other non-Pythonic thread.  This is the same as cffi on
     # CPython, or ctypes.
     if space.config.translation.thread:
         from pypy.module.thread.os_thread import setup_threads
         setup_threads(space)
コード例 #41
0
def load_cffi1_module(space, name, path, initptr):
    # This is called from pypy.module.cpyext.api.load_extension_module()
    from pypy.module._cffi_backend.call_python import get_ll_cffi_call_python

    initfunc = rffi.cast(INITFUNCPTR, initptr)
    with lltype.scoped_alloc(rffi.VOIDPP.TO, 16, zero=True) as p:
        p[0] = rffi.cast(rffi.VOIDP, VERSION_EXPORT)
        p[1] = rffi.cast(rffi.VOIDP, get_ll_cffi_call_python())
        initfunc(p)
        version = rffi.cast(lltype.Signed, p[0])
        if not (VERSION_MIN <= version <= VERSION_MAX):
            raise oefmt(
                space.w_ImportError,
                "cffi extension module '%s' uses an unknown version tag %s. "
                "This module might need a more recent version of PyPy. "
                "The current PyPy provides CFFI %s.", name, hex(version),
                _cffi_backend.VERSION)
        src_ctx = rffi.cast(parse_c_type.PCTX, p[1])

    ffi = W_FFIObject(space, src_ctx)
    lib = W_LibObject(ffi, name)
    if src_ctx.c_includes:
        lib.make_includes_from(src_ctx.c_includes)

    w_name = space.newtext(name)
    module = Module(space, w_name)
    if path is not None:
        module.setdictvalue(space, '__file__', space.newtext(path))
    module.setdictvalue(space, 'ffi', ffi)
    module.setdictvalue(space, 'lib', lib)
    w_modules_dict = space.sys.get('modules')
    space.setitem(w_modules_dict, w_name, module)
    space.setitem(w_modules_dict, space.newtext(name + '.lib'), lib)
    return module
コード例 #42
0
ファイル: interp_ssl.py プロジェクト: charred/pypy
    def peer_certificate(self, der=False):
        """peer_certificate([der=False]) -> certificate

        Returns the certificate for the peer.  If no certificate was provided,
        returns None.  If a certificate was provided, but not validated, returns
        an empty dictionary.  Otherwise returns a dict containing information
        about the peer certificate.

        If the optional argument is True, returns a DER-encoded copy of the
        peer certificate, or None if no certificate was provided.  This will
        return the certificate even if it wasn't validated."""
        if not self.peer_cert:
            return self.space.w_None

        if der:
            # return cert in DER-encoded format
            with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as buf_ptr:
                buf_ptr[0] = lltype.nullptr(rffi.CCHARP.TO)
                length = libssl_i2d_X509(self.peer_cert, buf_ptr)
                if length < 0:
                    raise _ssl_seterror(self.space, self, length)
                try:
                    # this is actually an immutable bytes sequence
                    return self.space.wrap(rffi.charpsize2str(buf_ptr[0],
                                                              length))
                finally:
                    libssl_OPENSSL_free(buf_ptr[0])
        else:
            verification = libssl_SSL_CTX_get_verify_mode(
                libssl_SSL_get_SSL_CTX(self.ssl))
            if not verification & SSL_VERIFY_PEER:
                return self.space.newdict()
            else:
                return _decode_certificate(self.space, self.peer_cert)
コード例 #43
0
ファイル: ruv.py プロジェクト: dckc/typhon
def spawn(loop, process, file, args, env, streams):
    """
    The file descriptor list should be a list of streams to wire up to FDs in
    the child. A None stream is mapped to UV_IGNORE.
    """

    with rffi.scoped_str2charp(file) as rawFile:
        rawArgs = rffi.liststr2charpp(args)
        rawEnv = rffi.liststr2charpp(env)
        with rffi.scoped_str2charp(".") as rawCWD:
            options = rffi.make(cConfig["process_options_t"], c_file=rawFile,
                                c_args=rawArgs, c_env=rawEnv, c_cwd=rawCWD)
            with lltype.scoped_alloc(rffi.CArray(stdio_container_t), len(streams)) as rawStreams:
                for i, stream in enumerate(streams):
                    if stream == lltype.nullptr(stream_t):
                        flags = UV_IGNORE
                    else:
                        flags = UV_CREATE_PIPE
                        if i == 0:
                            flags |= UV_READABLE_PIPE
                        elif i in (1, 2):
                            flags |= UV_WRITABLE_PIPE
                        set_stdio_stream(rawStreams[i], stream)
                    rffi.setintfield(rawStreams[i], "c_flags", flags)
                options.c_stdio = rawStreams
                rffi.setintfield(options, "c_stdio_count", len(streams))
                add_exit_cb(options, processDiscard)
                rffi.setintfield(options, "c_flags", UV_PROCESS_WINDOWS_HIDE)
                rv = uv_spawn(loop, process, options)
                free(options)
        rffi.free_charpp(rawEnv)
        rffi.free_charpp(rawArgs)

    check("spawn", rv)
コード例 #44
0
ファイル: Con_POSIX_File.py プロジェクト: ltratt/converge
def File_read(vm):
    (self, rsize_o),_ = vm.decode_args(mand="!", opt="I", self_of=File)
    assert isinstance(self, File)
    _check_open(vm, self)
    
    flockfile(self.filep)
    fsize = os.fstat(fileno(self.filep)).st_size
    if rsize_o is None:
        rsize = fsize
    else:
        assert isinstance(rsize_o, Con_Int)
        rsize = rsize_o.v
        if rsize < 0:
            vm.raise_helper("File_Exception", \
              [Con_String(vm, "Can not read less than 0 bytes from file.")])
        elif rsize > fsize:
            rsize = fsize

    if objectmodel.we_are_translated():
        with lltype.scoped_alloc(rffi.CCHARP.TO, rsize) as buf:
            r = fread(buf, 1, rsize, self.filep)
            if r < rffi.r_size_t(rsize) and ferror(self.filep) != 0:
                vm.raise_helper("File_Exception", [Con_String(vm, "Read error.")])
            s = rffi.charpsize2str(buf, rarithmetic.intmask(r))
    else:
        # rffi.charpsize2str is so slow (taking minutes for big strings) that it's worth bypassing
        # it when things are run untranslated.
        s = os.read(fileno(self.filep), rsize)
    funlockfile(self.filep)

    return Con_String(vm, s)
コード例 #45
0
ファイル: interp_time.py プロジェクト: mozillazg/pypy
 def clock_getres(space, clk_id):
     with lltype.scoped_alloc(rtime.TIMESPEC) as tp:
         ret = rtime.c_clock_getres(clk_id, tp)
         if ret != 0:
             raise exception_from_saved_errno(space, space.w_IOError)
         t = float(rffi.getintfield(tp, "c_tv_sec")) + float(rffi.getintfield(tp, "c_tv_nsec")) * 0.000000001
     return space.wrap(t)
コード例 #46
0
def realize_global_int(ffi, g, gindex):
    fetch_fnptr = rffi.cast(FUNCPTR_FETCH_LONGLONG, g.c_address)
    with lltype.scoped_alloc(parse_c_type.GETCONST_S) as p_value:
        p_value.c_ctx = ffi.ctxobj.ctx
        rffi.setintfield(p_value, 'c_gindex', gindex)
        neg = fetch_fnptr(p_value)
        value = p_value.c_value
    neg = rffi.cast(lltype.Signed, neg)

    if neg == 0:     # positive
        if value <= rffi.cast(rffi.ULONGLONG, sys.maxint):
            return ffi.space.wrap(intmask(value))
        else:
            return ffi.space.wrap(value)
    elif neg == 1:   # negative
        value = rffi.cast(rffi.LONGLONG, value)
        if value >= -sys.maxint-1:
            return ffi.space.wrap(intmask(value))
        else:
            return ffi.space.wrap(value)

    if neg == 2:
        got = "%d (0x%x)" % (value, value)
    else:
        got = "%d" % (rffi.cast(rffi.LONGLONG, value),)
    raise oefmt(ffi.w_FFIError,
                "the C compiler says '%s' is equal to %s, "
                "but the cdef disagrees", rffi.charp2str(g.c_name), got)
コード例 #47
0
    def call_varargs_kw(self, space, h_self, __args__, skip_args,
                        has_keywords):
        # this function is more or less the equivalent of
        # ctx_CallRealFunctionFromTrampoline in cpython-universal
        n = len(__args__.arguments_w) - skip_args

        # XXX this looks inefficient: ideally, we would like the equivalent of
        # alloca(): do we have it in RPython? The alternative is to wrap
        # arguments_w in a tuple, convert to handle and pass it to a C
        # function whichs calls alloca() and the forwards everything to the
        # functpr
        with lltype.scoped_alloc(rffi.CArray(llapi.HPy), n) as args_h:
            i = 0
            while i < n:
                args_h[i] = handles.new(space,
                                        __args__.arguments_w[i + skip_args])
                i += 1

            if has_keywords:
                h_result = self.call_keywords(space, h_self, args_h, n,
                                              __args__)
            else:
                h_result = self.call_varargs(space, h_self, args_h, n)

            # XXX this should probably be in a try/finally. We should add a
            # test to check that we don't leak handles
            for i in range(n):
                handles.close(space, args_h[i])

        return handles.consume(space, h_result)
コード例 #48
0
ファイル: interp_epoll.py プロジェクト: yuyichao/pypy
    def descr_poll(self, space, timeout=-1.0, maxevents=-1):
        self.check_closed(space)
        if timeout < 0:
            timeout = -1.0
        else:
            timeout *= 1000.0

        if maxevents == -1:
            maxevents = FD_SETSIZE - 1
        elif maxevents < 1:
            raise oefmt(space.w_ValueError,
                        "maxevents must be greater than 0, not %d", maxevents)

        with lltype.scoped_alloc(rffi.CArray(epoll_event), maxevents) as evs:
            nfds = epoll_wait(self.epfd, evs, maxevents, int(timeout))
            if nfds < 0:
                raise exception_from_errno(space, space.w_IOError)

            elist_w = [None] * nfds
            for i in xrange(nfds):
                event = evs[i]
                elist_w[i] = space.newtuple(
                    [space.wrap(event.c_data.c_fd), space.wrap(event.c_events)]
                )
            return space.newlist(elist_w)
コード例 #49
0
    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_saved_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_saved_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
コード例 #50
0
ファイル: rposix_stat.py プロジェクト: cimarieta/usp
def fstat(fd):
    if not _WIN32:
        with lltype.scoped_alloc(STAT_STRUCT.TO) as stresult:
            handle_posix_error("fstat", c_fstat(fd, stresult))
            return build_stat_result(stresult)
    else:
        handle = rwin32.get_osfhandle(fd)
        win32traits = make_win32_traits(string_traits)
        filetype = win32traits.GetFileType(handle)
        if filetype == win32traits.FILE_TYPE_CHAR:
            # console or LPT device
            return make_stat_result((win32traits._S_IFCHR, 0, 0, 0, 0, 0, 0, 0, 0, 0))
        elif filetype == win32traits.FILE_TYPE_PIPE:
            # socket or named pipe
            return make_stat_result((win32traits._S_IFIFO, 0, 0, 0, 0, 0, 0, 0, 0, 0))
        elif filetype == win32traits.FILE_TYPE_UNKNOWN:
            error = rwin32.GetLastError_saved()
            if error != 0:
                raise WindowsError(error, "os_fstat failed")
            # else: unknown but valid file

        # normal disk file (FILE_TYPE_DISK)
        info = lltype.malloc(win32traits.BY_HANDLE_FILE_INFORMATION, flavor="raw", zero=True)
        try:
            res = win32traits.GetFileInformationByHandle(handle, info)
            if res == 0:
                raise WindowsError(rwin32.GetLastError_saved(), "os_fstat failed")
            return win32_by_handle_info_to_stat(win32traits, info)
        finally:
            lltype.free(info, flavor="raw")
コード例 #51
0
def setupterm_func(vm):
    mod = vm.get_funcs_mod()
    (term_o, file_o), _ = vm.decode_args(opt="sO")
    
    if HAVE_CURSES:
        if term_o:
            assert isinstance(term_o, Con_String)
            raise Exception("XXX")
        else:
            term = None

        if file_o:
            fd = type_check_int(vm, vm.get_slot_apply(file_o, "fileno")).v
        else:
            fd = STDOUT_FILENO

        with lltype.scoped_alloc(rffi.INTP.TO, 1) as erret:
            if setupterm(term, fd, erret) != OK:
                ec = int(erret[0])
                if ec == -1:
                    msg = "Can't find terminfo database."
                elif ec == 0:
                    msg = "Terminal not found or not enough information known about it."
                elif ec == 1:
                    msg = "Terminal is hardcopy."
                else:
                    raise Exception("XXX")

                cex_class = mod.get_defn(vm, "Curses_Exception")
                vm.raise_(vm.get_slot_apply(cex_class, "new", [Con_String(vm, msg)]))
    else:
        raise Exception("XXX")
    
    return vm.get_builtin(BUILTIN_NULL_OBJ)
コード例 #52
0
def fstat(fd):
    if not _WIN32:
        with lltype.scoped_alloc(STAT_STRUCT.TO) as stresult:
            handle_posix_error('fstat', c_fstat(fd, stresult))
            return build_stat_result(stresult)
    else:
        handle = rwin32.get_osfhandle(fd)
        win32traits = make_win32_traits(string_traits)
        filetype = win32traits.GetFileType(handle)
        if filetype == win32traits.FILE_TYPE_CHAR:
            # console or LPT device
            return make_stat_result(
                (win32traits._S_IFCHR, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
        elif filetype == win32traits.FILE_TYPE_PIPE:
            # socket or named pipe
            return make_stat_result(
                (win32traits._S_IFIFO, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
        elif filetype == win32traits.FILE_TYPE_UNKNOWN:
            error = rwin32.GetLastError_saved()
            if error != 0:
                raise WindowsError(error, "os_fstat failed")
            # else: unknown but valid file

        # normal disk file (FILE_TYPE_DISK)
        info = lltype.malloc(win32traits.BY_HANDLE_FILE_INFORMATION,
                             flavor='raw',
                             zero=True)
        try:
            res = win32traits.GetFileInformationByHandle(handle, info)
            if res == 0:
                raise WindowsError(rwin32.GetLastError_saved(),
                                   "os_fstat failed")
            return win32_by_handle_info_to_stat(win32traits, info)
        finally:
            lltype.free(info, flavor='raw')
コード例 #53
0
def test_high_precision_stat_time():
    def f():
        st = os.stat('.')
        # should be supported on all platforms, but give a result whose
        # precision might be lower than full nanosecond
        highprec = rposix_stat.get_stat_ns_as_bigint(st, "ctime")
        return '%s;%s' % (st.st_ctime, highprec.str())

    fc = compile(f, [])
    as_string = fc()
    asfloat, highprec = as_string.split(';')
    asfloat = float(asfloat)
    highprec = int(highprec)
    st = os.stat('.')
    assert abs(asfloat - st.st_ctime) < 500e-9
    assert abs(highprec - int(st.st_ctime * 1e9)) < 500
    assert abs(
        rposix_stat.get_stat_ns_as_bigint(st, "ctime").tolong() -
        st.st_ctime * 1e9) < 3
    if rposix_stat.TIMESPEC is not None:
        with lltype.scoped_alloc(rposix_stat.STAT_STRUCT.TO) as stresult:
            rposix_stat.c_stat(".", stresult)
            if sys.platform == "darwin":
                assert 0 <= stresult.c_st_ctimespec.c_tv_nsec <= 999999999
                assert highprec == (
                    int(stresult.c_st_ctimespec.c_tv_sec) * 1000000000 +
                    int(stresult.c_st_ctimespec.c_tv_nsec))
            else:
                assert 0 <= stresult.c_st_ctim.c_tv_nsec <= 999999999
                assert highprec == (
                    int(stresult.c_st_ctim.c_tv_sec) * 1000000000 +
                    int(stresult.c_st_ctim.c_tv_nsec))
コード例 #54
0
ファイル: slotdefs.py プロジェクト: mozillazg/pypy
def wrap_getbuffer(space, w_self, w_args, func):
    func_target = rffi.cast(getbufferproc, func)
    with lltype.scoped_alloc(Py_buffer) as pybuf:
        _flags = 0
        if space.len_w(w_args) > 0:
            _flags = space.int_w(space.listview(w_args)[0])
        flags = rffi.cast(rffi.INT_real,_flags)
        size = generic_cpy_call(space, func_target, w_self, pybuf, flags)
        if widen(size) < 0:
            space.fromcache(State).check_and_raise_exception(always=True)
        ptr = pybuf.c_buf
        size = pybuf.c_len
        ndim = widen(pybuf.c_ndim)
        shape =   [pybuf.c_shape[i]   for i in range(ndim)]
        if pybuf.c_strides:
            strides = [pybuf.c_strides[i] for i in range(ndim)]
        else:
            strides = [1]
        if pybuf.c_format:
            format = rffi.charp2str(pybuf.c_format)
        else:
            format = 'B'
        return space.newbuffer(CPyBuffer(ptr, size, w_self, format=format,
                            ndim=ndim, shape=shape, strides=strides,
                            itemsize=pybuf.c_itemsize,
                            readonly=widen(pybuf.c_readonly)))
コード例 #55
0
ファイル: display.py プロジェクト: HPI-SWA-Lab/RSqueak
 def get_next_event(self, time=0):
     if self.has_queued_events():
         return self.dequeue_event()
     got_event = False
     with lltype.scoped_alloc(RSDL.Event) as event:
         while RSDL.PollEvent(event) == 1:
             got_event = True
             event_type = r_uint(event.c_type)
             if event_type in (RSDL.MOUSEBUTTONDOWN, RSDL.MOUSEBUTTONUP):
                 self.handle_mouse_button(event_type, event)
                 self.queue_event(self.get_next_mouse_event(time))
             elif event_type == RSDL.MOUSEMOTION:
                 self.handle_mouse_move(event_type, event)
                 self.queue_event(self.get_next_mouse_event(time))
             elif event_type == RSDL.MOUSEWHEEL:
                 self.queue_event(self.get_next_mouse_wheel_event(time, event))
             elif event_type == RSDL.KEYDOWN:
                 self.handle_keyboard_event(event_type, event)
                 later = None
                 if not self.is_modifier_key(self.key):
                     # no TEXTINPUT event for this key will follow, but
                     # Squeak needs a KeyStroke anyway
                     if ((system.IS_LINUX and
                         self.is_control_key(self.key)) or
                         (not system.IS_LINUX and
                          (self.is_control_key(self.key) or
                           RSDL.GetModState() & ~RSDL.KMOD_SHIFT != 0))):
                         later = self.get_next_key_event(EventKeyChar, time)
                 self.fix_key_code_case()
                 self.queue_event(self.get_next_key_event(EventKeyDown, time))
                 if later:
                     self.insert_padding_event()
                     self.queue_event(later)
             elif event_type == RSDL.TEXTINPUT:
                 self.handle_textinput_event(event)
                 self.queue_event(self.get_next_key_event(EventKeyChar, time))
             elif event_type == RSDL.KEYUP:
                 self.handle_keyboard_event(event_type, event)
                 self.fix_key_code_case()
                 self.queue_event(self.get_next_key_event(EventKeyUp, time))
             elif event_type == RSDL.WINDOWEVENT:
                 self.handle_windowevent(event_type, event)
             elif event_type == RSDL.DROPFILE:
                 self.queue_event(self.get_dropevent(time, event_type, event))
             elif event_type == RSDL.QUIT:
                 if self.altf4quit:  # we want to quit hard
                     from rsqueakvm.util.dialog import ask_question
                     if ask_question('Quit Squeak without saving?'):
                         raise Exception
                 self.queue_event([
                     EventTypeWindow, time, WindowEventClose, 0, 0, 0, 0, 0
                 ])
             elif event_type in (RSDL.RENDER_TARGETS_RESET, RSDL.RENDER_DEVICE_RESET):
                 self.full_damage()
                 self.render(force=True)
             self.insert_padding_event()
     if got_event:
         return self.dequeue_event()
     return [EventTypeNone, 0, 0, 0, 0, 0, 0, 0]
コード例 #56
0
ファイル: rsocket.py プロジェクト: abhinavthomas/pypy
 def setsockopt_int(self, level, option, value):
     with lltype.scoped_alloc(rffi.INTP.TO, 1) as flag_p:
         flag_p[0] = rffi.cast(rffi.INT, value)
         res = _c.socketsetsockopt(self.fd, level, option,
                                   rffi.cast(rffi.VOIDP, flag_p),
                                   rffi.sizeof(rffi.INT))
         if res < 0:
             raise self.error_handler()
コード例 #57
0
ファイル: slotdefs.py プロジェクト: mozillazg/pypy
def wrap_getwritebuffer(space, w_self, w_args, func):
    func_target = rffi.cast(readbufferproc, func)
    with lltype.scoped_alloc(rffi.VOIDPP.TO, 1) as ptr:
        index = rffi.cast(Py_ssize_t, 0)
        size = generic_cpy_call(space, func_target, w_self, index, ptr)
        if size < 0:
            space.fromcache(State).check_and_raise_exception(always=True)
        return space.newbuffer(CPyBuffer(ptr[0], size, w_self, readonly=False))