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

Dylan Baker dylan at pnwbakers.com
Wed Jun 1 22:22:12 UTC 2016


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

> +
> +                    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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: signature
URL: <https://lists.freedesktop.org/archives/piglit/attachments/20160601/fdaad67c/attachment.sig>


More information about the Piglit mailing list