def testSplitStructSignature(self): self.assertRaises(ValueError, objc.splitStructSignature, objc._C_ID) self.assertRaises(ValueError, objc.splitStructSignature, b"{NSPoint=dd") self.assertRaises(ValueError, objc.splitStructSignature, b"{NSPoint=dd}d") self.assertEqual(objc.splitStructSignature(b'{NSPoint=dd}'), ("NSPoint", [(None, b'd'), (None, b'd')])) self.assertEqual(objc.splitStructSignature(b'{NSPoint="x"d"y"d}'), ("NSPoint", [("x", b'd'), ("y", b'd')]))
def has_embedded_function(typestr): nm, fields = objc.splitStructSignature(_as_bytes(typestr)) for nm, tp in fields: if tp == b'?': return True elif tp == b'^?': return True elif tp.startswith(objc._C_STRUCT_B): return has_embedded_function(tp) return False
def typestr2typestr(self, typestr): typestr = _as_bytes(typestr) # As of macOS 10.13 metadata files may contain # typestring that end with property specific data; # first remove that junk. if b"," in typestr: typestr = typestr.split(b",", 1)[0] result = [] for item in objc.splitSignature(typestr): if item == objc._C_BOOL: result.append(objc._C_NSBOOL) elif item == objc._C_NSBOOL: result.append(objc._C_BOOL) elif item.startswith(objc._C_STRUCT_B) or item.startswith( objc._C_UNION_B): # unions and structs have the same structure start, stop = item[:1], item[-1:] name, fields = objc.splitStructSignature( objc._C_STRUCT_B + _as_bytes(item[1:-1]) + objc._C_STRUCT_E) result.append(start) if name is not None: result.append(_as_bytes(name)) result.append(b"=") for nm, tp in fields: if nm is not None: result.append(b'"') result.append(_as_bytes(nm)) result.append(b'"') result.append(self.typestr2typestr(tp)) result.append(stop) elif item.startswith(objc._C_ARY_B): m = re.match(br"^.(\d*)(.*).$", item) result.append(objc._C_ARY_B) result.append(m.group(1)) result.append(self.typestr2typestr(m.group(2))) result.append(objc._C_ARY_E) else: result.append(item) return b"".join(result)
def typestr2typestr(self, typestr): typestr = _as_bytes(typestr) result = [] for item in objc.splitSignature(typestr): if item == objc._C_BOOL: result.append(objc._C_NSBOOL) elif item == objc._C_NSBOOL: result.append(objc._C_BOOL) elif item.startswith(objc._C_STRUCT_B) or item.startswith( objc._C_UNION_B): # unions and structs have the same structure start, stop = item[:1], item[-1:] name, fields = objc.splitStructSignature( objc._C_STRUCT_B + _as_bytes(item[1:-1]) + objc._C_STRUCT_E) result.append(start) result.append(_as_bytes(name)) result.append(b'=') for nm, tp in fields: if nm is not None: result.append(b'"') result.append(_as_bytes(nm)) result.append(b'"') result.append(self.typestr2typestr(tp)) result.append(stop) elif item.startswith(objc._C_ARY_B): m = re.match(b'^.(\d*)(.*).$', item) result.append(objc._C_ARY_B) result.append(m.group(1)) result.append(self.typestr2typestr(m.group(2))) result.append(objc._C_ARY_E) else: result.append(item) result = b''.join(result) return result
def typestr2typestr(self, typestr): typestr = _as_bytes(typestr) result = [] for item in objc.splitSignature(typestr): if item == objc._C_BOOL: result.append(objc._C_NSBOOL) elif item == objc._C_NSBOOL: result.append(objc._C_BOOL) elif item.startswith(objc._C_STRUCT_B) or item.startswith(objc._C_UNION_B): # unions and structs have the same structure start, stop = item[:1], item[-1:] name, fields = objc.splitStructSignature(objc._C_STRUCT_B + _as_bytes(item[1:-1]) + objc._C_STRUCT_E) result.append(start) result.append(_as_bytes(name)) result.append(b'=') for nm, tp in fields: if nm is not None: result.append(b'"') result.append(_as_bytes(nm)) result.append(b'"') result.append(self.typestr2typestr(tp)) result.append(stop) elif item.startswith(objc._C_ARY_B): m = re.match(b'^.(\d*)(.*).$', item) result.append(objc._C_ARY_B) result.append(m.group(1)) result.append(self.typestr2typestr(m.group(2))) result.append(objc._C_ARY_E) else: result.append(item) result = b''.join(result) return result
def splitStruct(value): warnings.warn("Deprecated: use splitStructSignature()", DeprecationWarning) import objc return objc.splitStructSignature(value)