473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

__LINE__ and __FILE__ functionality in Python?


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
Aug 13 '06 #1
4 9236
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
Aug 13 '06 #2
Joakim Hove wrote:
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

Aug 13 '06 #3

Maric Michaud <ma***@aristote.infowrites:

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
Aug 13 '06 #4
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
Aug 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Spry | last post by:
Hi, I wanted to write macros for finding the number of memory allocations and deallocations also wanted to find the locations. The code I have is a pretty big one. I have a wrapper on top of...
9
by: qazmlp | last post by:
How exactly __FILE__ and __LINE__ macros are defined? Or, Is the definition of these macros implementation dependent ? I am wondering how easily they can get the file name and line number...
18
by: Paul Shipley | last post by:
Hi, Does anyone know a way of converting the __LINE__ macro to a string at compile time? The reason I ask it because I want to put some debug information in to tell me if memory is not being...
5
by: jake1138 | last post by:
I couldn't find an example of this anywhere so I post it in the hope that someone finds it useful. I believe this is compiler specific (I'm using gcc), as C99 defines __VA_ARGS__. Comments are...
5
by: baumann.Pan | last post by:
where are these macros defined? can I use it a release ver code?
3
by: wsq | last post by:
in C++ we can use __FILE__ and __LINE__ to help us locating the bug, is there anything like these in C# ? thanks, wsq
7
by: Kenneth Brody | last post by:
Am I correct that using __FILE__ and __LINE__ within a macro will expand to the filename and line in which the macro is invoked, rather than where it is defined? For example, in a header file: ...
5
by: Neo | last post by:
Hie, Can I put __FILE__ and __LINE__ macros inside the class function which may not be inline. And void function() { classobject::LogInfo(...); } Internally LogInfo should log file name...
9
by: Neo | last post by:
Hi Friends, I am planning to use "__FILE__,__LINE__,__FUNCTION__ " for a logging component in my class. In debug build I gets all information. I tried with release mode also and it works. But I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.