Beispiel #1
0
 def __init__(self,
              object_or_type,
              attribute_dict=None,
              strict=True,
              **attributes):
     if attributes:
         if attribute_dict is None:
             attribute_dict = attributes
         else:
             attribute_dict.update(attributes)
     if isinstance(object_or_type, basestring):
         container, method, name, c = resolve(object_or_type)
         if c is not_there:
             raise AttributeError('%r could not be resolved' % object_or_type)
     elif isinstance(object_or_type, (ClassType, type)):
         c = object_or_type
     elif isinstance(object_or_type, BaseException):
         c = object_or_type.__class__
         if attribute_dict is None:
             attribute_dict = vars(object_or_type)
             attribute_dict['args'] = object_or_type.args
     else:
         c = object_or_type.__class__
         if attribute_dict is None:
             attribute_dict=vars(object_or_type)
     self.c = c
     self.v = attribute_dict
     self.strict = strict
Beispiel #2
0
 def __init__(self,
              object_or_type,
              attribute_dict=None,
              strict=True,
              **attributes):
     if attributes:
         if attribute_dict is None:
             attribute_dict = attributes
         else:
             attribute_dict.update(attributes)
     if isinstance(object_or_type, basestring):
         container, method, name, c = resolve(object_or_type)
         if c is not_there:
             raise AttributeError('%r could not be resolved' %
                                  object_or_type)
     elif isinstance(object_or_type, (ClassType, type)):
         c = object_or_type
     elif isinstance(object_or_type, BaseException):
         c = object_or_type.__class__
         if attribute_dict is None:
             attribute_dict = vars(object_or_type)
             attribute_dict['args'] = object_or_type.args
     else:
         c = object_or_type.__class__
         if attribute_dict is None:
             attribute_dict = vars(object_or_type)
     self.c = c
     self.v = attribute_dict
     self.strict = strict
Beispiel #3
0
    def __call__(self, target, replacement, strict=True):
        """
        Replace the specified target with the supplied replacement.
        """

        container, method, attribute, t_obj = resolve(target)
        if method is None:
            raise ValueError('target must contain at least one dot!')
        if t_obj is not_there and strict:
            raise AttributeError('Original %r not found' % attribute)
        if t_obj is not_there and replacement is not_there:
            return not_there

        replacement_to_use = replacement

        if isinstance(container, (type, ClassType)):

            if not_same_descriptor(t_obj, replacement, classmethod):
                replacement_to_use = classmethod(replacement)

            elif not_same_descriptor(t_obj, replacement, staticmethod):
                replacement_to_use = staticmethod(replacement)

        self._replace(container, attribute, method, replacement_to_use, strict)
        if target not in self.originals:
            self.originals[target] = t_obj
        return replacement
Beispiel #4
0
    def replace(self,target,replacement,strict=True):
        """
        Replace the specified target with the supplied replacement.

        :param target: A string containing the dotted-path to the
                       object to be replaced. This path may specify a
                       module in a package, an attribute of a module,
                       or any attribute of something contained within
                       a module.

        :param replacement: The object to use as a replacement.

        :param strict: When `True`, an exception will be raised if an
                       attempt is made to replace an object that does
                       not exist.
        """
        container, method, attribute, t_obj = resolve(target)
        if method is None:
            raise ValueError('target must contain at least one dot!')
        if t_obj is not_there and strict:
            raise AttributeError('Original %r not found'%attribute)
        if t_obj is not_there and replacement is not_there:
            return not_there
        if (isinstance(t_obj,MethodType)
            and getattr(t_obj, self_name) is container
            and not isinstance(replacement,MethodType)):
            replacement_to_use = classmethod(replacement)
        else:
            replacement_to_use = replacement
        self._replace(container, attribute, method, replacement_to_use, strict)
        if target not in self.originals:
            self.originals[target] = t_obj
        if self.replace_returns:
            return replacement
Beispiel #5
0
    def __call__(self, target, replacement, strict=True):
        """
        Replace the specified target with the supplied replacement.
        """

        container, method, attribute, t_obj = resolve(target)
        if method is None:
            raise ValueError('target must contain at least one dot!')
        if t_obj is not_there and strict:
            raise AttributeError('Original %r not found' % attribute)
        if t_obj is not_there and replacement is not_there:
            return not_there

        replacement_to_use = replacement

        if isinstance(container, (type, ClassType)):

            if not_same_descriptor(t_obj, replacement, classmethod):
                replacement_to_use = classmethod(replacement)

            elif not_same_descriptor(t_obj, replacement, staticmethod):
                replacement_to_use = staticmethod(replacement)

        self._replace(container, attribute, method, replacement_to_use, strict)
        if target not in self.originals:
            self.originals[target] = t_obj
        return replacement
