classification
Title: There is functionality bug in linecache library.
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Yang Xiao, emilyemorehouse, gvanrossum, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-06-26 11:30 by Yang Xiao, last changed 2017-06-27 16:38 by gvanrossum. This issue is now closed.

Messages (5)
msg296874 - (view) Author: Yang Xiao (Yang Xiao) Date: 2017-06-26 11:30
There is a functionality bug in linecache library.

>>test.py<<
import linecache

def test_getline(f):
    print linecache.getlines(f)


if __name__ == "__main__":
    tf1 = 'aaa'
    with open(tf1,'w') as f:
        f.write('good morning\n')
    test_getline(tf1)

    tf2 = 'bbb'
    with open(tf2,'w') as f:
        f.write('good evening\n')
    test_getline(tf2)

    tf1 = 'aaa'
    with open(tf1,'w') as f:
        f.write('good morning 123\n')
    test_getline(tf1)

    tf2 = 'bbb'
    with open(tf2,'w') as f:
        f.write('good evening 123\n')
    test_getline(tf2)

The expectant output shoule be:
['good morning\n']
['good evening\n']
['good morning\n']
['good evening\n']

However, the script above outputs below:
['good morning\n']
['good evening\n']
['good morning\n']
['good evening\n']

I think there is a bug about implementation of linecache library.
msg296875 - (view) Author: Yang Xiao (Yang Xiao) Date: 2017-06-26 11:35
Sorry, there is a mistake in msg296874.

The expectant output shoule be:
['good morning\n']
['good evening\n']
['good morning 123\n']
['good evening 123\n']
msg296988 - (view) Author: Emily Morehouse (emilyemorehouse) * (Python committer) Date: 2017-06-27 02:35
This is the expected functionality of linecache. As each file is read, it is stored in a cache, subsequent calls using linecache do not check to see if the file has changed.

If you know that the file has changed, you should call linecache.checkcache() to check all files or linecache.checkcache(filename) to check a specific file. (https://docs.python.org/2/library/linecache.html#linecache.checkcache)


[Since no one is listed in the Experts Index, I've included some other contributors to linecache who can assist in closing out this issue]
msg296998 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2017-06-27 04:54
You nailed it, Emily! This is not a bug (though the docs could be a bit more upfront about this -- just having "cache" in the name doesn't cut it these days :-).

If either Serhiy or Raymond agrees they can close the issue (we won't wait for the third vote). If we don't hear from them within a reasonable time we can also close the issue.
msg297006 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-06-27 05:26
I concur with Emily and Guido. This is not a bug. Maybe this should be documented more explicitly if "cache" in the name is not enough, I don't know. I have no any special relation to the linecache module, maybe just once fixed a bug or two.
History
Date User Action Args
2017-06-27 16:38:14gvanrossumsetstatus: open -> closed
resolution: not a bug
stage: resolved
2017-06-27 05:26:05serhiy.storchakasetmessages: + msg297006
2017-06-27 04:54:47gvanrossumsetmessages: + msg296998
2017-06-27 02:35:59emilyemorehousesetnosy: + gvanrossum, rhettinger, serhiy.storchaka, emilyemorehouse
messages: + msg296988
components: + Library (Lib), - 2to3 (2.x to 3.x conversion tool)
2017-06-26 11:35:18Yang Xiaosetmessages: + msg296875
2017-06-26 11:30:08Yang Xiaocreate