473,396 Members | 1,970 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,396 software developers and data experts.

inspect: get the calling command

Dear all,

I have a problem to get the command that has called a function if the
command was given on multiple lines. E.g.:

################################################## #
import inspect

def call(a,b,c,d,e):
s = inspector()
return s

def inspector():
frame = inspect.currentframe()
outerframes = inspect.getouterframes(frame)
cmd = ''
for c in outerframes[2][4]:
cmd += c
return cmd

if __name__ == '__main__':
s1 = call (1,2,3,4,5)
s2 = call (1,\
2,\
3,\
4,\
5)
print '-'*10
print s1
print '-'*10
print s2
print '-'*10
################################################## #

This gives the output:

----------
s1 = call (1,2,3,4,5)

----------
5)

----------

I'm quite new to python and the standard libs. So forgive me, if this is
a stupid question.

Any help is very much appreciated.

Best regards
Hans Georg
Jul 18 '05 #1
4 1815
It's an old bug. See Python bug [607092] at Sourceforge.

Inyeol

Jul 18 '05 #2
Inyeol Lee wrote:
It's an old bug. See Python bug [607092] at Sourceforge.

Inyeol

Thanks for pointing me there. Resolution for this bug is 'Wont Fix'. So,
did you have any idea how to work around that 'feature'. What I wanted
to have is some kind of autosave functionality. I have a class doing
some measurements which might take some days of time. At certain points,
I want to pickle the class instance (this is working) to a file. If the
measurement fails, e.g. due to a power main failure, I can recover from
the pickle file. Now, I want also to save the command that issued the
measurement in order to recover automatically with the right command.

BTW, are you sure that my problem is really due to that bug. I'm asking
because one comment stated: '...You should also check out current CVS,
seeing as there are no SET_LINENO instructions any more...'

Hans Georg
Jul 18 '05 #3
I got a working solution by myself:

import inspect

def call(a,b,c,d,e):
s = inspector()
return s

def inspector():
frame = inspect.currentframe()
outerframes = inspect.getouterframes(frame)
ccframe = outerframes[2][0]
ccmodule = inspect.getmodule(ccframe)
try:
slines, start = inspect.getsourcelines(ccmodule)
except IOError:
clen = 10
else:
clen = len(slines)
finfo = inspect.getframeinfo(ccframe, clen)
theindex = finfo[4]
lines = finfo[3]
theline = lines[theindex]
cmd = theline
for i in range(theindex-1, 0, -1):
line = lines[i]
try:
compile (cmd.lstrip(), '<string>', 'exec')
except SyntaxError:
cmd = line + cmd
else:
break
return cmd

if __name__ == '__main__':
s1 = call (1,2,3,4,5)
s2 = call (1,\
2,\
3,\
4,\
5)
print '-'*10
print s1
print '-'*10
print s2
print '-'*10

Hans Georg Krauthaeuser wrote:
Dear all,

I have a problem to get the command that has called a function if the
command was given on multiple lines. E.g.:

################################################## #
import inspect

def call(a,b,c,d,e):
s = inspector()
return s

def inspector():
frame = inspect.currentframe()
outerframes = inspect.getouterframes(frame)
cmd = ''
for c in outerframes[2][4]:
cmd += c
return cmd

if __name__ == '__main__':
s1 = call (1,2,3,4,5)
s2 = call (1,\
2,\
3,\
4,\
5)
print '-'*10
print s1
print '-'*10
print s2
print '-'*10
################################################## #

This gives the output:

----------
s1 = call (1,2,3,4,5)

----------
5)

----------

I'm quite new to python and the standard libs. So forgive me, if this is
a stupid question.

Any help is very much appreciated.

Best regards
Hans Georg

Jul 18 '05 #4
On Tue, Jun 15, 2004 at 09:36:34AM +0200, Hans Georg Krauthaeuser wrote:
....
Thanks for pointing me there. Resolution for this bug is 'Wont Fix'. So,
did you have any idea how to work around that 'feature'.
This is what I was doing. There might be better way.

1. get caller's source code using inspect.getsourcelines(traceback).
(Above function has a bug dealing with module-level traceback in
2.3.3. I'm not sure if it's fixed now.)
2. Tokenize the source up to traceback.tb_lineno.
3. Scan for last logical line break (token.NR ?).
BTW, are you sure that my problem is really due to that bug. I'm asking
because one comment stated: '...You should also check out current CVS,
seeing as there are no SET_LINENO instructions any more...'


Python 2.3.3 has the same bug (feature?). I didn't try 2.3.4.
2.3 doesn't use SET_LINENO bytecode, but uses traceback.tb_lineno instead.
It shows the same behavior.

Inyeol

Jul 18 '05 #5

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

Similar topics

2
by: Fernando Perez | last post by:
Hi all, IPython has suffered quite a few problems with the inspect module in python 2.3. For these, unfortunately all I've been able to do is guard with overreaching except clauses, as I had...
11
by: It's me | last post by:
I discovered the hardway what inspect.isclass() is doing. Consider this no brainer code: ### import inspect class Abc: def Hello(self): return
1
by: Darran Edmundson | last post by:
I was playing around with the inspect module the other day trying to write a quick and dirty "smart" logger. By this I mean that writing a message to the global log would also yield some info...
0
by: Ron Adam | last post by:
While playing around with the inspect module I found that the Blockfinder doesn't recognize single line function definitions. Adding the following two lines to it fixes it, but I'm not sure if it...
4
by: Wal Turner | last post by:
Consider the following simple class: public class ClassA { public static string VAR = "hello"; } public void MethodA() { while(true)
4
by: aramsey | last post by:
I have a javascript program that works fine under Firefox and on IE when running XP, but is having a problem with IE running under Windows 2000 Pro. On my personal XP development machine I have...
1
by: aj | last post by:
What is the difference (if any) between inspect check database and db2dart ??? Do they both find the same potential problems? Does one provide more comprehensive checking than the other? ...
8
by: Aaron \Castironpi\ Brady | last post by:
Hello, The 'inspect' module has this method: inspect.getargvalues(frame) It takes a frame and returns the parameters used to call it, including the locals as defined in the frame, as shown....
0
by: rajasankar | last post by:
Hi, I am using Jython based application and trying to use inspect.py in the python files. Here is my code import inspect,os,sys,pprint,imp def handle_stackframe_without_leak(getframe): ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.