Re: How can I get the line number ?



On Fri, Jul 24, 2009 at 2:51 PM, kk<maymunbeyin@xxxxxxxxx> wrote:
Hello

I am writing some Python code that runs in another application(has
wrapper functions). Due to lack of debugging I am printing out alot of
outputs and manual messages. I want to be able to create a function
that would let me print the current line number that is called from.
This is not for debugging exceptions it is rather to simplify my debug
messages, at least I can trace my debug messages.

thanks

Modify the following as needed:

from inspect import currentframe, getframeinfo

def caller_info(depth=0):
"""
Get file, line number, and name of the calling function.
"""
if depth < 0:
raise ValueError('invalid stack depth')

caller = frame = currentframe()
try:
for i in xrange(-1, depth):
caller = caller.f_back
if caller is None:
return (None, None, None)

return getframeinfo(caller, 0)[:3]
finally:
del caller, frame

- Max
.



Relevant Pages

  • Re: Question about return style
    ... The caller needn't be storing it in a variable either. ... For unmanaged debugging, you can pluck the value from EAX, but I'm not aware of any similar trick that works for managed debugging. ... However, unless your function is returning a primitive type like int you will not be able to inspect, let alone modify the value, because it's an address, and as far as managed code is concerned, there's no such thing as an address to a managed object. ... mixed-mode debugging is slower and less flexible than pure managed debugging so it should preferably be used only when you're actually debugging an application that uses both managed and unmanaged components. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: get name of variable as string?
    ... Again for debugging. ... WriteLine method that will state who called it by going up the call ... stack: caller -> writeline. ... I have wrappers for this WriteLine ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Special Var with Name of current scope or name of subroutine?
    ... Is there a special variable or trick to get the name of the current scope or subroutine name? ... The caller* function will tell you information about the call stack ... When it comes to logging (for debugging purposes) ...
    (perl.beginners)
  • Re: Question about return style
    ... The caller needn't be storing it in a variable either. ... For unmanaged debugging, you can pluck the value from EAX, but I'm not aware of any similar trick that works for managed debugging. ... Admittedly I haven't explored advanced debugging techniques since moving over to managed code, but given that the code that's executing is in fact native x86, I think it's very likely there's a mode in the debugger you can switch to that gives you access to the data at a lower level, including register access. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C function return value?
    ... > "every invocation of a function causes a frame for that function to be ... > start of this frame) and local variables of this function" ... > gets popped before caller tries to access the location but then again the ... > struct object and returns that. ...
    (comp.lang.c)