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.
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]
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.
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
2022-04-11 14:58:48
admin
set
github: 74948
2017-06-27 16:38:14
gvanrossum
set
status: open -> closedresolution: not a bugstage: resolved