Exemple #1
0
    def test_structreturn(self):
        import _rawffi
        X = _rawffi.Structure([('x', 'l')])
        x = X()
        x.x = 121
        Tm = _rawffi.Structure([('tm_sec', 'i'),
                                ('tm_min', 'i'),
                                ('tm_hour', 'i'),
                                ("tm_mday", 'i'),
                                ("tm_mon", 'i'),
                                ("tm_year", 'i'),
                                ("tm_wday", 'i'),
                                ("tm_yday", 'i'),
                                ("tm_isdst", 'i')])
        libc = _rawffi.get_libc()
        try:
            gmtime = libc.ptr('gmtime', ['P'], 'P')
        except AttributeError:
            # Since msvcr80, this function is named differently
            gmtime = libc.ptr('_gmtime32', ['P'], 'P')

        arg = x.byptr()
        res = gmtime(arg)
        t = Tm.fromaddress(res[0])
        arg.free()
        assert t.tm_year == 70
        assert t.tm_sec == 1
        assert t.tm_min == 2
        x.free()
Exemple #2
0
 def test_callback(self):
     import _rawffi
     import struct
     libc = _rawffi.get_libc()
     ll_to_sort = _rawffi.Array('i')(4)
     for i in range(4):
         ll_to_sort[i] = 4-i
     qsort = libc.ptr('qsort', ['P', 'l', 'l', 'P'], None)
     bogus_args = []
     def compare(a, b):
         a1 = _rawffi.Array('i').fromaddress(_rawffi.Array('P').fromaddress(a, 1)[0], 1)
         a2 = _rawffi.Array('i').fromaddress(_rawffi.Array('P').fromaddress(b, 1)[0], 1)
         if a1[0] not in [1,2,3,4] or a2[0] not in [1,2,3,4]:
             bogus_args.append((a1[0], a2[0]))
         if a1[0] > a2[0]:
             return 1
         return -1
     a1 = ll_to_sort.byptr()
     a2 = _rawffi.Array('l')(1)
     a2[0] = len(ll_to_sort)
     a3 = _rawffi.Array('l')(1)
     a3[0] = struct.calcsize('i')
     cb = _rawffi.CallbackPtr(compare, ['P', 'P'], 'l')
     a4 = cb.byptr()
     qsort(a1, a2, a3, a4)
     res = [ll_to_sort[i] for i in range(len(ll_to_sort))]
     assert res == [1,2,3,4]
     assert not bogus_args
     a1.free()
     a2.free()
     a3.free()
     a4.free()
     ll_to_sort.free()
     cb.free()
Exemple #3
0
    def test_gettimeofday(self):
        if self.iswin32:
            skip("No gettimeofday on win32")
        import _rawffi
        struct_type = _rawffi.Structure([('tv_sec', 'l'), ('tv_usec', 'l')])
        structure = struct_type()
        libc = _rawffi.get_libc()
        gettimeofday = libc.ptr('gettimeofday', ['P', 'P'], 'i')

        arg1 = structure.byptr()
        arg2 = _rawffi.Array('P')(1)
        res = gettimeofday(arg1, arg2)
        assert res[0] == 0

        struct2 = struct_type()
        arg1[0] = struct2
        res = gettimeofday(arg1, arg2)
        assert res[0] == 0

        assert structure.tv_usec != struct2.tv_usec
        assert (structure.tv_sec == struct2.tv_sec) or (structure.tv_sec == struct2.tv_sec - 1)
        raises(AttributeError, "structure.xxx")
        structure.free()
        struct2.free()
        arg1.free()
        arg2.free()
Exemple #4
0
 def test_getattr(self):
     import _rawffi
     libc = _rawffi.get_libc()
     func = libc.ptr('rand', [], 'i')
     assert libc.ptr('rand', [], 'i') is func # caching
     assert libc.ptr('rand', [], 'l') is not func
     assert isinstance(func, _rawffi.FuncPtr)
     raises(AttributeError, "libc.xxxxxxxxxxxxxx")
Exemple #5
0
 def test_time(self):
     import _rawffi
     libc = _rawffi.get_libc()
     try:
         time = libc.ptr('time', ['z'], 'l')  # 'z' instead of 'P' just for test
     except AttributeError:
         # Since msvcr80, this function is named differently
         time = libc.ptr('_time32', ['z'], 'l')
     arg = _rawffi.Array('P')(1)
     arg[0] = 0
     res = time(arg)
     assert res[0] != 0
     arg.free()
