Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise an error if the file is erased while following #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/tailer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import sys
import time
import os.path

if sys.version_info < (3,):
range = xrange
Expand Down Expand Up @@ -159,6 +160,8 @@ def follow(self, delay=1.0):
trailing = True

while 1:
if not os.path.exists(os.path.realpath(self.file.name)): #Check if the file still exists
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.file may be a stream instead of a file with a name. So self.file.name might result in an attribute error. There are a couple of ways I can think of to handle this:

  1. If getattr(self.file, "name"), then assume it's a file and check for existence. However, this could be an expensive call on a long file since it will cause an extra disk read for every single line.
  2. Do a non-blocking readline and only yield something was read. This might be able to be done with select. I'm not certain how it will react if the file disappears. This will be a little more complex and might involve some more refactoring, but it should result in the best experience.

I'm inclined to choose option 2. Since you opened this pull request 5 years ago, you probably don't care anymore 😅. I'll see about fixing this. Thanks for the submission!

raise IOError
where = self.file.tell()
line = self.file.readline()
if line:
Expand Down