How can I get the line number ?

Discussion in 'Python' started by kk, Jul 24, 2009.

  1. kk

    kk Guest

    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
    kk, Jul 24, 2009
    #1
    1. Advertising

  2. On Fri, Jul 24, 2009 at 2:51 PM, kk<> 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
    Maxim Khitrov, Jul 24, 2009
    #2
    1. Advertising

  3. kk

    kk Guest

    Maxim,

    Thank you so much. I will try right now.
    kk, Jul 24, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Gibson
    Replies:
    1
    Views:
    689
    Eric Gibson
    Aug 31, 2004
  2. Hugo
    Replies:
    10
    Views:
    1,299
    Matt Humphrey
    Oct 18, 2004
  3. Marek Prerovsky
    Replies:
    1
    Views:
    579
    python
    Feb 16, 2005
  4. kaushikshome
    Replies:
    4
    Views:
    759
    kaushikshome
    Sep 10, 2006
  5. Jekyll
    Replies:
    2
    Views:
    422
    Victor Bazarov
    Apr 24, 2007
Loading...

Share This Page