Exemple #6
0
 def test_time(self):
     import _rawffi
     libc = _rawffi.get_libc()
     try:
         time = libc.ptr('time', ['z'],
                         'l')  # 'z' instead of 'P' just for test
     except AttributeError:
         # Since msvcr80, this function is named differently
         time = libc.ptr('_time32', ['z'], 'l')
     arg = _rawffi.Array('P')(1)
     arg[0] = 0
     res = time(arg)
     assert res[0] != 0
     arg.free()
Exemple #7
0
    def test_structreturn(self):
        import _rawffi
        X = _rawffi.Structure([('x', 'l')])
        x = X()
        x.x = 121
        Tm = _rawffi.Structure([('tm_sec', 'i'), ('tm_min', 'i'),
                                ('tm_hour', 'i'), ("tm_mday", 'i'),
                                ("tm_mon", 'i'), ("tm_year", 'i'),
                                ("tm_wday", 'i'), ("tm_yday", 'i'),
                                ("tm_isdst", 'i')])
        libc = _rawffi.get_libc()
        gmtime = libc.ptr('gmtime', ['P'], 'P')

        arg = x.byptr()
        res = gmtime(arg)
        t = Tm.fromaddress(res[0])
        arg.free()
        assert t.tm_year == 70
        assert t.tm_sec == 1
        assert t.tm_min == 2
        x.free()
Exemple #8
0
    def test_callback(self):
        import _rawffi
        import struct
        libc = _rawffi.get_libc()
        ll_to_sort = _rawffi.Array('i')(4)
        for i in range(4):
            ll_to_sort[i] = 4 - i
        qsort = libc.ptr('qsort', ['P', 'l', 'l', 'P'], None)
        bogus_args = []

        def compare(a, b):
            a1 = _rawffi.Array('i').fromaddress(
                _rawffi.Array('P').fromaddress(a, 1)[0], 1)
            a2 = _rawffi.Array('i').fromaddress(
                _rawffi.Array('P').fromaddress(b, 1)[0], 1)
            print "comparing", a1[0], "with", a2[0]
            if a1[0] not in [1, 2, 3, 4] or a2[0] not in [1, 2, 3, 4]:
                bogus_args.append((a1[0], a2[0]))
            if a1[0] > a2[0]:
                return 1
            return -1

        a1 = ll_to_sort.byptr()
        a2 = _rawffi.Array('l')(1)
        a2[0] = len(ll_to_sort)
        a3 = _rawffi.Array('l')(1)
        a3[0] = struct.calcsize('i')
        cb = _rawffi.CallbackPtr(compare, ['P', 'P'], 'i')
        a4 = cb.byptr()
        qsort(a1, a2, a3, a4)
        res = [ll_to_sort[i] for i in range(len(ll_to_sort))]
        assert res == [1, 2, 3, 4]
        assert not bogus_args
        a1.free()
        a2.free()
        a3.free()
        a4.free()
        ll_to_sort.free()
        cb.free()
Exemple #9
0
 def test_libc_load(self):
     import _rawffi
     _rawffi.get_libc()
Exemple #10
0
""" This file provides some support for things like standard_c_lib and
errno access, as portable as possible
"""

import ctypes
import ctypes.util
import sys

# __________ the standard C library __________

if sys.platform == "win32":
    import _rawffi

    standard_c_lib = ctypes.CDLL("msvcrt", handle=_rawffi.get_libc())
else:
    standard_c_lib = ctypes.CDLL(ctypes.util.find_library("c"))

if sys.platform == "win32":
    standard_c_lib._errno.restype = ctypes.POINTER(ctypes.c_int)

    def _where_is_errno():
        return standard_c_lib._errno()


elif sys.platform in ("linux2", "freebsd6"):
    standard_c_lib.__errno_location.restype = ctypes.POINTER(ctypes.c_int)

    def _where_is_errno():
        return standard_c_lib.__errno_location()

