__LINE__ and __FILE__ functionality in Python?

J

Joakim Hove

Hello,

i have simple[1] function like this:

def log_msg(msg , file , line):
print "%s:%s %s" % (file,line,msg)

the file and line arguments should be the filename and linenumber of
the source file where the function is called. If this were C I would
have used the __FILE__ and __LINE__ macros as:

log_msg(msg , __FILE__ , __LINE__)

Is there a way to emulate this behaviour in Python?


Best Regards

Joakim Hove



[1]: It is not *that* simple, but you get the point.


--
Joakim Hove
hove AT ntnu.no /
Tlf: +47 (73 5)9 34 27 / Stabburveien 18
Fax: ................. / N-5231 Paradis
http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04
 
M

Maric Michaud

Le dimanche 13 août 2006 13:31, Joakim Hove a écrit :
Hello,

i have simple[1] function like this:

def log_msg(msg , file , line):
print "%s:%s %s" % (file,line,msg)

the file and line arguments should be the filename and linenumber of
the source file where the function is called. If this were C I would
have used the __FILE__ and __LINE__ macros as:

log_msg(msg , __FILE__ , __LINE__)

Is there a way to emulate this behaviour in Python?

Sure, try :

In [46]: import inspect

In [47]: c=inspect.currentframe()

In [48]: c.f_lineno
Out[48]: 1

In [49]: c.f_code.co_filename
Out[49]: '<ipython console>'

Of course, in the console, these are not truly relevant.


--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 
J

John Machin

Joakim said:
Hello,

i have simple[1] function like this:

def log_msg(msg , file , line):
print "%s:%s %s" % (file,line,msg)

the file and line arguments should be the filename and linenumber of
the source file where the function is called. If this were C I would
have used the __FILE__ and __LINE__ macros as:

log_msg(msg , __FILE__ , __LINE__)

Is there a way to emulate this behaviour in Python?

It's better in Python not to emulate that but to let the caller do the
work:

C:\junk>type caller_id.py
import inspect

def logger(msg):
print "logger:", msg
print "called from %s:%d" % inspect.stack()[1][1:3]
print

def client1():
logger("one")

def client2():
logger("two")

client1()
client2()


C:\junk>caller_id.py
logger: one
called from C:\junk\caller_id.py:9

logger: two
called from C:\junk\caller_id.py:12

If you care to search for __LINE__ in this newsgroup, there's a slight
chance you might find a thread or two or twenty-two on the topic :)

I don't usually go for one-liners, especially ugly ones like
"inspect.stack()[1][1:3]" but it avoids the risk of hanging on to a
reference to the frame object -- see the warning in the inspect docs.

You can get the name of the calling function or method (and,
indirectly, the method's class) if you want to log the whole dossier --
details left as an exercise :)

HTH,
John
 
J

Joakim Hove

Sure, try :

In [46]: import inspect

In [47]: c=inspect.currentframe()

In [48]: c.f_lineno
Out[48]: 1

In [49]: c.f_code.co_filename
Out[49]: '<ipython console>'

Thanks a lot - that was just what I wanted.


Regards - Joakim


--
Joakim Hove
hove AT ntnu.no /
Tlf: +47 (73 5)9 34 27 / Stabburveien 18
Fax: ................. / N-5231 Paradis
http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04
 
M

Maric Michaud

Le dimanche 13 août 2006 14:18, John Machin a écrit :
I don't usually go for one-liners, especially ugly ones like
"inspect.stack()[1][1:3]" but it avoids the risk of hanging on to a
reference to the frame object -- see the warning in the inspect docs.

Yes, my mistake, thanks for pointing this out, my suggestion should have been
more accurate :

import inspect

try :
c=inspect.currentframe()
log_msg(c.f_lineno, c.f_code.co_filename)
finally :
del c

or shorter ;

from inspect import currentframe
log_msg(currentframe().f_lineno,
currentframe().f_code.co_filename)


--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top