def set_has_ext_modules(dist): """ Set new function handler to dist object """ from types import MethodType as instancemethod if sys.version_info[0] == 2: m = instancemethod(has_ext_modules, dist, Distribution) else: m = instancemethod(has_ext_modules, dist) dist.has_ext_modules = m
def set_has_ext_modules(dist): """ Set new function handler to dist object """ from types import MethodType as instancemethod try: m = instancemethod(has_ext_modules, dist, Distribution) except TypeError: m = instancemethod(has_ext_modules, dist) dist.has_ext_modules = m
def applyCatalogPatch(portal): """Patch catalog """ catalog = getToolByName(portal, 'portal_catalog') klass = catalog.__class__ LOG.info('Patching catalog_object and uncatalog_object of %s' % catalog.absolute_url(1)) if hasattr(klass, '_atct_catalog_object'): raise RuntimeError, "%s already has _atct_catalog_object" % catalog if hasattr(klass, '_atct_uncatalog_object'): raise RuntimeError, "%s already has _atct_uncatalog_object" % catalog klass._atct_catalog_object = klass.catalog_object.im_func klass.catalog_object = instancemethod(catalog_object, None, klass) klass._atct_uncatalog_object = klass.uncatalog_object.im_func klass.uncatalog_object = instancemethod(uncatalog_object, None, klass) return klass
def applyCatalogPatch(portal): """Patch catalog """ catalog = getToolByName(portal, 'portal_catalog') klass = catalog.__class__ LOG.info('Patching catalog_object and uncatalog_object of %s' % catalog.absolute_url(1)) if hasattr(klass, '_atct_catalog_object'): raise RuntimeError("%s already has _atct_catalog_object" % catalog) if hasattr(klass, '_atct_uncatalog_object'): raise RuntimeError("%s already has _atct_uncatalog_object" % catalog) klass._atct_catalog_object = klass.catalog_object.im_func klass.catalog_object = instancemethod(catalog_object, None, klass) klass._atct_uncatalog_object = klass.uncatalog_object.im_func klass.uncatalog_object = instancemethod(uncatalog_object, None, klass) return klass
def __get__(self, instance, cls): """Descriptor protocol support. This makes an instance of this class function correctly when it is used to decorate a method on a user-defined class. If called as a bound method, we store the decorated function in the instance dictionary, so we will not be called again for that instance. If called as an unbound method, we store a reference to the decorated function internally and use it on future unbound method calls. """ if instance: deco = instancemethod(self._decorated(cls, instance), instance, cls) # This prevents us from being called again for this instance setattr(instance, self._func.__name__, deco) elif cls: if not self.__clsfunc: self.__clsfunc = instancemethod(self._decorated(cls), None, cls) deco = self.__clsfunc else: raise ValueError("Must supply instance or class to descriptor.") return deco
def create(self, maps = None, arg = None, initkw = None): """ Creates a new struct with extended properties :Parameters: - `maps`: The mappers to use (``{'membername': mapper, ...}``) - `arg`: Initializer argument for the descriptors - `initkw`: Initial values of members (``{'name', 'value', ...}``) :Types: - `maps`: ``dict`` - `arg`: any - `initkw`: ``dict`` :return: A new instance of the class :rtype: `Struct` :exception AssertionError: The parameter set was inconsistent """ cls = self._cls private = self._createPrivate() # merge alias maps to real maps = dict(maps or {}) for alias, real in self._aliases.items(): if maps.has_key(alias): maps[real] = maps[alias] del maps[alias] # add members space = self._generateMembers(private, maps, arg) space.update({'__module__': cls.__module__, '__slots__': self._names}) # create new class cls = self._createMetaClass(cls.__name__, (cls,), space) # add __special__s for name, func in self._generateSpecials(private).items(): setattr(cls, name, instancemethod(func, None, cls)) # return instance of the new class return cls(**(initkw or {}))
def __get__(self, obj, type=None): if obj is None: return self return instancemethod(self, obj)
def wrapped(*args): if args and type(args[0]) is not dict: self = args[0] args = args[1:] return wrapper(instancemethod(app, self), *args) return wrapper(app, *args)
def bind(func, inst): return instancemethod(func, inst, inst.__class__)
def set_has_ext_modules(dist): """ Set new function handler to dist object """ from types import MethodType as instancemethod m = instancemethod(has_ext_modules, dist, Distribution) dist.has_ext_modules = m