| 1 |
import sys |
| 2 |
from pypy.interpreter.mixedmodule import MixedModule |
| 3 |
from rpython.rlib import rdynload, clibffi |
| 4 |
from rpython.rtyper.lltypesystem import rffi |
| 5 |
|
| 6 |
from . import VERSION |
| 7 |
|
| 8 |
FFI_DEFAULT_ABI = clibffi.FFI_DEFAULT_ABI |
| 9 |
try: |
| 10 |
FFI_STDCALL = clibffi.FFI_STDCALL |
| 11 |
has_stdcall = True |
| 12 |
except AttributeError: |
| 13 |
has_stdcall = False |
| 14 |
FFI_STDCALL = None |
| 15 |
|
| 16 |
|
| 17 |
class Module(MixedModule): |
| 18 |
|
| 19 |
appleveldefs = { |
| 20 |
} |
| 21 |
interpleveldefs = { |
| 22 |
'__version__': 'space.wrap("%s")' % VERSION, |
| 23 |
|
| 24 |
'load_library': 'libraryobj.load_library', |
| 25 |
|
| 26 |
'new_primitive_type': 'newtype.new_primitive_type', |
| 27 |
'new_pointer_type': 'newtype.new_pointer_type', |
| 28 |
'new_array_type': 'newtype.new_array_type', |
| 29 |
'new_struct_type': 'newtype.new_struct_type', |
| 30 |
'new_union_type': 'newtype.new_union_type', |
| 31 |
'complete_struct_or_union': 'newtype.complete_struct_or_union', |
| 32 |
'new_void_type': 'newtype.new_void_type', |
| 33 |
'new_enum_type': 'newtype.new_enum_type', |
| 34 |
'new_function_type': 'newtype.new_function_type', |
| 35 |
|
| 36 |
'newp': 'func.newp', |
| 37 |
'cast': 'func.cast', |
| 38 |
'callback': 'func.callback', |
| 39 |
'alignof': 'func.alignof', |
| 40 |
'sizeof': 'func.sizeof', |
| 41 |
'typeof': 'func.typeof', |
| 42 |
'typeoffsetof': 'func.typeoffsetof', |
| 43 |
'rawaddressof': 'func.rawaddressof', |
| 44 |
'getcname': 'func.getcname', |
| 45 |
'newp_handle': 'handle.newp_handle', |
| 46 |
'from_handle': 'handle.from_handle', |
| 47 |
'_get_types': 'func._get_types', |
| 48 |
'_get_common_types': 'func._get_common_types', |
| 49 |
'from_buffer': 'func.from_buffer', |
| 50 |
'gcp': 'func.gcp', |
| 51 |
|
| 52 |
'_offset_in_bytes': 'func.offset_in_bytes', |
| 53 |
|
| 54 |
'string': 'func.string', |
| 55 |
'unpack': 'func.unpack', |
| 56 |
'buffer': 'cbuffer.MiniBuffer', |
| 57 |
'memmove': 'func.memmove', |
| 58 |
'release': 'func.release', |
| 59 |
|
| 60 |
'get_errno': 'cerrno.get_errno', |
| 61 |
'set_errno': 'cerrno.set_errno', |
| 62 |
|
| 63 |
'FFI_DEFAULT_ABI': 'space.wrap(%d)' % FFI_DEFAULT_ABI, |
| 64 |
'FFI_CDECL': 'space.wrap(%d)' % FFI_DEFAULT_ABI, |
| 65 |
|
| 66 |
|
| 67 |
'FFI': 'ffi_obj.W_FFIObject', |
| 68 |
'Lib': 'lib_obj.W_LibObject', |
| 69 |
'CType': 'ctypeobj.W_CType', |
| 70 |
'CField': 'ctypestruct.W_CField', |
| 71 |
'CLibrary': 'libraryobj.W_Library', |
| 72 |
'_CDataBase': 'cdataobj.W_CData', |
| 73 |
'__FFIAllocator': 'allocator.W_Allocator', |
| 74 |
'__FFIGlobSupport': 'cglob.W_GlobSupport', |
| 75 |
'__CData_iterator': 'ctypearray.W_CDataIter', |
| 76 |
'__FFIFunctionWrapper': 'wrapper.W_FunctionWrapper', |
| 77 |
} |
| 78 |
if sys.platform == 'win32': |
| 79 |
interpleveldefs['getwinerror'] = 'cerrno.getwinerror' |
| 80 |
|
| 81 |
if has_stdcall: |
| 82 |
interpleveldefs['FFI_STDCALL'] = 'space.wrap(%d)' % FFI_STDCALL |
| 83 |
|
| 84 |
def __init__(self, space, *args): |
| 85 |
MixedModule.__init__(self, space, *args) |
| 86 |
|
| 87 |
if (not space.config.objspace.disable_entrypoints and |
| 88 |
not space.config.objspace.disable_entrypoints_in_cffi): |
| 89 |
|
| 90 |
|
| 91 |
from pypy.module._cffi_backend import embedding |
| 92 |
embedding.glob.space = space |
| 93 |
|
| 94 |
|
| 95 |
def get_dict_rtld_constants(): |
| 96 |
found = {} |
| 97 |
for name in ["RTLD_LAZY", "RTLD_NOW", "RTLD_GLOBAL", "RTLD_LOCAL", |
| 98 |
"RTLD_NODELETE", "RTLD_NOLOAD", "RTLD_DEEPBIND"]: |
| 99 |
if getattr(rdynload.cConfig, name) is not None: |
| 100 |
found[name] = getattr(rdynload.cConfig, name) |
| 101 |
for name in ["RTLD_LAZY", "RTLD_NOW", "RTLD_GLOBAL", "RTLD_LOCAL"]: |
| 102 |
found.setdefault(name, 0) |
| 103 |
return found |
| 104 |
|
| 105 |
for _name, _value in get_dict_rtld_constants().items(): |
| 106 |
Module.interpleveldefs[_name] = 'space.wrap(%d)' % _value |