Library, providing access to those non-portable, but
still useful routines.
"""

# XXX incomplete: implemented only functions needed by subprocess.py
# PAC: 2010/08 added MS locking for Whoosh

# 07/2016: rewrote in CFFI

import sys
if sys.platform != 'win32':
    raise ImportError("The 'msvcrt' module is only available on Windows")

import _rawffi
from _pypy_winbase_cffi import ffi as _ffi
_lib = _ffi.dlopen(_rawffi.get_libc().name)
_kernel32 = _ffi.dlopen('kernel32')

import errno

try:
    from __pypy__ import builtinify, validate_fd
except ImportError:
    builtinify = validate_fd = lambda f: f


def _ioerr():
    e = _ffi.errno
    raise IOError(e, errno.errorcode[e])

""" This file provides some support for things like standard_c_lib and
errno access, as portable as possible
"""

import ctypes
import ctypes.util
import sys

# __________ the standard C library __________

if sys.platform == 'win32':
    import _rawffi
    standard_c_lib = ctypes.CDLL('msvcrt', handle=_rawffi.get_libc())
else:
    standard_c_lib = ctypes.CDLL(ctypes.util.find_library('c'))

if sys.platform == 'win32':
    standard_c_lib._errno.restype = ctypes.POINTER(ctypes.c_int)

    def _where_is_errno():
        return standard_c_lib._errno()

elif sys.platform in ('linux2', 'freebsd6'):
    standard_c_lib.__errno_location.restype = ctypes.POINTER(ctypes.c_int)

    def _where_is_errno():
        return standard_c_lib.__errno_location()

elif sys.platform in ('darwin', 'freebsd7', 'freebsd8', 'freebsd9'):
    standard_c_lib.__error.restype = ctypes.POINTER(ctypes.c_int)
Exemple #13
0
Library, providing access to those non-portable, but
still useful routines.
"""

# XXX incomplete: implemented only functions needed by subprocess.py
# PAC: 2010/08 added MS locking for Whoosh

# 07/2016: rewrote in CFFI

import sys
if sys.platform != 'win32':
    raise ImportError("The 'msvcrt' module is only available on Windows")

import _rawffi
from _pypy_winbase_cffi import ffi as _ffi
_lib = _ffi.dlopen(_rawffi.get_libc().name)

import errno

try: from __pypy__ import builtinify, validate_fd
except ImportError: builtinify = validate_fd = lambda f: f


def _ioerr():
    e = _ffi.errno
    raise IOError(e, errno.errorcode[e])


@builtinify
def open_osfhandle(fd, flags):
    """"open_osfhandle(handle, flags) -> file descriptor
Exemple #14
0
 def test_libc_load(self):
     import _rawffi
     _rawffi.get_libc()
Exemple #15
0
import _rawffi, sys
try:
    from thread import _local as local
except ImportError:
    local = object    # no threads

class ConvMode:
    encoding = 'ascii'
    errors = 'strict'

_memmove_addr = _rawffi.get_libc().getaddressindll('memmove')
_memset_addr = _rawffi.get_libc().getaddressindll('memset')

def _string_at_addr(addr, lgt):
    # address here can be almost anything
    import ctypes
    cobj = ctypes.c_void_p.from_param(addr)
    arg = cobj._get_buffer_value()
    return _rawffi.charp2rawstring(arg, lgt)

def set_conversion_mode(encoding, errors):
    old_cm = ConvMode.encoding, ConvMode.errors
    ConvMode.errors = errors
    ConvMode.encoding = encoding
    return old_cm

def _wstring_at_addr(addr, lgt):
    import ctypes
    cobj = ctypes.c_void_p.from_param(addr)
    arg = cobj._get_buffer_value()
Exemple #16
0
import _rawffi, sys


class ConvMode:
    encoding = 'ascii'
    errors = 'strict'


_memmove_addr = _rawffi.get_libc().getaddressindll('memmove')
_memset_addr = _rawffi.get_libc().getaddressindll('memset')


def _string_at_addr(addr, lgt):
    # address here can be almost anything
    import ctypes
    arg = ctypes.c_char_p._CData_value(addr)
    return _rawffi.charp2rawstring(arg, lgt)


def set_conversion_mode(encoding, errors):
    old_cm = ConvMode.encoding, ConvMode.errors
    ConvMode.errors = errors
    ConvMode.encoding = encoding
    return old_cm


def _wstring_at_addr(addr, lgt):
    import ctypes
    arg = ctypes.c_wchar_p._CData_value(addr)
    # XXX purely applevel
    if lgt == -1:
Exemple #17
0
"""
Python interface to the Microsoft Visual C Runtime
Library, providing access to those non-portable, but
still useful routines.
"""

# XXX incomplete: implemented only functions needed by subprocess.py

import _rawffi
import ctypes

_c = ctypes.CDLL('msvcrt', _rawffi.get_libc())

open_osfhandle = _c._open_osfhandle
open_osfhandle.argtypes = [ctypes.c_int, ctypes.c_int]
open_osfhandle.restype = ctypes.c_int

get_osfhandle = _c._get_osfhandle
get_osfhandle.argtypes = [ctypes.c_int]
get_osfhandle.restype = ctypes.c_int

del ctypes