lglaf icon indicating copy to clipboard operation
lglaf copied to clipboard

Windows 10 file read failed

Open IndustrialMuffin opened this issue 9 years ago • 7 comments
trafficstars

Running on Windows 10, I had trouble with file operations. Made a few changes and it worked:

import os
class FileCommunication(Communication):
    def __init__(self, file_path):
        super(FileCommunication, self).__init__()
        if sys.version_info[0] >= 3:
            self.f = os.open(file_path, os.O_RDWR | os.O_BINARY)
        else:
            self.f = os.open(file_path, os.O_RDWR | os.O_BINARY)
    def _read(self, n, timeout=None):
        try:
            return os.read(self.f,n)
        except IOError as e:
            _logger.warning("failed to read")
            raise e
    def write(self, data):
        os.write(self.f,data)
    def close(self):
        os.close(self.f)

IndustrialMuffin avatar Apr 11 '16 19:04 IndustrialMuffin

The Python 2 vs 3 case can be folded:

self.f = os.open(file_path, os.O_RDWR | os.O_BINARY)

Why do you catch the IOError for _read? The caller should take care of IO errors and do logging as needed.

Can you also post any exceptions/errors that prompted for these patches? Thanks :-) (A PR would also be nice)

Lekensteyn avatar Apr 14 '16 15:04 Lekensteyn

I guess there was no good reason for trapping IOError for _read.

I thought about folding Python 2 vs 3 case, but thought I'd leave the cases split up like they were before, just in case they'd need to be handled differently since I only tried it on Python 2.

The errors I got were:

Traceback (most recent call last):
  File "lglaf.py", line 404, in <module>
    main()
  File "lglaf.py", line 386, in main
    try_hello(comm)
  File "lglaf.py", line 290, in try_hello
    data = comm.read(0x20, timeout=HELLO_READ_TIMEOUT)
  File "lglaf.py", line 148, in read
    buff = self._read(need, timeout=timeout)
  File "lglaf.py", line 189, in _read
    return self.f.read(n)
IOError: [Errno 0] Error

IndustrialMuffin avatar Apr 15 '16 16:04 IndustrialMuffin

Isn't os.O_RDONLY | os.O_BINARY sufficient?

Lekensteyn avatar Nov 06 '16 11:11 Lekensteyn

I doubt it would work with os.O_RDONLY | os.O_BINARY since writing is required.

IndustrialMuffin avatar Nov 06 '16 14:11 IndustrialMuffin

Oh, nevermind, thinko (r+ = O_RDWR).

Since you mentioned Python 2, I think that buffering could be a problem. From pydoc open:

The optional buffering argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size (in bytes). A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used. [2]

What if you just remove the if sys.version_info[0] >= 3: and else: ... lines completely, enabling the buffering parameter by default?

(Note to self: when adding such checks, add a comment why you exactly do it. I vaguely remember that buffering=0 gave some error on Windows, but do not know for sure.)

Lekensteyn avatar Nov 06 '16 15:11 Lekensteyn

I think I tried different buffering options without success. When I get a chance I'll check it out. It probably was a python bug, and by now maybe the bug is fixed.

IndustrialMuffin avatar Nov 07 '16 10:11 IndustrialMuffin

Is this not the same as this ? https://github.com/steadfasterX/lglaf/pull/9

kenkit avatar Mar 01 '19 13:03 kenkit