def setup(): INSPECT = {'b': 'signed char', 'h': 'signed short', 'i': 'signed int', 'l': 'signed long', 'q': 'signed long long', 'B': 'unsigned char', 'H': 'unsigned short', 'I': 'unsigned int', 'L': 'unsigned long', 'Q': 'unsigned long long', 'P': 'char *', 'f': 'float', 'd': 'double', } pre_include_bits = [] for fmtchar, ctype in INSPECT.items(): pre_include_bits.append(""" struct about_%s { char pad; %s field; }; """ % (fmtchar, ctype)) class CConfig: _compilation_info_ = ExternalCompilationInfo( pre_include_bits = pre_include_bits ) for fmtchar, ctype in INSPECT.items(): setattr(CConfig, fmtchar, rffi_platform.Struct( "struct about_%s" % (fmtchar,), [('field', lltype.FixedSizeArray(rffi.CHAR, 1))])) cConfig = rffi_platform.configure(CConfig) for fmtchar, ctype in INSPECT.items(): S = cConfig[fmtchar] alignment = rffi.offsetof(S, 'c_field') size = rffi.sizeof(S.c_field) signed = 'a' <= fmtchar <= 'z' if fmtchar == 'f': pack = pack_float unpack = unpack_float elif fmtchar == 'd': pack = pack_double unpack = unpack_double else: cpython_checks_range = fmtchar in 'bBhH' pack = std.make_int_packer(size, signed, cpython_checks_range) unpack = std.make_int_unpacker(size, signed) native_fmttable[fmtchar] = {'size': size, 'alignment': alignment, 'pack': pack, 'unpack': unpack}
def setup(): INSPECT = {'b': 'signed char', 'h': 'signed short', 'i': 'signed int', 'l': 'signed long', 'q': 'signed long long', 'B': 'unsigned char', 'H': 'unsigned short', 'I': 'unsigned int', 'L': 'unsigned long', 'Q': 'unsigned long long', 'P': 'char *', 'f': 'float', 'd': 'double', } pre_include_lines = [] for fmtchar, ctype in INSPECT.items(): pre_include_lines += (""" struct about_%s { char pad; %s field; }; """ % (fmtchar, ctype)).split("\n") class CConfig: _compilation_info_ = ExternalCompilationInfo( pre_include_lines = pre_include_lines ) for fmtchar, ctype in INSPECT.items(): setattr(CConfig, fmtchar, rffi_platform.Struct( "struct about_%s" % (fmtchar,), [('field', lltype.FixedSizeArray(rffi.CHAR, 1))])) cConfig = rffi_platform.configure(CConfig) for fmtchar, ctype in INSPECT.items(): S = cConfig[fmtchar] alignment = rffi.offsetof(S, 'c_field') size = rffi.sizeof(S.c_field) signed = 'a' <= fmtchar <= 'z' if fmtchar == 'f': pack = pack_float unpack = unpack_float elif fmtchar == 'd': pack = pack_double unpack = unpack_double else: cpython_checks_range = fmtchar in 'bBhH' pack = std.make_int_packer(size, signed, cpython_checks_range) unpack = std.make_int_unpacker(size, signed) native_fmttable[fmtchar] = {'size': size, 'alignment': alignment, 'pack': pack, 'unpack': unpack}
def as_object(self, space): """Convert the address to an app-level object.""" # If we don't know the address family, don't raise an # exception -- return it as a tuple. addr = self.lock() family = rffi.cast(lltype.Signed, addr.c_sa_family) datalen = self.addrlen - offsetof(_c.sockaddr, 'c_sa_data') rawdata = ''.join([addr.c_sa_data[i] for i in range(datalen)]) self.unlock() return space.newtuple([space.wrap(family), space.wrap(rawdata)])
class UNIXAddress(Address): family = AF_UNIX struct = _c.sockaddr_un minlen = offsetof(_c.sockaddr_un, 'c_sun_path') maxlen = sizeof(struct) def __init__(self, path): sun = lltype.malloc(_c.sockaddr_un, flavor='raw', zero=True) baseofs = offsetof(_c.sockaddr_un, 'c_sun_path') self.setdata(sun, baseofs + len(path)) rffi.setintfield(sun, 'c_sun_family', AF_UNIX) if _c.linux and path.startswith('\x00'): # Linux abstract namespace extension if len(path) > sizeof(_c.sockaddr_un.c_sun_path): raise RSocketError("AF_UNIX path too long") else: # regular NULL-terminated string if len(path) >= sizeof(_c.sockaddr_un.c_sun_path): raise RSocketError("AF_UNIX path too long") sun.c_sun_path[len(path)] = '\x00' for i in range(len(path)): sun.c_sun_path[i] = path[i] def __repr__(self): try: return '<UNIXAddress %r>' % (self.get_path(), ) except SocketError: return '<UNIXAddress ?>' def get_path(self): a = self.lock(_c.sockaddr_un) maxlength = self.addrlen - offsetof(_c.sockaddr_un, 'c_sun_path') if _c.linux and maxlength > 0 and a.c_sun_path[0] == '\x00': # Linux abstract namespace length = maxlength else: # regular NULL-terminated string length = 0 while length < maxlength and a.c_sun_path[length] != '\x00': length += 1 result = ''.join([a.c_sun_path[i] for i in range(length)]) self.unlock() return result def eq(self, other): # __eq__() is not called by RPython :-/ return (isinstance(other, UNIXAddress) and self.get_path() == other.get_path()) def as_object(self, space): return space.wrap(self.get_path()) def from_object(space, w_address): return UNIXAddress(space.str_w(w_address)) from_object = staticmethod(from_object)
def get_path(self): a = self.lock(_c.sockaddr_un) maxlength = self.addrlen - offsetof(_c.sockaddr_un, 'c_sun_path') if _c.linux and maxlength > 0 and a.c_sun_path[0] == '\x00': # Linux abstract namespace length = maxlength else: # regular NULL-terminated string length = 0 while length < maxlength and a.c_sun_path[length] != '\x00': length += 1 result = ''.join([a.c_sun_path[i] for i in range(length)]) self.unlock() return result
def __init__(self, path): sun = lltype.malloc(_c.sockaddr_un, flavor='raw', zero=True) baseofs = offsetof(_c.sockaddr_un, 'c_sun_path') self.setdata(sun, baseofs + len(path)) rffi.setintfield(sun, 'c_sun_family', AF_UNIX) if _c.linux and path.startswith('\x00'): # Linux abstract namespace extension if len(path) > sizeof(_c.sockaddr_un.c_sun_path): raise RSocketError("AF_UNIX path too long") else: # regular NULL-terminated string if len(path) >= sizeof(_c.sockaddr_un.c_sun_path): raise RSocketError("AF_UNIX path too long") sun.c_sun_path[len(path)] = '\x00' for i in range(len(path)): sun.c_sun_path[i] = path[i]
def setup(): INSPECT = { 'b': 'signed char', 'h': 'signed short', 'i': 'signed int', 'l': 'signed long', 'q': 'signed long long', 'B': 'unsigned char', 'H': 'unsigned short', 'I': 'unsigned int', 'L': 'unsigned long', 'Q': 'unsigned long long', 'P': 'char *', 'f': 'float', 'd': 'double', '?': '_Bool', } pre_include_bits = [ """ #ifdef _MSC_VER #define _Bool char #endif""" ] field_names = dict.fromkeys(INSPECT) for fmtchar, ctype in INSPECT.iteritems(): field_name = ctype.replace(" ", "_").replace("*", "star") field_names[fmtchar] = field_name pre_include_bits.append(""" struct about_%s { char pad; %s field; }; """ % (field_name, ctype)) class CConfig: _compilation_info_ = ExternalCompilationInfo( pre_include_bits=pre_include_bits) for fmtchar, ctype in INSPECT.items(): setattr( CConfig, field_names[fmtchar], rffi_platform.Struct( "struct about_%s" % (field_names[fmtchar], ), [('field', lltype.FixedSizeArray(rffi.CHAR, 1))])) cConfig = rffi_platform.configure(CConfig) for fmtchar, ctype in INSPECT.items(): S = cConfig[field_names[fmtchar]] alignment = rffi.offsetof(S, 'c_field') size = rffi.sizeof(S.c_field) signed = 'a' <= fmtchar <= 'z' if fmtchar == 'f': pack = pack_float unpack = unpack_float elif fmtchar == 'd': pack = pack_double unpack = unpack_double elif fmtchar == '?': pack = std.pack_bool unpack = std.unpack_bool else: pack = std.make_int_packer(size, signed, True) unpack = std.make_int_unpacker(size, signed) native_fmttable[fmtchar] = { 'size': size, 'alignment': alignment, 'pack': pack, 'unpack': unpack }
def setup(): INSPECT = {'b': 'signed char', 'h': 'signed short', 'i': 'signed int', 'l': 'signed long', 'q': 'signed long long', 'B': 'unsigned char', 'H': 'unsigned short', 'I': 'unsigned int', 'L': 'unsigned long', 'Q': 'unsigned long long', 'P': 'char *', 'f': 'float', 'd': 'double', '?': '_Bool', } pre_include_bits = [""" #ifdef _MSC_VER #define _Bool char #endif"""] field_names = dict.fromkeys(INSPECT) for fmtchar, ctype in INSPECT.iteritems(): field_name = ctype.replace(" ", "_").replace("*", "star") field_names[fmtchar] = field_name pre_include_bits.append(""" struct about_%s { char pad; %s field; }; """ % (field_name, ctype)) class CConfig: _compilation_info_ = ExternalCompilationInfo( pre_include_bits = pre_include_bits ) for fmtchar, ctype in INSPECT.items(): setattr(CConfig, field_names[fmtchar], rffi_platform.Struct( "struct about_%s" % (field_names[fmtchar],), [('field', lltype.FixedSizeArray(rffi.CHAR, 1))])) cConfig = rffi_platform.configure(CConfig) for fmtchar, ctype in INSPECT.items(): S = cConfig[field_names[fmtchar]] alignment = rffi.offsetof(S, 'c_field') size = rffi.sizeof(S.c_field) signed = 'a' <= fmtchar <= 'z' if fmtchar == 'f': pack = pack_float unpack = unpack_float elif fmtchar == 'd': pack = pack_double unpack = unpack_double elif fmtchar == '?': pack = std.pack_bool unpack = std.unpack_bool else: pack = std.make_int_packer(size, signed, True) unpack = std.make_int_unpacker(size, signed) native_fmttable[fmtchar] = {'size': size, 'alignment': alignment, 'pack': pack, 'unpack': unpack}