def parseBridgeSupport(xmldata, globals, frameworkName, dylib_path=None, inlineTab=None): if dylib_path: lib = ctypes.cdll.LoadLibrary(dylib_path) _libraries.append(lib) objc._updatingMetadata(True) try: prs = _BridgeSupportParser(xmldata, frameworkName) globals.update(prs.values) for entry in prs.cftypes: tp = objc.registerCFSignature(*entry) globals[entry[0]] = tp for name, typestr in prs.opaque: globals[name] = objc.createOpaquePointerType(name, typestr) for name, typestr, alias in prs.structs: if alias is not None: globals[name] = alias objc.createStructAlias(name, typestr, alias) else: globals[name] = value = objc.createStructType( name, typestr, None) for name, typestr, magic in prs.constants: try: value = objc._loadConstant(name, _as_string(typestr), magic) except AttributeError: continue globals[name] = value for class_name, sel_name, is_class in prs.meta: objc.registerMetaDataForSelector( class_name, sel_name, prs.meta[(class_name, sel_name, is_class)]) if prs.functions: objc.loadBundleFunctions(None, globals, prs.functions) if inlineTab is not None: objc.loadFunctionList(inlineTab, globals, prs.functions) for name, orig in prs.func_aliases: try: globals[name] = globals[orig] except KeyError: pass finally: objc._updatingMetadata(False)
def parseBridgeSupport(xmldata, globals, frameworkName, dylib_path=None, inlineTab=None): if dylib_path: lib = ctypes.cdll.LoadLibrary(dylib_path) _libraries.append(lib) objc._updatingMetadata(True) try: prs = _BridgeSupportParser(xmldata, frameworkName) globals.update(prs.values) for entry in prs.cftypes: tp = objc.registerCFSignature(*entry) globals[entry[0]] = tp for name, typestr in prs.opaque: globals[name] = objc.createOpaquePointerType(name, typestr) for name, typestr, alias in prs.structs: if alias is not None: globals[name] = alias objc.createStructAlias(name, typestr, alias) else: globals[name] = value = objc.createStructType(name, typestr, None) for name, typestr, magic in prs.constants: try: value = objc._loadConstant(name, _as_string(typestr), magic) except AttributeError: continue globals[name] = value for class_name, sel_name, is_class in prs.meta: objc.registerMetaDataForSelector(class_name, sel_name, prs.meta[(class_name, sel_name, is_class)]) if prs.functions: objc.loadBundleFunctions(None, globals, prs.functions) if inlineTab is not None: objc.loadFunctionList(inlineTab, globals, prs.functions) for name, orig in prs.func_aliases: try: globals[name] = globals[orig] except KeyError: pass finally: objc._updatingMetadata(False)
def __get_constant(self, name): if self.__varmap_dct: if name in self.__varmap_dct: tp = self.__varmap_dct.pop(name) if tp.startswith('='): tp = tp[1:] magic = True else: magic = False result = objc._loadConstant(name, tp, magic) return result if self.__varmap: m = re.search(r"\$%s(@[^$]*)?\$" % (name, ), self.__varmap) if m is not None: tp = m.group(1) if not tp: tp = '@' else: tp = tp[1:] d = {} if tp.startswith('='): tp = tp[1:] magic = True else: magic = False return objc._loadConstant(name, tp, magic) if self.__enummap: m = re.search(r"\$%s@([^$]*)\$" % (name, ), self.__enummap) if m is not None: return self.__prs_enum(m.group(1)) if self.__funcmap: if name in self.__funcmap: # NOTE: Remove 'name' from funcmap because # it won't be needed anymore (either the # function doesn't exist, or it is loaded) # Should use slightly less memory. info = self.__funcmap.pop(name) func_list = [(name, ) + info] d = {} objc.loadBundleFunctions(self.__bundle, d, func_list) if name in d: return d[name] if self.__inlinelist is not None: objc.loadFunctionList(self.__inlinelist, d, func_list, skip_undefined=True) if name in d: return d[name] if self.__expressions: if name in self.__expressions: # NOTE: 'name' is popped because it is no longer needed # in the metadata and popping should slightly reduce # memory usage. info = self.__expressions.pop(name) try: return eval(info, {}, self.__expressions_mapping) except: # Ignore all errors in evaluation the expression. pass if self.__aliases: if name in self.__aliases: alias = self.__aliases.pop(name) if alias == 'ULONG_MAX': return (sys.maxsize * 2) + 1 elif alias == 'LONG_MAX': return sys.maxsize elif alias == 'LONG_MIN': return -sys.maxsize - 1 elif alias == 'DBL_MAX': return sys.float_info.max elif alias == 'DBL_MIN': return sys.float_info.min elif alias == 'FLT_MAX': return objc._FLT_MAX elif alias == 'FLT_MIN': return objc._FLT_MIN return getattr(self, alias) raise AttributeError(name)
def __calc_all(self): # Ensure that all dynamic entries get loaded if self.__varmap_dct: dct = {} objc.loadBundleVariables( self.__bundle, dct, [(nm, self.__varmap_dct[nm].encode('ascii')) for nm in self.__varmap_dct if not self.__varmap_dct[nm].startswith('=')]) for nm in dct: if nm not in self.__dict__: self.__dict__[nm] = dct[nm] for nm, tp in self.__varmap_dct.items(): if tp.startswith('='): try: self.__dict__[nm] = objc._loadConstant( nm, tp[1:], True) except AttributeError: pass self.__varmap_dct = {} if self.__varmap: varmap = [] specials = [] for nm, tp in re.findall(r"\$([A-Z0-9a-z_]*)(@[^$]*)?(?=\$)", self.__varmap): if tp and tp.startswith('@='): specials.append((nm, tp[2:])) else: varmap.append( (nm, b'@' if not tp else tp[1:].encode('ascii'))) dct = {} objc.loadBundleVariables(self.__bundle, dct, varmap) for nm in dct: if nm not in self.__dict__: self.__dict__[nm] = dct[nm] for nm, tp in specials: try: self.__dict__[nm] = objc._loadConstant(nm, tp, True) except AttributeError: pass self.__varmap = "" if self.__enummap: for nm, val in re.findall(r"\$([A-Z0-9a-z_]*)@([^$]*)(?=\$)", self.__enummap): if nm not in self.__dict__: self.__dict__[nm] = self.__prs_enum(val) self.__enummap = "" if self.__funcmap: func_list = [] for nm in self.__funcmap: if nm not in self.__dict__: func_list.append((nm, ) + self.__funcmap[nm]) dct = {} objc.loadBundleFunctions(self.__bundle, dct, func_list) for nm in dct: if nm not in self.__dict__: self.__dict__[nm] = dct[nm] if self.__inlinelist is not None: dct = {} objc.loadFunctionList(self.__inlinelist, dct, func_list, skip_undefined=True) for nm in dct: if nm not in self.__dict__: self.__dict__[nm] = dct[nm] self.__funcmap = {} if self.__expressions: for nm in list(self.__expressions): try: getattr(self, nm) except AttributeError: pass if self.__aliases: for nm in list(self.__aliases): try: getattr(self, nm) except AttributeError: pass all_names = set() # Add all names that are already in our __dict__ all_names.update(self.__dict__) # Merge __all__of parents ('from parent import *') for p in self.__parents: try: all_names.update(p.__all__) except AttributeError: all_names.update(dir(p)) # Add all class names all_names.update(cls.__name__ for cls in getClassList()) return [v for v in all_names if not v.startswith('_')]
("swapX_andY_", b'v^d^d', '', dict(arguments={ 0: dict(type_modifier=objc._C_INOUT, null_accepted=False), 1: dict(type_modifier=objc._C_INOUT, null_accepted=False), })), ("input_output_inputAndOutput_", b'@^i^i^i', '', dict(arguments={ 0: dict(type_modifier=objc._C_IN, null_accepted=True), 1: dict(type_modifier=objc._C_OUT, null_accepted=True), 2: dict(type_modifier=objc._C_INOUT, null_accepted=True), })), ] objc.loadFunctionList(function_list, globals(), _FunctionTable, False) class TestExists (TestCase): def testFunctionsExists(self): for item in _FunctionTable: self.assertIn(item[0], globals()) class TestArrayDefault (TestCase): # TODO: what is the default anyway? pass class TestArraysOut (TestCase): def testFixedSize(self): v = fill4Tuple_(None) self.assertEqual(list(v), [0, -1, -8, -27])
objc.registerMetaDataForSelector(b"OCTestDeprecations", b"method9", dict(deprecated=1010)) _FunctionTable = [ ("func1", b'i', '', dict()), ("func2", b'i', '', dict(deprecated=1004)), ("func3", b'i', '', dict(deprecated=1004)), ("func4", b'i', '', dict(deprecated=1005)), ("func5", b'i', '', dict(deprecated=1005)), ("func6", b'i', '', dict(deprecated=1006)), ("func7", b'i', '', dict(deprecated=1006)), ("func8", b'i', '', dict(deprecated=1010)), ("func9", b'i', '', dict(deprecated=1010)), ] objc.loadFunctionList(function_list, globals(), _FunctionTable, False) @contextlib.contextmanager def deprecation_warnings(level): orig = objc.options.deprecation_warnings try: objc.options.deprecation_warnings = level yield finally: objc.options.deprecation_warnings = orig class TestDeprecationWarnings(TestCase): def assertDeprecationWarning(self, func): with warnings.catch_warnings(record=True) as w:
def __get_constant(self, name): if self.__varmap_dct: if name in self.__varmap_dct: tp = self.__varmap_dct.pop(name) if tp.startswith("=="): tp = tp[2:] magic = 2 elif tp.startswith("="): tp = tp[1:] magic = 1 else: magic = 0 result = objc._loadConstant(name, tp, magic) if name in self.__varmap_deprecated: _check_deprecated(name, self.__varmap_deprecated[name]) return result if self.__varmap: m = re.search(r"\$%s(@[^$]*)?\$" % (name,), self.__varmap) if m is not None: tp = m.group(1) if not tp: tp = "@" else: tp = tp[1:] d = {} if tp.startswith("=="): magic = 2 tp = tp[2:] elif tp.startswith("="): tp = tp[1:] magic = 1 else: magic = 0 result = objc._loadConstant(name, tp, magic) if name in self.__varmap_deprecated: _check_deprecated(name, self.__varmap_deprecated[name]) return result if self.__enummap: m = re.search(r"\$%s@([^$]*)\$" % (name,), self.__enummap) if m is not None: result = self.__prs_enum(m.group(1)) if name in self.__enum_deprecated: _check_deprecated(name, self.__enum_deprecated[name]) return result if self.__funcmap: if name in self.__funcmap: # NOTE: Remove 'name' from funcmap because # it won't be needed anymore (either the # function doesn't exist, or it is loaded) # Should use slightly less memory. info = self.__funcmap.pop(name) func_list = [(name,) + info] d = {} objc.loadBundleFunctions(self.__bundle, d, func_list) if name in d: return d[name] if self.__inlinelist is not None: objc.loadFunctionList( self.__inlinelist, d, func_list, skip_undefined=True ) if name in d: return d[name] if self.__expressions: if name in self.__expressions: # NOTE: 'name' is popped because it is no longer needed # in the metadata and popping should slightly reduce # memory usage. info = self.__expressions.pop(name) try: return eval(info, {}, self.__expressions_mapping) except: # noqa: E722, B001. Ignore all errors in evaluation the expression. pass if self.__aliases: if name in self.__aliases: alias = self.__aliases.pop(name) if alias == "ULONG_MAX": result = (sys.maxsize * 2) + 1 elif alias == "LONG_MAX": result = sys.maxsize elif alias == "LONG_MIN": result = -sys.maxsize - 1 elif alias == "DBL_MAX": result = sys.float_info.max elif alias == "DBL_MIN": result = sys.float_info.min elif alias == "FLT_MAX": result = objc._FLT_MAX elif alias == "FLT_MIN": result = objc._FLT_MIN elif alias == "objc.NULL": result = objc.NULL elif alias == "objc.UINT32_MAX": result = 0xFFFFFFFF else: result = getattr(self, alias) if name in self.__aliases_deprecated: _check_deprecated(name, self.__aliases_deprecated[name]) return result raise AttributeError(name)
def parseBridgeSupport(xmldata, globals, frameworkName, dylib_path=None, inlineTab=None): if dylib_path: lib = ctypes.cdll.LoadLibrary(dylib_path) _libraries.append(lib) objc._updatingMetadata(True) try: prs = _BridgeSupportParser(xmldata, frameworkName) globals.update(prs.values) for entry in prs.cftypes: tp = objc.registerCFSignature(*entry) globals[entry[0]] = tp for name, typestr in prs.opaque: globals[name] = objc.createOpaquePointerType(name, typestr) for name, typestr, alias in prs.structs: if alias is not None: globals[name] = alias objc.createStructAlias(name, typestr, alias) else: globals[name] = value = objc.createStructType(name, typestr, None) for name, typestr, magic in prs.constants: try: value = objc._loadConstant(name, typestr, magic) except AttributeError: continue globals[name] = value for class_name, sel_name, is_class in prs.meta: objc.registerMetaDataForSelector(class_name, sel_name, prs.meta[(class_name, sel_name, is_class)]) for name, method_list in prs.informal_protocols: proto = objc.informal_protocol(name, method_list) # XXX: protocols submodule should be deprecated if "protocols" not in globals: mod_name = "%s.protocols"%(frameworkName,) m = globals["protocols"] = type(objc)(mod_name) sys.modules[mod_name] = m else: m = globals["protocols"] setattr(m, name, proto) if prs.functions: objc.loadBundleFunctions(None, globals, prs.functions) if inlineTab is not None: objc.loadFunctionList(inlineTab, globals, prs.functions) for name, orig in prs.func_aliases: try: globals[name] = globals[orig] except KeyError: pass finally: objc._updatingMetadata(False)
def __get_constant(self, name): # FIXME: Loading variables and functions requires too much # code at the moment, the objc API can be adjusted for # this later on. if self.__varmap_dct: if name in self.__varmap_dct: tp = self.__varmap_dct[name] return objc._loadConstant(name, tp, False) if self.__varmap: m = re.search(r"\$%s(@[^$]*)?\$" % (name, ), self.__varmap) if m is not None: tp = m.group(1) if tp is None: tp = '@' else: tp = tp[1:] d = {} if tp.startswith('='): tp = tp[1:] magic = True else: magic = False #try: return objc._loadConstant(name, tp, magic) #except Exception as exc: # print "LOAD %r %r %r -> raise %s"%(name, tp, magic, exc) # raise if self.__enummap: m = re.search(r"\$%s@([^$]*)\$" % (name, ), self.__enummap) if m is not None: val = m.group(1) if val.startswith("'"): if isinstance(val, bytes): # Python 2.x val, = struct.unpack('>l', val[1:-1]) else: # Python 3.x val, = struct.unpack('>l', val[1:-1].encode('latin1')) elif '.' in val: val = float(val) else: val = int(val) return val if self.__funcmap: if name in self.__funcmap: info = self.__funcmap[name] func_list = [(name, ) + info] d = {} objc.loadBundleFunctions(self.__bundle, d, func_list) if name in d: return d[name] if self.__inlinelist is not None: try: objc.loadFunctionList(self.__inlinelist, d, func_list, skip_undefined=False) except objc.error: pass else: if name in d: return d[name] if self.__expressions: if name in self.__expressions: info = self.__expressions[name] try: return eval(info, {}, self.__expressions_mapping) except NameError: pass if self.__aliases: if name in self.__aliases: alias = self.__aliases[name] if alias == 'ULONG_MAX': return (sys.maxsize * 2) + 1 elif alias == 'LONG_MAX': return sys.maxsize elif alias == 'LONG_MIN': return -sys.maxsize - 1 return getattr(self, alias) raise AttributeError(name)
def parseBridgeSupport(xmldata, globals, frameworkName, dylib_path=None, inlineTab=None): if dylib_path: lib = ctypes.cdll.LoadLibrary(dylib_path) _libraries.append(lib) objc._updatingMetadata(True) try: prs = _BridgeSupportParser(xmldata, frameworkName) globals.update(prs.values) for entry in prs.cftypes: tp = objc.registerCFSignature(*entry) globals[entry[0]] = tp for name, typestr in prs.opaque: globals[name] = objc.createOpaquePointerType(name, typestr) for name, typestr, alias in prs.structs: if alias is not None: globals[name] = alias objc.createStructAlias(name, typestr, alias) else: globals[name] = value = objc.createStructType( name, typestr, None) for name, typestr, magic in prs.constants: try: value = objc._loadConstant(name, typestr, magic) except AttributeError: continue globals[name] = value for class_name, sel_name, is_class in prs.meta: objc.registerMetaDataForSelector( class_name, sel_name, prs.meta[(class_name, sel_name, is_class)]) for name, method_list in prs.informal_protocols: proto = objc.informal_protocol(name, method_list) # XXX: protocols submodule should be deprecated if "protocols" not in globals: mod_name = "%s.protocols" % (frameworkName, ) m = globals["protocols"] = type(objc)(mod_name) sys.modules[mod_name] = m else: m = globals["protocols"] setattr(m, name, proto) if prs.functions: objc.loadBundleFunctions(None, globals, prs.functions) if inlineTab is not None: objc.loadFunctionList(inlineTab, globals, prs.functions) for name, orig in prs.func_aliases: try: globals[name] = globals[orig] except KeyError: pass finally: objc._updatingMetadata(False)
def __get_constant(self, name): if self.__varmap_dct: if name in self.__varmap_dct: tp = self.__varmap_dct.pop(name) if tp.startswith('='): tp = tp[1:] magic = True else: magic = False result = objc._loadConstant(name, tp, magic) return result if self.__varmap: m = re.search(r"\$%s(@[^$]*)?\$"%(name,), self.__varmap) if m is not None: tp = m.group(1) if not tp: tp = '@' else: tp = tp[1:] d = {} if tp.startswith('='): tp = tp[1:] magic = True else: magic = False return objc._loadConstant(name, tp, magic) if self.__enummap: m = re.search(r"\$%s@([^$]*)\$"%(name,), self.__enummap) if m is not None: return self.__prs_enum(m.group(1)) if self.__funcmap: if name in self.__funcmap: # NOTE: Remove 'name' from funcmap because # it won't be needed anymore (either the # function doesn't exist, or it is loaded) # Should use slightly less memory. info = self.__funcmap.pop(name) func_list = [ (name,) + info ] d = {} objc.loadBundleFunctions(self.__bundle, d, func_list) if name in d: return d[name] if self.__inlinelist is not None: objc.loadFunctionList( self.__inlinelist, d, func_list, skip_undefined=True) if name in d: return d[name] if self.__expressions: if name in self.__expressions: # NOTE: 'name' is popped because it is no longer needed # in the metadata and popping should slightly reduce # memory usage. info = self.__expressions.pop(name) try: return eval(info, {}, self.__expressions_mapping) except: # Ignore all errors in evaluation the expression. pass if self.__aliases: if name in self.__aliases: alias = self.__aliases.pop(name) if alias == 'ULONG_MAX': return (sys.maxsize * 2) + 1 elif alias == 'LONG_MAX': return sys.maxsize elif alias == 'LONG_MIN': return -sys.maxsize-1 return getattr(self, alias) raise AttributeError(name)
def __calc_all(self): # Ensure that all dynamic entries get loaded if self.__varmap_dct: dct = {} objc.loadBundleVariables(self.__bundle, dct, [ (nm, self.__varmap_dct[nm].encode('ascii')) for nm in self.__varmap_dct if not self.__varmap_dct[nm].startswith('=')]) for nm in dct: if nm not in self.__dict__: self.__dict__[nm] = dct[nm] for nm, tp in self.__varmap_dct.items(): if tp.startswith('='): try: self.__dict__[nm] = objc._loadConstant(nm, tp[1:], True) except AttributeError: pass self.__varmap_dct = {} if self.__varmap: varmap = [] specials = [] for nm, tp in re.findall(r"\$([A-Z0-9a-z_]*)(@[^$]*)?(?=\$)", self.__varmap): if tp and tp.startswith('@='): specials.append((nm, tp[2:])) else: varmap.append((nm, b'@' if not tp else tp[1:].encode('ascii'))) dct = {} objc.loadBundleVariables(self.__bundle, dct, varmap) for nm in dct: if nm not in self.__dict__: self.__dict__[nm] = dct[nm] for nm, tp in specials: try: self.__dict__[nm] = objc._loadConstant(nm, tp, True) except AttributeError: pass self.__varmap = "" if self.__enummap: for nm, val in re.findall(r"\$([A-Z0-9a-z_]*)@([^$]*)(?=\$)", self.__enummap): if nm not in self.__dict__: self.__dict__[nm] = self.__prs_enum(val) self.__enummap = "" if self.__funcmap: func_list = [] for nm in self.__funcmap: if nm not in self.__dict__: func_list.append((nm,) + self.__funcmap[nm]) dct = {} objc.loadBundleFunctions(self.__bundle, dct, func_list) for nm in dct: if nm not in self.__dict__: self.__dict__[nm] = dct[nm] if self.__inlinelist is not None: dct = {} objc.loadFunctionList( self.__inlinelist, dct, func_list, skip_undefined=True) for nm in dct: if nm not in self.__dict__: self.__dict__[nm] = dct[nm] self.__funcmap = {} if self.__expressions: for nm in list(self.__expressions): try: getattr(self, nm) except AttributeError: pass if self.__aliases: for nm in list(self.__aliases): try: getattr(self, nm) except AttributeError: pass all_names = set() # Add all names that are already in our __dict__ all_names.update(self.__dict__) # Merge __all__of parents ('from parent import *') for p in self.__parents: try: all_names.update(p.__all__) except AttributeError: all_names.update(dir(p)) # Add all class names all_names.update(cls.__name__ for cls in getClassList()) return [ v for v in all_names if not v.startswith('_') ]
def _loadFunctionList(*args, **kwds): warnings.warn("Deprecated: use loadFunctionList()", DeprecationWarning) import objc objc.loadFunctionList(*args, **kwds)