inspect.stack() or inspect.currentframe() gives "list index out ofrange error"

Discussion in 'Python' started by deluxstar, Sep 24, 2010.

  1. deluxstar

    deluxstar Guest

    Hi,

    We have an application working on several servers generally written
    with Python 2.6 and Twisted 10.
    The source codes are located in one server and compiled in this
    server. The compiled files are copied to other server via network and
    application server works with these compiled files.

    In this application, I have to write a function to return a value of
    the caller object that calles this function. The function may be
    called from several modules of the application. To achieve this goal,
    I try to use the python inspect module:

    curframe = inspect.currentframe()
    calframe = inspect.getouterframes(curframe, 2)
    calframe[1][0].f_locals['variable']

    This sample code works on local development environment both on linux
    and windows. When checked in to production environment,
    inspect.currentframe() or inspect.stack() function gives "List index
    out of range error".

    When I googled, I found only one clue of copying pyc files:
    http://forum.webfaction.com/viewtopic.php?pid=16808

    Why inspect modules gives this error? OR Is there another way to get
    the caller objects variable value?

    Thanks in advance.
     
    deluxstar, Sep 24, 2010
    #1
    1. Advertising

  2. deluxstar

    Peter Otten Guest

    Re: inspect.stack() or inspect.currentframe() gives "list index out of range error"

    deluxstar wrote:

    > We have an application working on several servers generally written
    > with Python 2.6 and Twisted 10.
    > The source codes are located in one server and compiled in this
    > server. The compiled files are copied to other server via network and
    > application server works with these compiled files.
    >
    > In this application, I have to write a function to return a value of
    > the caller object that calles this function. The function may be
    > called from several modules of the application. To achieve this goal,
    > I try to use the python inspect module:
    >
    > curframe = inspect.currentframe()
    > calframe = inspect.getouterframes(curframe, 2)
    > calframe[1][0].f_locals['variable']
    >
    > This sample code works on local development environment both on linux
    > and windows. When checked in to production environment,
    > inspect.currentframe() or inspect.stack() function gives "List index
    > out of range error".
    >
    > When I googled, I found only one clue of copying pyc files:
    > http://forum.webfaction.com/viewtopic.php?pid=16808
    >
    > Why inspect modules gives this error? OR Is there another way to get
    > the caller objects variable value?


    Can you provide the actual traceback? Provide a small script that reproduces
    the error? Add print statements and post their output?

    print inspect.currentframe
    > curframe = inspect.currentframe()

    print curframe
    > calframe = inspect.getouterframes(curframe, 2)

    print calframe

    Peter
     
    Peter Otten, Sep 24, 2010
    #2
    1. Advertising

  3. deluxstar

    deluxstar Guest

    Re: inspect.stack() or inspect.currentframe() gives "list index outof range error"

    On 24 Eylül, 12:39, Peter Otten <> wrote:
    > deluxstar wrote:
    > > We have an application working on several servers generally written
    > > with Python 2.6 and Twisted 10.
    > > The source codes are located in one server and compiled in this
    > > server. The compiled files are copied to other server via network and
    > > application server works with these compiled files.

    >
    > > In this application, I have to write a function to return a value of
    > > the caller object that calles this function. The function may be
    > > called from several modules of the application. To achieve this goal,
    > > I try to use the python inspect module:

    >
    > > curframe = inspect.currentframe()
    > > calframe = inspect.getouterframes(curframe, 2)
    > > calframe[1][0].f_locals['variable']

    >
    > > This sample code works on local development environment both on linux
    > > and windows. When checked in to production environment,
    > > inspect.currentframe() or inspect.stack() function gives "List index
    > > out of range error".

    >
    > > When I googled, I found only one clue of copying pyc files:
    > >http://forum.webfaction.com/viewtopic.php?pid=16808

    >
    > > Why inspect modules gives this error? OR Is there another way to get
    > > the caller objects variable value?

    >
    > Can you provide the actual traceback? Provide a small script that reproduces
    > the error? Add print statements and post their output?
    >
    > print inspect.currentframe> curframe = inspect.currentframe()
    > print curframe
    > > calframe = inspect.getouterframes(curframe, 2)

    >
    > print calframe
    >
    > Peter


    The traceback is:
    2010-09-25 10:50:38+0300 [-] Traceback (most recent call last):
    2010-09-25 10:50:38+0300 [-] File "../appsrv/lqcommon.py", line 983,
    in getPRMS
    2010-09-25 10:50:38+0300 [-] File "/usr/lib/python2.6/inspect.py",
    line 931, in getouterframes
    2010-09-25 10:50:38+0300 [-] framelist.append((frame,) +
    getframeinfo(frame, context))
    2010-09-25 10:50:38+0300 [-] File "/usr/lib/python2.6/inspect.py",
    line 906, in getframeinfo
    2010-09-25 10:50:38+0300 [-] lines, lnum = findsource(frame)
    2010-09-25 10:50:38+0300 [-] File "/usr/lib/python2.6/inspect.py",
    line 568, in findsource
    2010-09-25 10:50:38+0300 [-] if pat.match(lines[lnum]): break
    2010-09-25 10:50:38+0300 [-] IndexError: list index out of range

    It is hard to reproduce the error with a script. I will work and send
    if I success.
    If the traceback tells smth, please tell me :)
    Thnx
     
    deluxstar, Sep 25, 2010
    #3
  4. Re: inspect.stack() or inspect.currentframe() gives "list index outof range error"

    On Sat, 25 Sep 2010 00:53:13 -0700, deluxstar wrote:

    > The traceback is:
    > 2010-09-25 10:50:38+0300 [-] Traceback (most recent call last):
    > 2010-09-25 10:50:38+0300 [-] File "../appsrv/lqcommon.py", line 983,
    > in getPRMS
    > 2010-09-25 10:50:38+0300 [-] File "/usr/lib/python2.6/inspect.py",
    > line 931, in getouterframes
    > 2010-09-25 10:50:38+0300 [-] framelist.append((frame,) +
    > getframeinfo(frame, context))
    > 2010-09-25 10:50:38+0300 [-] File "/usr/lib/python2.6/inspect.py",
    > line 906, in getframeinfo
    > 2010-09-25 10:50:38+0300 [-] lines, lnum = findsource(frame)
    > 2010-09-25 10:50:38+0300 [-] File "/usr/lib/python2.6/inspect.py",
    > line 568, in findsource
    > 2010-09-25 10:50:38+0300 [-] if pat.match(lines[lnum]): break
    > 2010-09-25 10:50:38+0300 [-] IndexError: list index out of range


    I'm going to take a wild guess here.

    My guess is that you've copied the .pyc file onto the server, BUT there
    is also an *older* version of the .py file there as well. Because the
    modification date is older than that of the .pyc file, Python executes
    the compiled code from the .pyc file. But when you search for the source
    code, the old .py file is discovered -- but it doesn't have the right
    number of lines, and so you end up with an IndexError.



    --
    Steven
     
    Steven D'Aprano, Sep 25, 2010
    #4
  5. Re: inspect.stack() or inspect.currentframe() gives "list index outof range error"

    On Samstag 25 September 2010, Steven D'Aprano wrote:
    > My guess is that you've copied the .pyc file onto the server,
    > BUT there is also an older version of the .py file there as
    > well. Because the modification date is older than that of the
    > .pyc file, Python executes the compiled code from the .pyc
    > file.


    that would be horrible - this is what our own legacy software
    does. A maintenance nightmare. Think adjusting system time
    or "cp -a spam.py"

    Actually the docs say something different:

    The modification time of the version of spam.py used to create
    spam.pyc is recorded in spam.pyc, and the .pyc file is ignored if
    these don’t match.

    found here:
    http://docs.python.org/tutorial/modules.html

    --
    Wolfgang
     
    Wolfgang Rohdewald, Sep 25, 2010
    #5
  6. deluxstar

    Terry Reedy Guest

    Re: inspect.stack() or inspect.currentframe() gives "list index outof range error"

    On 9/25/2010 3:53 AM, deluxstar wrote:

    > The traceback is:
    > 2010-09-25 10:50:38+0300 [-] Traceback (most recent call last):
    > 2010-09-25 10:50:38+0300 [-] File "../appsrv/lqcommon.py", line 983,
    > in getPRMS
    > 2010-09-25 10:50:38+0300 [-] File "/usr/lib/python2.6/inspect.py",
    > line 931, in getouterframes
    > 2010-09-25 10:50:38+0300 [-] framelist.append((frame,) +
    > getframeinfo(frame, context))
    > 2010-09-25 10:50:38+0300 [-] File "/usr/lib/python2.6/inspect.py",
    > line 906, in getframeinfo
    > 2010-09-25 10:50:38+0300 [-] lines, lnum = findsource(frame)
    > 2010-09-25 10:50:38+0300 [-] File "/usr/lib/python2.6/inspect.py",
    > line 568, in findsource
    > 2010-09-25 10:50:38+0300 [-] if pat.match(lines[lnum]): break
    > 2010-09-25 10:50:38+0300 [-] IndexError: list index out of range
    >
    > It is hard to reproduce the error with a script. I will work and send
    > if I success.
    > If the traceback tells smth, please tell me :)


    The traceback is terribly difficult to read with the timestamps added.
    They are not from Python. In the future, please try to suppress them or
    use a global search/replace (with nothing) before posting such.

    --
    Terry Jan Reedy
     
    Terry Reedy, Sep 25, 2010
    #6
    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. Mr m?ll
    Replies:
    2
    Views:
    1,428
    Mr m?ll
    Oct 16, 2004
  2. Replies:
    2
    Views:
    297
  3. John Kotuby
    Replies:
    2
    Views:
    1,105
    George Ter-Saakov
    Mar 7, 2007
  4. Surinder Singh
    Replies:
    1
    Views:
    1,256
    Richard Bos
    Dec 20, 2007
  5. Félix-Antoine Fortin

    inspect.stack() and frame

    Félix-Antoine Fortin, Mar 11, 2010, in forum: Python
    Replies:
    5
    Views:
    1,195
Loading...

Share This Page