# Path: support/profiling.py
# def rename_function( function, name, filename=None, firstlineno=-1 ):
#     """
#     Rename a function and its associated code object.
#     
#     This is handy when using the @c profile and @c cProfile modules:
#     both retrieve the function names from the code object
#     (the @c co_name attribute); @c __name__ is ignored.
#     """
#     # Renaming a function in the profile thus requires generating a new
#     # code object. As CodeType.__doc__ notes, this is not for the
#     # faint of heart.
#     
#     # Modify the unbound function object of methods
#     if hasattr( function, "__func__" ):
#         function = function.__func__
# 
#     try:
#         code = function.__code__
#     
#     except AttributeError:
#         message = "expected '{func}' to have an associated code object ('__code__' attribute)" 
#         raise ValueError( message.format( function ) )
#     
#     # Copy old values if unspecified
#     if filename is None:
#         filename = code.co_filename
#     if firstlineno == -1:
#         firstlineno = code.co_firstlineno
#     
#     renamed_code = types.CodeType(
#                       code.co_argcount,
#                       code.co_kwonlyargcount,
#                       code.co_nlocals,
#                       code.co_stacksize,
#                       code.co_flags,
#                       code.co_code,
#                       code.co_consts,
#                       code.co_names,
#                       code.co_varnames,
#                       str( filename ),
#                       str( name ),
#                       int( firstlineno ),
#                       code.co_lnotab,
#                       code.co_freevars,
#                       code.co_cellvars
#                   )
# 
#     function.__name__ = str( name )
#     function.__code__ = renamed_code


from .profiling import rename_function
   ]



def __casting_wrapped( operation ):
    """
    Return the wrapped @p operation (without nesting the wrappers).
    
    This function is not intended for direct use; rather, employ
    @c operand_casting()
    """
    if hasattr( operation, "__wrapped_method__" ):
        return operation
    
    # A function outside operation_casting_new()'s scope is required
    # to avoid variable binding problems.
    # (If defined inside the scope, 'operation' points at the
    #  last used value for _all_ wrappers.)
    def casting_wrapper( self, other ):
        if self.__class__ is other.__class__:
            return operation( self, other )
        
        try:
            return operation( self, self.__class__( other ) )
        except TypeError:
            return NotImplemented
    
    # Rename the code object to reflect the wrapped operation's name.
    # Otherwise, the profile lists all wrapped operations
    # as 'wrapped_operation'.

# Path: support/profiling.py
# def rename_function( function, name, filename=None, firstlineno=-1 ):
#     """
#     Rename a function and its associated code object.
#     
#     This is handy when using the @c profile and @c cProfile modules:
#     both retrieve the function names from the code object
#     (the @c co_name attribute); @c __name__ is ignored.
#     """
#     # Renaming a function in the profile thus requires generating a new
#     # code object. As CodeType.__doc__ notes, this is not for the
#     # faint of heart.
#     
#     # Modify the unbound function object of methods
#     if hasattr( function, "__func__" ):
#         function = function.__func__
# 
#     try:
#         code = function.__code__
#     
#     except AttributeError:
#         message = "expected '{func}' to have an associated code object ('__code__' attribute)" 
#         raise ValueError( message.format( function ) )
#     
#     # Copy old values if unspecified
#     if filename is None:
#         filename = code.co_filename
#     if firstlineno == -1:
#         firstlineno = code.co_firstlineno
#     
#     renamed_code = types.CodeType(
#                       code.co_argcount,
#                       code.co_kwonlyargcount,
#                       code.co_nlocals,
#                       code.co_stacksize,
#                       code.co_flags,
#                       code.co_code,
#                       code.co_consts,
#                       code.co_names,
#                       code.co_varnames,
#                       str( filename ),
#                       str( name ),
#                       int( firstlineno ),
#                       code.co_lnotab,
#                       code.co_freevars,
#                       code.co_cellvars
#                   )
# 
#     function.__name__ = str( name )
#     function.__code__ = renamed_code


# Path: support/operators.py
from .profiling import rename_function

   ]



def __casting_wrapped( operation ):
    """
    Return the wrapped @p operation (without nesting the wrappers).
    
    This function is not intended for direct use; rather, employ
    @c operand_casting()
    """
    if hasattr( operation, "__wrapped_method__" ):
        return operation
    
    # A function outside operation_casting_new()'s scope is required
    # to avoid variable binding problems.
    # (If defined inside the scope, 'operation' points at the
    #  last used value for _all_ wrappers.)
    def casting_wrapper( self, other ):
        if self.__class__ is other.__class__:
            return operation( self, other )
        
        try:
            return operation( self, self.__class__( other ) )
        except TypeError:
            return NotImplemented
    
    # Rename the code object to reflect the wrapped operation's name.
    # Otherwise, the profile lists all wrapped operations
    # as 'wrapped_operation'.

    rename_function(