printing line numbers for debugging purpose

  • Thread starter Philippe C. Martin
  • Start date
P

Philippe C. Martin

Hi,

All of the methods from my program return None on error (i.e; I do not
want to assert and have the program exit).

Is it possible to print the current source file name/line number ?

ex: in C/C++ I would use the macros __FILE__ and __LINE__.

Regards,

Philippe



--
***************************
Philippe C. Martin
SnakeCard LLC
www.snakecard.com
***************************
 
M

Michael Fuhr

Philippe C. Martin said:
All of the methods from my program return None on error (i.e; I do not
want to assert and have the program exit).

Is it possible to print the current source file name/line number ?

Someone else posted an example using an exception handler. See
also the traceback module.
 
P

python

Below is a function to get the current line number and file name.

/Jean Brouwers


- # dashes added to preserve indentation.
-
- import traceback
-
- def caller(up=0):
- '''Get file name, line number, function name and
- source text of the caller's caller as 4-tuple:
- (file, line, func, text).
-
- The optional argument 'up' allows retrieval of
- a caller further back up into the call stack.
-
- Note, the source text may be None and function
- name may be '?' in the returned result. In
- Python 2.3+ the file name may be an absolute
- path.
- '''
- try: # just get a few frames
- f = traceback.extract_stack(limit=up+2)
- if f:
- return f[0]
- except:
- if __debug__:
- traceback.print_exc()
- pass
- # running with psyco?
- return ('', 0, '', None)
-
-
- if __name__ == '__main__':
- print caller()
-
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top