Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7530981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:03:42+00:00 2026-05-30T05:03:42+00:00

Our code base has a few decorators that are used extensively. When I create

  • 0

Our code base has a few decorators that are used extensively.

When I create a runtime profile, a large part of the call graph looks like an hour glass; many functions call one function (the decorator), which then calls many functions. This is a less-useful profile than I’d like.

Is there any way to rectify this situation? Removing the decorator is not an option; it provides necessary functionality.

We’ve considered manually stripping the decorator from the cProfile data after the fact, but it doesn’t seem possible, because the data is summarized into caller->callee relationships, which destroys the caller->decorator->callee relationship.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:03:44+00:00Added an answer on May 30, 2026 at 5:03 am

    Using something like the new library (or types in Python 2.6+), you could theoretically dynamically create a code object and then a function object based on that code object that had a built-in name that varied along with the function you were wrapping.

    That would allow you to manipulate things as deep as <func>.__code__.co_name (which is normally read-only).


    import functools
    import types
    
    def metadec(func):
    
        @functools.wraps(func)
        def wrapper(*args, **kwargs):   
            # do stuff
            return func(*args, **kwargs)
    
        c = wrapper.func_code
        fname = "%s__%s" % (func.__name__, wrapper.__name__)
    
        code = types.CodeType(
                    c.co_argcount, 
                    c.co_nlocals,
                    c.co_stacksize,
                    c.co_flags,  
                    c.co_code,        
                    c.co_consts,         
                    c.co_names,
                    c.co_varnames,
                    c.co_filename,
                    fname, # change the name
                    c.co_firstlineno,
                    c.co_lnotab,
                    c.co_freevars,
                    c.co_cellvars,
                )
    
        return types.FunctionType(
                code, # Use our updated code object
                wrapper.func_globals,
                fname, # Use the updated name
                wrapper.func_defaults,
                wrapper.func_closure,
            )
    

    (functools.wraps is still used here in order to allow for pass-through of things like docstrings, module names, etc.)


    In [1]: from metadec import metadec
    
    In [2]: @metadec
       ...: def foobar(x):
       ...:     print(x)
       ...:     
       ...:     
    
    In [3]: foobar.__name__
    Out[3]: 'foobar__wrapper'
    
    In [4]: foobar(1)
    1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a code base that has many modules nested 4 or
I am currently trying to fix a few weaknesses in our code base by
so, we have a few folders in our source base that are htdigest protected.
Several methods in our code base use a 'MaybeObject' that can be passed into
I would like to reuse some existing code in our code base that accepts
Since a BSTR is only a typedef for wchar_t* our code base has several
At work we have a very large code-base that we commonly export for a
I just saw some code in our code base (and it's OLD code, as
We have recently started using FxCop on our code base and I am in
I have introduced boost to our code base, on my machine I created a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.