def __new__(cls, name, bases, methods): if len(bases) != 1: raise TypeError("Cannot have multiple inheritance with Categories") c = bases[0].real_class if c.__name__ != name: raise TypeError("Category name must be same as class name") m = [ x[1] for x in methods.items() if x[0] not in cls._IGNORENAMES and isinstance( x[1], (FunctionType, MethodType, selector, classmethod)) ] vars = [ x for x in methods.items() if x[0] not in cls._IGNORENAMES and not isinstance( x[1], (FunctionType, MethodType, selector, classmethod)) ] for k, v in vars: if isinstance(v, ivar): raise TypeError("Cannot add instance variables in a Category") classAddMethods(c, m) for k, v in vars: setattr(c, k, v) return c
def __new__(cls, name, bases, methods): if len(bases) != 1: raise TypeError("Cannot have multiple inheritance with Categories") c = bases[0].real_class if c.__name__ != name: raise TypeError("Category name must be same as class name") m = [ x[1] for x in methods.iteritems() if x[0] not in cls._IGNORENAMES and isinstance(x[1], (FunctionType, MethodType, selector, classmethod))] vars = [ x for x in methods.iteritems() if x[0] not in cls._IGNORENAMES and not isinstance(x[1], (FunctionType, MethodType, selector, classmethod))] for k, v in vars: if isinstance(v, ivar): raise TypeError("Cannot add instance variables in a Category") classAddMethods(c, m) for k, v in vars: setattr(c, k, v) return c
def classAddMethod(cls, name, method): """ Add a single method to a class. 'name' is the ObjC selector """ if isinstance(method, selector): sel = selector(method.callable, selector=name, signature=method.signature, isClassMethod=method.isClassMethod) else: sel = selector(method, selector=name) return classAddMethods(cls, [sel])