def testClassList(self): NSObject = objc.lookUpClass("NSObject") NSException = objc.lookUpClass("NSException") NSMutableArray = objc.lookUpClass("NSMutableArray") self.assertIn(NSObject, objc.getClassList()) self.assertIn(NSException, objc.getClassList()) self.assertIn(NSMutableArray, objc.getClassList())
def testClassList(self): ###! This test should probably be moved down to the Foundation test suite... NSObject = objc.lookUpClass('NSObject') NSException = objc.lookUpClass('NSException') NSMutableArray = objc.lookUpClass('NSMutableArray') self.assertIn(NSObject, objc.getClassList()) self.assertIn(NSException, objc.getClassList()) self.assertIn(NSMutableArray, objc.getClassList())
def testClassList(self): ###! This test should probably be moved down to the Foundation test suite... NSObject = objc.lookUpClass('NSObject') NSException = objc.lookUpClass('NSException') NSMutableArray = objc.lookUpClass('NSMutableArray') self.assert_(NSObject in objc.getClassList(), "getClassList() does not appear to contain NSObject class") self.assert_(NSException in objc.getClassList(), "getClassList() does not appear to contain NSException class") self.assert_(NSMutableArray in objc.getClassList(), "getClassList() does not appear to contain NSMutableArray class")
def testClassList(self): ###! This test should probably be moved down to the Foundation test suite... NSObject = objc.lookUpClass("NSObject") NSException = objc.lookUpClass("NSException") NSMutableArray = objc.lookUpClass("NSMutableArray") self.assertIn(NSObject, objc.getClassList()) self.assertIn(NSException, objc.getClassList()) self.assertIn(NSMutableArray, objc.getClassList())
def awakeFromNib(self): classes = getClassList() rootClasses = [] for cls in classes: if cls.__base__ == objc_object: rootClasses.append(cls) rootClasses = _sortClasses(rootClasses) self.columns = [rootClasses] self.browser.setMaxVisibleColumns_(7) self.browser.setMinColumnWidth_(150) self.selectedClass = None self.selectedClassMethods = None
def __getattr__(self, name): warnings.warn("Deprecated: use objc.lookUpClass", DeprecationWarning) import objc if name == '__objc_classes__': return objc.getClassList() elif name == '__kind__': return 'python' try: return objc.lookUpClass(name) except objc.nosuchclass_error: raise AttributeError(name)
def testSplitSignature(self): # This is a very expensive test, with 1 goal: Verify that all method # signatures, and therefore all signatures changed by PyObjC, are # valid. for cls in objc.getClassList(): for selName in cls.__dict__.keys(): try: sel = getattr(cls, selName.decode('latin1')) except AttributeError: continue if not isinstance(sel, objc.selector): continue elems = objc.splitSignature(sel.signature)
def testSplitSignature(self): # This is a very expensive test, with 1 goal: Verify that all method # signatures, and therefore all signatures changed by PyObjC, are # valid. for cls in objc.getClassList(): for selName in list(cls.__dict__.keys()): try: sel = getattr(cls, selName.decode('latin1')) except AttributeError: continue if not isinstance(sel, objc.selector): continue elems = objc.splitSignature(sel.signature)
def testSignatureCount(self): EXCEPTIONS = [ # For some reason this signature doesn't seem to be correct, even # though we don't touch it... "initWithDocument_URL_windowProperties_locationProperties_interpreterBuiltins_", # Some unittests... "setKey4", "get_key2", "read_bar", "setFoo_", "method_with_embedded_underscores", "methodWithArg_", "myMethod", "twoargs", "set_helper", "callback", # dictionary methods "get", "has_key", ] for cls in objc.getClassList(): if cls.__name__.startswith('OC'): continue for selName in cls.__dict__.keys(): self.assertIsInstance(selName, str) if selName in EXCEPTIONS: continue if selName.startswith('__') and selName.endswith('__'): continue try: sel = getattr(cls, selName) except (AttributeError, TypeError): continue if not isinstance(sel, objc.selector): continue elems = objc.splitSignature(sel.signature) argcount = len(elems) - 3 # retval, self, _sel coloncount = sel.selector.count(b':') self.assertEqual( argcount, coloncount, '%s [%d:%d] %r %r' % (sel.selector.decode('latin1'), argcount, coloncount, elems, cls))
def testSignatureCount(self): EXCEPTIONS=[ # For some reason this signature doesn't seem to be correct, even # though we don't touch it... "initWithDocument_URL_windowProperties_locationProperties_interpreterBuiltins_", # Some unittests... "setKey4", "get_key2", "read_bar", "setFoo_", "method_with_embedded_underscores", "methodWithArg_", "myMethod", "twoargs", "set_helper", "callback", # dictionary methods "get", "has_key", ] for cls in objc.getClassList(): #if cls.__name__.startswith('OC_'): continue if cls.__name__.startswith('OC'): continue for selName in cls.__dict__.keys(): self.assertIsInstance(selName, str) if selName in EXCEPTIONS: continue if selName.startswith('__') and selName.endswith('__'): continue try: sel = getattr(cls, selName) except (AttributeError, TypeError): continue if not isinstance(sel, objc.selector): continue elems = objc.splitSignature(sel.signature) argcount = len(elems) - 3 # retval, self, _sel coloncount = sel.selector.count(b':') self.assertEqual(argcount, coloncount, '%s [%d:%d] %r %r'%(sel.selector.decode('latin1'), argcount, coloncount, elems, cls))
def refreshClasses(self): self._classList = getClassList() self._classTree = {} self._methodInfo = [] for cls in self._classList: super = cls.__bases__[0] if super == objc_object: super = None else: super = super if not self._classTree.has_key(cls): self._classTree[cls] = [] if self._classTree.has_key(super): self._classTree[super].append(cls) else: self._classTree[super] = [cls] for lst in self._classTree.values(): lst.sort()
def refreshClasses(self): self._classList = getClassList() self._classTree = {} self._methodInfo = [] for cls in self._classList: super = cls.__bases__[0] if super == objc_object: super = None else: super = super if not self._classTree.has_key(cls): self._classTree[cls] = [] if self._classTree.has_key(super): self._classTree[super].append(cls) else: self._classTree[super] = [ cls ] for lst in self._classTree.values(): lst.sort()
def init(self): self._classInfo = getClassList() self.refreshClasses() return self
class Tests (unittest.TestCase): @os_level_between('10.5', '10.8') def testUntilLeopard(self): pass """ return onlyIf( os_level_key(min_release) <= os_level_key( os_release()) <= os_level_key(max_release), "Requires OSX %s upto %s" % (min_release, max_release)) _poolclass = objc.lookUpClass('NSAutoreleasePool') # NOTE: On at least OSX 10.8 there are multiple proxy classes for CFTypeRef... _nscftype = tuple(cls for cls in objc.getClassList() if 'NSCFType' in cls.__name__) _typealias = {} if not is32Bit(): _typealias[objc._C_LNG_LNG] = objc._C_LNG _typealias[objc._C_ULNG_LNG] = objc._C_ULNG else: # pragma: no cover (32-bit) _typealias[objc._C_LNG] = objc._C_INT _typealias[objc._C_ULNG] = objc._C_UINT class TestCase(_unittest.TestCase): """
def awakeFromNib(self): self._classInfo = getClassList() self.refreshClasses()
import Foundation import objc import CoreFoundation from PyObjCTools.TestSupport import TestCase, min_os_level MachPortClasses = tuple( cls for cls in objc.getClassList() if cls.__name__ == "NSMachPort" ) class TestMachPort(TestCase): def testTypes(self): try: if objc.lookUpClass("NSMachPort") is CoreFoundation.CFMachPortRef: return except objc.error: pass self.assertIsCFType(CoreFoundation.CFMachPortRef) def testTypeID(self): self.assertIsInstance(CoreFoundation.CFMachPortGetTypeID(), int) @min_os_level("10.8") def testCreate10_8(self): class Context: pass context = Context() def callout(port, msg, size, info): pass
@os_level_between('10.5', '10.8') def testUntilLeopard(self): pass """ return onlyIf( os_level_key(min_release) <= os_level_key(os_release()) <= os_level_key(max_release), "Requires OSX %s upto %s" % (min_release, max_release), ) _poolclass = objc.lookUpClass("NSAutoreleasePool") # NOTE: On at least OSX 10.8 there are multiple proxy classes for CFTypeRef... _nscftype = tuple(cls for cls in objc.getClassList() if "NSCFType" in cls.__name__) _typealias = {} if not is32Bit(): _typealias[objc._C_LNG_LNG] = objc._C_LNG _typealias[objc._C_ULNG_LNG] = objc._C_ULNG else: # pragma: no cover (32-bit) _typealias[objc._C_LNG] = objc._C_INT _typealias[objc._C_ULNG] = objc._C_UINT class TestCase(_unittest.TestCase): """ A version of TestCase that wraps every test into its own
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 __calc_all(self): all = set() # Ensure that all dynamic entries get loaded if self.__varmap_dct: for nm in self.__varmap_dct: try: getattr(self, nm) except AttributeError: pass if self.__varmap: for nm in re.findall(r"\$([A-Z0-9a-z_]*)(?:@[^$]*)?(?=\$)", self.__varmap): try: getattr(self, nm) except AttributeError: pass if self.__enummap: for nm in re.findall(r"\$([A-Z0-9a-z_]*)@[^$]*(?=\$)", self.__enummap): try: getattr(self, nm) except AttributeError: pass if self.__funcmap: for nm in self.__funcmap: try: getattr(self, nm) except AttributeError: pass if self.__expressions: for nm in self.__expressions: try: getattr(self, nm) except AttributeError: pass if self.__aliases: for nm in self.__aliases: try: getattr(self, nm) except AttributeError: pass # Add all names that are already in our __dict__ all.update(self.__dict__) # Merge __all__of parents ('from parent import *') for p in self.__parents: all.update(getattr(p, '__all__', ())) # Add all class names all.update(cls.__name__ for cls in getClassList()) return [v for v in all if not v.startswith('_')] return list(all)
def max_os_level(release): """ Usage:: class Tests (unittest.TestCase): @max_os_level('10.5') def testUntilLeopard(self): pass """ return onlyIf(os_release() <= release) _poolclass = objc.lookUpClass('NSAutoreleasePool') # NOTE: On at least OSX 10.8 there are multiple proxy classes for CFTypeRef... _nscftype = tuple(cls for cls in objc.getClassList() if 'NSCFType' in cls.__name__) _typealias = {} if not is32Bit(): _typealias[objc._C_LNG_LNG] = objc._C_LNG _typealias[objc._C_ULNG_LNG] = objc._C_ULNG else: # pragma: no cover (32-bit) _typealias[objc._C_LNG] = objc._C_INT _typealias[objc._C_ULNG] = objc._C_UINT class TestCase (_unittest.TestCase): """ A version of TestCase that wraps every test into its own autorelease pool.
def testFunctions(self): myInfo = object() callcount = [0] def callbackRefresh(count, rects, info): self.assertTrue(info is myInfo) self.assertIsInstance(rects, tuple) self.assertIsInstance(count, int) for i in rects: self.assertIsInstance(i, Quartz.CGRect) callcount[0] += 1 err = Quartz.CGRegisterScreenRefreshCallback(callbackRefresh, myInfo) self.assertEqual(err, 0) # FIXME: should force a refresh here Quartz.CGUnregisterScreenRefreshCallback(callbackRefresh, myInfo) # FIXME: This complete hangs the interpreter, don't have # time to investigate this. # Quartz.CGWaitForScreenRefreshRects # err, rects, count = Quartz.CGWaitForScreenRefreshRects(None, None) # self.assertEqual(err, 0) # self.assertIsInstance(rects, tuple) # self.assertIsInstance(count, int) # for i in rects: # self.assertIsInstance(i, Quartz.CGRect) v = Quartz.CGCursorIsVisible() self.assertIsInstance(v, int) v = Quartz.CGCursorIsDrawnInFramebuffer() self.assertIsInstance(v, int) v = Quartz.CGPostMouseEvent((50, 50), True, 3, 0, 0, 0) self.assertEqual(v, 0) v = Quartz.CGPostScrollWheelEvent(3, 0, 0, 0) self.assertEqual(v, 0) v = Quartz.CGPostKeyboardEvent(0, 56, 1) self.assertEqual(v, 0) v = Quartz.CGWarpMouseCursorPosition((800, 800)) self.assertEqual(v, 0) v = Quartz.CGInhibitLocalEvents(False) self.assertEqual(v, 0) v = Quartz.CGSetLocalEventsSuppressionInterval(0.1) self.assertEqual(v, 0) v = Quartz.CGEnableEventStateCombining(0) self.assertEqual(v, 0) v = Quartz.CGSetLocalEventsFilterDuringSuppressionState( Quartz.kCGEventFilterMaskPermitAllEvents, Quartz.kCGEventSuppressionStateSuppressionInterval, ) self.assertEqual(v, 0) v = Quartz.CGAssociateMouseAndMouseCursorPosition(0) self.assertEqual(v, 0) # For some reason there are 2 NSMachPort classes on OSX 10.8 classes = tuple(cls for cls in objc.getClassList() if cls.__name__ == "NSMachPort") v = Quartz.CGWindowServerCFMachPort() self.assertIsInstance(v, classes) self.assertTrue(Quartz.CGSetLocalEventsFilterDuringSupressionState is Quartz.CGSetLocalEventsFilterDuringSuppressionState)
def testIncompleteClass(self): self.assertRaises(TypeError, self.doIncompleteClass) self.assertRaises(objc.error, objc.lookUpClass, 'ProtoClass2') for cls in objc.getClassList(): self.assertNotEqual(cls.__name__, 'ProtoClass2')
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('_')]