0

Let's imagine, I've got a module foo.py with declared a function foofunc:

def foofunc():
    # smart things
    return

And two different modules — bar.py and spam.py, where exists code which directly executes the function from foo module.

# `bar` module. All rights reserved.

from foo import foofunc


def run_foofunc():
     return foofunc()

The same thing in another module:

# `spam` module. All rights reserved.

from foo import foofunc


def run_foofunc():
    return foofunc()

I need to know where is executed the function without knowledge of possible places. Something like:

def foofunc():
    print inspect.executedfrom()

Will do something like that in standard output

<pack.bar.run_foofunc>

Does it something similar in real world?

I159
  • 29,741
  • 31
  • 97
  • 132
  • 2
    No matter what the actual answer is, it seems that you're doing it wrong. Why would you want to do that? – Fabian Aug 28 '13 at 13:48
  • I'm working in the huge project is named OpenStack ;) and I'm new in it. I need it for research and debug. – I159 Aug 28 '13 at 13:51
  • possible duplicate of [Get \_\_name\_\_ of calling function's module in Python](http://stackoverflow.com/questions/1095543/get-name-of-calling-functions-module-in-python) – Daan Timmer Aug 28 '13 at 13:57

4 Answers4

2

Running the risk of not answering the actual question, you wrote that you need it for research and debugging.

I think the traceback module is just great for that.

import traceback
traceback.print_stack()

Also take a look at pdb, it allows you to interactively step through your code during runtime.

Fabian
  • 4,160
  • 20
  • 32
2

test.py:

import inspect    

def foofunc():
    frame, filename, line_number, function_name, lines, index = inspect.getouterframes(
        inspect.currentframe())[1]

    print(filename, line_number, function_name)
    return

foo.py:

import test
def run_foofunc():
    test.foofunc()

run_foofunc()

yields

('/tmp/foo.py', 3, 'run_foofunc')
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 1
    I'll upvote you because it is the correct answer, but please don't use this in production code. – Fabian Aug 28 '13 at 13:58
  • 1
    @DaanTimmer In my opinion, if a function behaves differently depending on the caller, it's a sign for bad code layout and should be parameterized. That way you can easily re-use that function elsewhere, unit test it and don't have to touch the function when you move your caller. Furthermore, using the `inspect` module has a speed impact on Python interpreters like PyPy. – Fabian Aug 28 '13 at 14:24
0

Here is a possible implementation of executed_from:

import inspect

def executed_from():
    f = inspect.currentframe().f_back.f_back
    return "%s.%s" % (inspect.getmodule(f).__name__, f.f_code.co_name)
user4815162342
  • 141,790
  • 18
  • 296
  • 355
0

During wait for your answers I found my own answer. This issue possible to resolve by raising an exception in debugged function and make out the traceback layer by layer. This approach is bad in general because it is stops execution, but not bad in the my particular case. And besides it is very pythonic and easy to clean.

I159
  • 29,741
  • 31
  • 97
  • 132
  • 1
    You don't have to raise an exception, use the `traceback` module and save the output to a file for example. – Fabian Aug 28 '13 at 14:42