Get caller method name from a stack

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
VladVons
Posts: 43
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Get caller method name from a stack

Post by VladVons » Fri Feb 21, 2020 8:23 pm

within a function i want to know who called me
another words i want to trace stack

in python for PC there is a module 'inspect' and method 'stack()'
From there I can get a stack information:
File name
Caller method name
Method's line number

Code: Select all

def MethodA():
    # some code to get a caller name
    # in my case it should be 'Main'

def Main():
    MethodA()

User avatar
jimmo
Posts: 1381
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Get caller method name from a stack

Post by jimmo » Sat Feb 22, 2020 12:02 am

This _might_ be possible by raising and catching an exception... but off the top of my head (on my phone atm) I can't remember if (or how) you can pull the traceback out of an exception object in MicroPython.

User avatar
jimmo
Posts: 1381
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Get caller method name from a stack

Post by jimmo » Mon Feb 24, 2020 12:56 am

I don't think this is possible. You can get the text of an exception (using sys.print_exception to a StringIO), but an exception traceback won't have the caller in it (as the traceback starts from the `try`). You could use this approach to find out the current method's name, but that doesn't seem very useful.

(Would love to be proven wrong though)

stijn
Posts: 426
Joined: Thu Apr 24, 2014 9:13 am

Re: Get caller method name from a stack

Post by stijn » Mon Feb 24, 2020 8:46 am

within a function i want to know who called me
another words i want to trace stack
It would also be interesting if you post what exactly you actually want to achieve, just to avoid classic A/B problem situations.

VladVons
Posts: 43
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: Get caller method name from a stack

Post by VladVons » Mon Feb 24, 2020 12:56 pm

I use this trick for logging both message + caller

Code: Select all

def Log(aMsg, aCallerName = 'unknown'):
  # to minimize code size it would be good to get a aCallerName from a stack here
  print(aMsg, aCallerName )
  
def Func1():
  Log('Message1', 'Func1()')

def Func2():
  Log('Message2')
Last edited by VladVons on Tue Feb 25, 2020 5:23 am, edited 1 time in total.

stijn
Posts: 426
Joined: Thu Apr 24, 2014 9:13 am

Re: Get caller method name from a stack

Post by stijn » Mon Feb 24, 2020 1:56 pm

Yeah if you need that there's not many options except for calling Log() in callers as well.

VladVons
Posts: 43
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: Get caller method name from a stack

Post by VladVons » Mon Feb 24, 2020 5:03 pm

Two reasons:
1) Main is less code size
It gives more free usefull RAM for tiny esp8266 (32K free pure memory only !)
2) Less WrongNamedCallers in a bullshits code

By the way how to free memory from unused imports, classes, functions?
Relative post was here, but no sucsess with it.
viewtopic.php?f=2&t=4893&p=28115

kevinkk525
Posts: 638
Joined: Sat Feb 03, 2018 7:02 pm

Re: Get caller method name from a stack

Post by kevinkk525 » Mon Feb 24, 2020 5:40 pm

Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply