Crash when opening files that are not project files

Issue #297 resolved
Emily Ellis created an issue

When opening (not importing) a file that is not a FlatCAM project file*, FlatCAM crashes with this error:

[DEBUG][MainThread] Opening project: /tmp/not-a-project.asdf
[ERROR][MainThread] Failed to parse project file, trying to see if it loads as an LZMA archive: /tmp/not-a-project.asdf because Expecting value: line 1 column 1 (char 0)
WARNING: Traceback (most recent call last):
  File "/home/mike/code/flatcam/FlatCAMApp.py", line 8120, in open_project
    d = json.load(f, object_hook=dict2obj)
  File "/usr/lib/python3.7/site-packages/simplejson/__init__.py", line 481, in load
    use_decimal=use_decimal, **kw)
  File "/usr/lib/python3.7/site-packages/simplejson/__init__.py", line 555, in loads
    return cls(encoding=encoding, **kw).decode(s)
  File "/usr/lib/python3.7/site-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/lib/python3.7/site-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/mike/code/flatcam/FlatCAMApp.py", line 6432, in on_file_openproject
    self.open_project(filename)
  File "/home/mike/code/flatcam/FlatCAMApp.py", line 8129, in open_project
    file_content = f.read().decode('utf-8')
  File "/usr/lib/python3.7/lzma.py", line 200, in read
    return self._buffer.read(size)
  File "/usr/lib/python3.7/_compression.py", line 99, in read
    raise EOFError("Compressed file ended before the "
EOFError: Compressed file ended before the end-of-stream marker was reached
Aborted (core dumped)

Here’s a patch to make it not do that.

* Why would anyone do this, you ask? I don’t know, I wasn’t paying attention. It’d be nice to attempt an import if opening fails, but since some file types can be imported as multiple object types, I’m not entirely sure how this would work.

Comments (3)

  1. Marius Stanciu

    Thank you Mike,
    You are right, this error should be handled. With PyQt4, when an exception was raised the app still survived but in PyQt5 for every unhandled exception the app will crash. There as still some places where things may go wrong.
    Your approach is OK, but it only handles one more error and if another will crop up, the app will crash again. Therefore I changed it to the more general “except Exception as e” and added the error to the log.debug so I (or the one who develop) will know the “why”. I’m not keen to add more strings as there are 5 languages to translate to for the moment… I will comment on the pull request on how it should be done.

  2. Log in to comment