Просмотр исходного кода

GCodeProfileReader: make deserialization more robust

CURA-3770
Lipu Fei 7 лет назад
Родитель
Сommit
a3d92d557d
1 измененных файлов с 7 добавлено и 3 удалено
  1. 7 3
      plugins/GCodeProfileReader/GCodeProfileReader.py

+ 7 - 3
plugins/GCodeProfileReader/GCodeProfileReader.py

@@ -56,7 +56,7 @@ class GCodeProfileReader(ProfileReader):
         # TODO: Consider moving settings to the start?
         serialized = ""  # Will be filled with the serialized profile.
         try:
-            with open(file_name) as f:
+            with open(file_name, "r") as f:
                 for line in f:
                     if line.startswith(prefix):
                         # Remove the prefix and the newline from the line and add it to the rest.
@@ -66,9 +66,13 @@ class GCodeProfileReader(ProfileReader):
             return None
 
         serialized = unescapeGcodeComment(serialized)
-        Logger.log("i", "Serialized the following from %s: %s" %(file_name, repr(serialized)))
 
-        json_data = json.loads(serialized)
+        # serialized data can be invalid JSON
+        try:
+            json_data = json.loads(serialized)
+        except Exception as e:
+            Logger.log("e", "Could not parse serialized JSON data from GCode %s, error: %s", file_name, e)
+            return None
 
         profiles = []
         global_profile = readQualityProfileFromString(json_data["global_quality"])