This function will be deprecated.

    wrap_around wraps an unbound method (the first argument)
    inside an advice (function given as the second argument). When the
    method is called, the code of the advice is executed to the point
    of 'self.__proceed(*args,**keyw)'. Then the original method is
    executed after which the control returns to the advice.

    Keyword parameter instances is a container. When it is not empty,
    it specifies a set of id:s of instances with which the advice is
    executed. If the wrapped method is bound to an instance not in the
    set, the advice is omitted.

    Returns the number of the added wrap. The number can be used later
    on for enabling and disabling the wrap.

def wrap_around(method, advice, instances=[]):
    """
    This function will be deprecated.

    wrap_around wraps an unbound method (the first argument)
    inside an advice (function given as the second argument). When the
    method is called, the code of the advice is executed to the point
    of 'self.__proceed(*args,**keyw)'. Then the original method is
    executed after which the control returns to the advice.

    Keyword parameter instances is a container. When it is not empty,
    it specifies a set of id:s of instances with which the advice is
    executed. If the wrapped method is bound to an instance not in the
    set, the advice is omitted.

    Returns the number of the added wrap. The number can be used later
    on for enabling and disabling the wrap.
    """
    _permission_to_touch_wraps.acquire()
    try:
        methods_name, methods_class, _ = _names_mce(method)

        if methods_name[:2]=="__" and not hasattr(methods_class,methods_name):
            methods_name="_"+methods_class.__name__+methods_name

        # Check if __proceed method is implemented in the class
        try:
            getattr(methods_class,'__proceed')
        except:
            setattr(methods_class, '__proceed_stack', _proceed_stack_method)
            setattr(methods_class,'__proceed', _proceed_method)

        # Check how many times this method has been wrapped
        methodwrapcount = wrap_count(method)

        # Rename the original method: it will be __wrapped/n/origmethodname
        # where /n/ is the number of wraps around it
        wrapped_method_name = "__wrapped" + str(methodwrapcount) + methods_name
        orig_method = getattr( methods_class, methods_name)
        setattr(methods_class, wrapped_method_name, orig_method)

        # Add the wrap (that is, the advice) as a new method
        wrapper_method_name = "__wrap" + str(methodwrapcount) + methods_name
        setattr(methods_class, wrapper_method_name, advice)

        # Add __wrap_enabled/n/methodname attribute that is used for
        # enabling and disabling the wrap on the fly. By default the wrap
        # is enabled.
        enabled_attr_name = "__wrap_enabled" + str(methodwrapcount) + methods_name
        setattr(methods_class, enabled_attr_name, 1)

        # Generate condition: should this wrap be executed when the method is called
        cond_execute_wrap="self." + enabled_attr_name
        if len(instances)>0:
            instanceids_attr_name="__wrap_instanceids"+str(methodwrapcount)+methods_name
            if _python_version >= (2,4):
                setattr(methods_class,instanceids_attr_name,set([id(i) for i in instances]))
            else: # slower implementation for older pythons
                setattr(methods_class,instanceids_attr_name,[id(i) for i in instances])
            cond_execute_wrap="("+cond_execute_wrap+")" + \
                               "and (id(self) in self."+instanceids_attr_name+")"

        # Replace the original method by a method that
        # 1) sets which method should be executed in the next proceed call
        # 2) calls the wrap (advice)

        new_code = "def " + methods_name + "(self,*args,**keyw):\n" +\
                   "\tif not (" + cond_execute_wrap + "):\n" +\
                   "\t\treturn self." + wrapped_method_name + "(*args,**keyw)\n" +\
                   "\tstack=self.__proceed_stack()\n" +\
                   "\tstack.append( _Proceed_stack_entry(self." + wrapped_method_name + ",'" + methods_class.__name__ + "." + methods_name + "'))\n" +\
                   "\ttry: retval = self." + wrapper_method_name + "(*args,**keyw)\n" +\
                   "\tfinally:\n" +\
                   "\t\tstack.pop()\n" +\
                   "\t\tif len(stack)==0: _remove_proceed_stack(self)\n" +\
                   "\treturn retval\n"
        new_method = _create_function(new_code, methods_name)
        new_method.__aspects_wrapid=methodwrapcount
        new_method.__aspects_orig=orig_method
        # setattr(new_method,'__aspects_enabled',1)
        # new_method.__name__=methods_name
        setattr(methods_class, methods_name, new_method)
        return methodwrapcount
    finally: _permission_to_touch_wraps.release()