From 2b4359de42aa49869948713a18992101dafb5c57 Mon Sep 17 00:00:00 2001 From: pradal Date: Wed, 23 Oct 2024 08:47:59 +1300 Subject: [PATCH 1/2] Fix issue 243: strings with simple quote are always replaced by double quotes --- src/pycropml/transpiler/codeGenerator.py | 41 +++++++++++++----------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/pycropml/transpiler/codeGenerator.py b/src/pycropml/transpiler/codeGenerator.py index d651f63e..1fca6b9a 100644 --- a/src/pycropml/transpiler/codeGenerator.py +++ b/src/pycropml/transpiler/codeGenerator.py @@ -142,27 +142,32 @@ def emit_string(self, node, prefix=u''): def safe_double(self, node): - if sys.version_info.major<3: - if '"' in node.value: - if "'" in node.value: - s = '"%s"' % node.value.replace('"', '\\"') - else: - s = "'%s'" % node.value - else: - s = '"%s"' % node.value + # Decode the byte string to a normal string if it's in byte form + value = node.value.decode() if isinstance(node.value, bytes) else node.value + + # Escape any existing double quotes in the value + escaped_value = value.replace('"', '') + escaped_value = escaped_value.replace("'", "") + + # Wrap the result in double quotes and return + self.write(f'"{escaped_value}"') + + + + def safe_double2(self, node): + value = node.value + if isinstance(value, bytes): + value = value.decode() + + if '"' in value: + s = '%s' % value.replace('"', '') + elif "'" in value: + s = '%s' % value.replace("'", '') else: - if b'"' in node.value: - if b"'" in node.value: - s = '"%s"' % node.value.replace('"', '\\"') - else: - #s = "'%s'" % node.value - s = node.value.decode() - else: - #s = '"%s"' % node.value - s = '"%s"' % node.value.decode() + s = '%s' % value self.write(s) - + def visit_simpleCall(self, node): self.visit(node.value) self.write(" %s "%node.op) From 90cb52c014638765b948c89679981078f9ebb89d Mon Sep 17 00:00:00 2001 From: pradal Date: Wed, 23 Oct 2024 08:48:44 +1300 Subject: [PATCH 2/2] Remove old function --- src/pycropml/transpiler/codeGenerator.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/pycropml/transpiler/codeGenerator.py b/src/pycropml/transpiler/codeGenerator.py index 1fca6b9a..a36a1784 100644 --- a/src/pycropml/transpiler/codeGenerator.py +++ b/src/pycropml/transpiler/codeGenerator.py @@ -153,21 +153,6 @@ def safe_double(self, node): self.write(f'"{escaped_value}"') - - def safe_double2(self, node): - value = node.value - if isinstance(value, bytes): - value = value.decode() - - if '"' in value: - s = '%s' % value.replace('"', '') - elif "'" in value: - s = '%s' % value.replace("'", '') - else: - s = '%s' % value - - self.write(s) - def visit_simpleCall(self, node): self.visit(node.value) self.write(" %s "%node.op)