[Piglit] [PATCH] Fix the fnctl import on Windows

Olivier Berthier olivierx.berthier at linux.intel.com
Thu Jun 2 08:56:25 UTC 2016


On Wednesday 01 Jun 2016 à 15:22:12 (-0700), Dylan Baker wrote:
> Quoting Olivier Berthier (2016-05-30 03:21:14)
> [snip]
> > +        if sys.platform.startswith('linux'):
> > +            try:
> > +                with open(self._monitoring_source, 'r') as f:
> > +                    lines = []
> > +                    if self._is_locked:
> > +                        # Create a duplicated file descriptor, this avoid lock
> > +                        fd = os.dup(f.fileno())
> > +                        # use I/O control for reading the lines
> > +                        fcntl.fcntl(fd, fcntl.F_SETFL, os.O_NONBLOCK)
> > +
> > +                        while True:
> > +                            try:
> > +                                line = os.read(fd, 1024)
> > +                                if not line:
> > +                                    break;
> > +                            except OSError as e:
> > +                                if e.errno == errno.EAGAIN:
> > +                                    break
> > +                                else:
> > +                                    raise e
> > +                            lines.append(line.decode("utf-8", "strict"))
> > +                        os.close(fd)
> 
> I'm very confused here. What exactly is a locked file? Since on Linux at
> least you can read a locked file as long as you have the proper
> permissions I can't figure out what locking has to do with this code.
> With the test you provided if I replace the whole of 'lines = []'
> through 'f.close()' with 'lines = f.readlines()' the test passes, so
> either the code here is bogus, the test is bogus, or both.
> 
> I think if we need to rely on POSIX-only modules then we need to guard
> is_locked behind os.name == 'posix' or similar.
> 
> Dylan
> 

I used this methode instead of readlines() because it works with standard files
and with the files like /dev/kmsg. I think it increase the field of view for
monitoring.

However, if there is another way I can change it. Or if reading these files
don't provides any relevant information we can't find in dmesg or in another
log, we can juste change to readlines() and remove fnctl.

Olivier

> > +
> > +                    else:
> > +                        lines = f.read().splitlines()
> > +
> > +                    f.close()
> > +
> > +                    # Find all new entries, do this by slicing the list of
> > +                    # the lines to only returns elements after the last element
> > +                    # stored. If there are not matches a value error is raised,
> > +                    # that means all of the lines are new
> > +                    l = 0
> > +                    for index, item in enumerate(reversed(lines)):
> > +                        if item == self._last_message:
> > +                            # don't include the matched element
> > +                            l = len(lines) - index
> > +                            break
> > +                    self._new_messages = lines[l:]
> > +                    # Attempt to store the last element of lines,
> > +                    # unless there was no line
> > +                    self._last_message = lines[-1] if lines else None
> > +            except Exception:
> > +                # if an error occured, we consider there are no new messages
> > +                self._new_messages = []
> > +                pass




More information about the Piglit mailing list