From a3d92d557dc1b3aa9eb9446e6dd819f3d9cece7c Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Mon, 8 May 2017 11:33:14 +0200 Subject: [PATCH] GCodeProfileReader: make deserialization more robust CURA-3770 --- plugins/GCodeProfileReader/GCodeProfileReader.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/plugins/GCodeProfileReader/GCodeProfileReader.py b/plugins/GCodeProfileReader/GCodeProfileReader.py index abfef6e296..0abbcfc833 100644 --- a/plugins/GCodeProfileReader/GCodeProfileReader.py +++ b/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"])