Home > front end >  Is there an easy way to find what function is calling another function in Python?
Is there an easy way to find what function is calling another function in Python?

Time:11-07

I'm not too well-versed in Python, although, I think this question is mostly language-agnostic, but wanted to see if there was a particular way to do it for Python.

I'm working with a couple of Pytorch Python scripts.

The function test_qtensor_cpu on line 147 in https://github.com/pytorch/pytorch/blob/fbcode/warm/test/quantization/core/test_quantized_tensor.py is being executed when I run the script in https://github.com/pytorch/pytorch/blob/fbcode/warm/test/test_quantization.py, but I cannot find the function that calls test_qtensor_cpu. I did a grep -ri "test_qtensor_cpu*" . in the root director of this repo and the only result was the definition of this function.

Is there a way for this function to be called without explicitly writing out the function's name?

CodePudding user response:

Just add:

def my_func_i_cant_figure_out_whats_calling_it()
    import traceback
    traceback.print_stack()

That will show you the callstack at that point, even without a breakpoint.

Yes, its possible to call a function without explicitly writing it out.

(and figuring out how to use a debugger is super useful... future you will thank you if you figure it out sooner rather than later)

CodePudding user response:

Line 29 of test_quantization.py imports TestQuantizedTensor (which includes the test_qtensor_cpu method).

The run_tests() (source here) at the bottom of the file will automatically run all test cases that have been imported (which includes TestQuantitizedTensor) via unittest (usually via unittest.main, though this can be changed by args passed to the test suite).

  • Related