Beispiel #6
0
    def replace(self, target, replacement, strict=True, all=False):
        """
        Replace the specified target with the supplied replacement.

        :param target: A string containing the dotted-path to the
                       object to be replaced. This path may specify a
                       module in a package, an attribute of a module,
                       or any attribute of something contained within
                       a module.

        :param replacement: The object to use as a replacement.

        :param strict: When `True`, an exception will be raised if an
                       attempt is made to replace an object that does
                       not exist.

        :param all: When `True`, the :mod:`gc` module to replace _all_ 
                    references of :param:`target` with :param:`replacement`.
                    There are few places the object cannot be replaced (e.g
                    within a frame object) but most instances of the obj
                    -- e.g. within a list, dict, nested datastructure, function
                    closure -- will be replaced.  See documention for all 
                    full notes when using this option. 

        """
        container, method, attribute, t_obj = resolve(target)
        if method is None:
            raise ValueError('target must contain at least one dot!')

        if t_obj is not_there and (strict or all):
            raise AttributeError('Original %r not found' % attribute)

        if replacement is not_there and all:
            raise TypeError("You cannot use 'not_there' with the all keyword")

        # Make sure when using 'all' that target/replacement not scalars --
        # very unsafe and may not be reversible to do so.
        if all and isinstance(t_obj, _SCALAR_TYPES):
            raise TypeError(
                "The target cannot be a scalar type when using 'all'.")
        if all and isinstance(replacement, _SCALAR_TYPES):
            raise TypeError(
                "The replacement cannot be a scalar type when using 'all'.")

        if t_obj is not_there and replacement is not_there:
            return not_there
        if (isinstance(t_obj, MethodType)
            and getattr(t_obj, self_name) is container
            and not isinstance(replacement, MethodType)):
            replacement_to_use = classmethod(replacement)
        else:
            replacement_to_use = replacement

        if all:
            _replace_all_refs(org_obj=t_obj, new_obj=replacement)
        else:
            self._replace(container, attribute, method, replacement_to_use)
        self.originals[target] = (all, t_obj, replacement,)
        if self.replace_returns:
            return replacement
Beispiel #7
0
 def restore(self):
     """
     Restore all the original objects that have been replaced by
     calls to the :meth:`replace` method of this :class:`Replacer`.
     """
     for target,original in tuple(self.originals.items()):
         container, method, attribute, found = resolve(target)
         self._replace(container, attribute, method, original, strict=False)
         del self.originals[target]
Beispiel #8
0
 def restore(self):
     """
     Restore all the original objects that have been replaced by
     calls to the :meth:`replace` method of this :class:`Replacer`.
     """
     for target, original in tuple(self.originals.items()):
         container, method, attribute, found = resolve(target)
         self._replace(container, attribute, method, original, strict=False)
         del self.originals[target]
Beispiel #9
0
 def restore(self):
     """
     Restore all the original objects that have been replaced by
     calls to the :meth:`replace` method of this :class:`Replacer`.
     """
     for target, original in tuple(self.originals.items()):
         replace_all, original_object, replacement = original
         if replace_all:
             _replace_all_refs(org_obj=replacement, new_obj=original_object)
         else:
             container, method, attribute, found = resolve(target)
             self._replace(container, attribute, method, original_object)
         del self.originals[target]
Beispiel #10
0
    def replace(self, target, replacement, strict=True):
        """
        Replace the specified target with the supplied replacement.

        :param target: A string containing the dotted-path to the
                       object to be replaced. This path may specify a
                       module in a package, an attribute of a module,
                       or any attribute of something contained within
                       a module.

        :param replacement: The object to use as a replacement.

        :param strict: When `True`, an exception will be raised if an
                       attempt is made to replace an object that does
                       not exist.
        """
        container, method, attribute, t_obj = resolve(target)
        if method is None:
            raise ValueError('target must contain at least one dot!')
        if t_obj is not_there and strict:
            raise AttributeError('Original %r not found' % attribute)
        if t_obj is not_there and replacement is not_there:
            return not_there
        replacement_to_use = replacement
        if isinstance(container, (type, ClassType)):
            if (isinstance(t_obj, classmethod)
                    and not isinstance(replacement, classmethod)):
                replacement_to_use = classmethod(replacement)
            elif (isinstance(t_obj, staticmethod)
                  and not isinstance(replacement, staticmethod)):
                replacement_to_use = staticmethod(replacement)

        self._replace(container, attribute, method, replacement_to_use, strict)
        if target not in self.originals:
            self.originals[target] = t_obj
        if self.replace_returns:
            return replacement
Beispiel #11
0
 def __init__(self,
              object_or_type,
              attribute_dict=None,
              partial=False,
              **attributes):
     self.partial = partial or not attributes.pop('strict', True)
     if attributes:
         if attribute_dict is None:
             attribute_dict = attributes
         else:
             attribute_dict.update(attributes)
     if isinstance(object_or_type, basestring):
         container, method, name, c = resolve(object_or_type)
         if c is not_there:
             raise AttributeError('%r could not be resolved' %
                                  object_or_type)
     elif isinstance(object_or_type, (ClassType, type)):
         c = object_or_type
     else:
         c = object_or_type.__class__
         if attribute_dict is None:
             attribute_dict = _extract_attrs(object_or_type)
     self.expected_type = c
     self.expected_attributes = attribute_dict
Beispiel #12
0
 def restore(self):
     for target, original in tuple(self.originals.items()):
         container, method, attribute, found = resolve(target)
         self._replace(container, attribute, method, original, strict=False)
         del self.originals[target]