fix string encoding python3
authorGP Orcullo <kinsamanka@gmail.com>
Fri, 28 Oct 2022 14:53:23 +0800
branchpython3
changeset 3755 ca814b175391
parent 3754 98a76dbb1b6d
child 3756 8db203310f68
fix string encoding
BeremizIDE.py
ConfigTreeNode.py
IDEFrame.py
PLCControler.py
ProjectController.py
plcopen/plcopen.py
targets/toolchain_gcc.py
util/ProcessLogger.py
xmlclass/xmlclass.py
--- a/BeremizIDE.py	Fri Oct 28 14:26:17 2022 +0800
+++ b/BeremizIDE.py	Fri Oct 28 14:53:23 2022 +0800
@@ -861,7 +861,7 @@
             return
 
         try:
-            defaultpath = DecodeFileSystemPath(self.Config.Read("lastopenedfolder"))
+            defaultpath = DecodeFileSystemPath(self.Config.Read("lastopenedfolder").encode())
         except Exception:
             defaultpath = os.path.expanduser("~")
 
--- a/ConfigTreeNode.py	Fri Oct 28 14:26:17 2022 +0800
+++ b/ConfigTreeNode.py	Fri Oct 28 14:53:23 2022 +0800
@@ -210,22 +210,22 @@
 
             # generate XML for base XML parameters controller of the confnode
             if self.MandatoryParams:
-                BaseXMLFile = open(self.ConfNodeBaseXmlFilePath(), 'w')
+                BaseXMLFile = open(self.ConfNodeBaseXmlFilePath(), 'w', encoding='utf-8')
                 BaseXMLFile.write(etree.tostring(
                     self.MandatoryParams[1],
                     pretty_print=True,
                     xml_declaration=True,
-                    encoding='utf-8'))
+                    encoding='utf-8').decode())
                 BaseXMLFile.close()
 
             # generate XML for XML parameters controller of the confnode
             if self.CTNParams:
-                XMLFile = open(self.ConfNodeXmlFilePath(), 'w')
+                XMLFile = open(self.ConfNodeXmlFilePath(), 'w', encoding='utf-8')
                 XMLFile.write(etree.tostring(
                     self.CTNParams[1],
                     pretty_print=True,
                     xml_declaration=True,
-                    encoding='utf-8'))
+                    encoding='utf-8').decode())
                 XMLFile.close()
 
             # Call the confnode specific OnCTNSave method
--- a/IDEFrame.py	Fri Oct 28 14:26:17 2022 +0800
+++ b/IDEFrame.py	Fri Oct 28 14:53:23 2022 +0800
@@ -98,16 +98,15 @@
 
 
 def EncodeFileSystemPath(path, use_base64=True):
-    path = path.encode(sys.getfilesystemencoding())
     if use_base64:
-        return base64.encodestring(path)
+        path = base64.b64encode(path.encode()).decode()
     return path
 
 
 def DecodeFileSystemPath(path, is_base64=True):
     if is_base64:
-        path = base64.decodestring(path)
-    return str(path, sys.getfilesystemencoding())
+        path = base64.b64decode(path.encode()).decode()
+    return path
 
 
 def AppendMenu(parent, help, kind, text, id=wx.ID_ANY):
--- a/PLCControler.py	Fri Oct 28 14:26:17 2022 +0800
+++ b/PLCControler.py	Fri Oct 28 14:53:23 2022 +0800
@@ -454,8 +454,8 @@
                 self.NextCompiledProject = self.Copy(self.Project)
                 program_text = "".join([item[0] for item in self.ProgramChunks])
                 if filepath is not None:
-                    programfile = open(filepath, "w")
-                    programfile.write(program_text.encode("utf-8"))
+                    programfile = open(filepath, "w", encoding='utf-8')
+                    programfile.write(program_text)
                     programfile.close()
                     self.ProgramFilePath = filepath
                 return program_text, errors, warnings
--- a/ProjectController.py	Fri Oct 28 14:26:17 2022 +0800
+++ b/ProjectController.py	Fri Oct 28 14:53:23 2022 +0800
@@ -806,8 +806,8 @@
             plc_file.write(POUsIECCodeContent)
 
         hasher = hashlib.md5()
-        hasher.update(IECCodeContent)
-        hasher.update(POUsIECCodeContent)
+        hasher.update(IECCodeContent.encode())
+        hasher.update(POUsIECCodeContent.encode())
         self.IECcodeDigest = hasher.hexdigest()
 
         return True
--- a/plcopen/plcopen.py	Fri Oct 28 14:26:17 2022 +0800
+++ b/plcopen/plcopen.py	Fri Oct 28 14:53:23 2022 +0800
@@ -211,7 +211,7 @@
 PLCOpen_v1_xml = PLCOpen_v1_xml.replace(
     "http://www.plcopen.org/xml/tc6.xsd",
     "http://www.plcopen.org/xml/tc6_0201")
-PLCOpen_v1_xsd = etree.XMLSchema(etree.fromstring(PLCOpen_v1_xml))
+PLCOpen_v1_xsd = etree.XMLSchema(etree.fromstring(PLCOpen_v1_xml.encode()))
 
 # XPath for file compatibility process
 ProjectResourcesXPath = PLCOpen_XPath("ppx:instances/ppx:configurations/ppx:configuration/ppx:resource")
@@ -301,7 +301,7 @@
 
 
 def LoadProject(filepath):
-    project_file = open(filepath)
+    project_file = open(filepath, encoding='utf-8')
     project_xml = project_file.read()
     project_file.close()
     return LoadProjectXML(project_xml)
@@ -332,11 +332,11 @@
         project,
         pretty_print=True,
         xml_declaration=True,
-        encoding='utf-8')
+        encoding='utf-8').decode()
 
     assert len(content) != 0
         
-    project_file = open(filepath, 'w')
+    project_file = open(filepath, 'w', encoding='utf-8')
     project_file.write(content)
     project_file.close()
 
--- a/targets/toolchain_gcc.py	Fri Oct 28 14:26:17 2022 +0800
+++ b/targets/toolchain_gcc.py	Fri Oct 28 14:53:23 2022 +0800
@@ -136,7 +136,7 @@
         # read source
         src = open(os.path.join(self.buildpath, bn)).read()
         # compute new hash
-        newhash = hashlib.md5(src).hexdigest()
+        newhash = hashlib.md5(src.encode()).hexdigest()
         # compare
         match = (oldhash == newhash)
         if not match:
--- a/util/ProcessLogger.py	Fri Oct 28 14:26:17 2022 +0800
+++ b/util/ProcessLogger.py	Fri Oct 28 14:53:23 2022 +0800
@@ -23,7 +23,6 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 
-
 import os
 import sys
 import subprocess
@@ -142,7 +141,7 @@
         if _debug and self.logger:
             self.logger.write("(DEBUG) launching:\n" + self.Command_str + "\n")
 
-        self.Proc = subprocess.Popen(self.Command, **popenargs)
+        self.Proc = subprocess.Popen(self.Command, encoding="utf-8", **popenargs)
 
         self.outt = outputThread(
             self.Proc,
--- a/xmlclass/xmlclass.py	Fri Oct 28 14:26:17 2022 +0800
+++ b/xmlclass/xmlclass.py	Fri Oct 28 14:53:23 2022 +0800
@@ -1731,7 +1731,7 @@
         return etree.QName(self.tag).localname
 
     def tostring(self):
-        return NAMESPACE_PATTERN.sub("", etree.tostring(self, pretty_print=True, encoding='utf-8')).decode('utf-8')
+        return NAMESPACE_PATTERN.sub("", etree.tostring(self, encoding='unicode'))
 
     def getElementInfos(self, name, path=None, derived=False):
         return {"name": name, "type": TAG, "value": None, "use": None, "children": []}
@@ -1844,7 +1844,7 @@
         self.ClassLookup = class_lookup
 
     def LoadXMLString(self, xml_string):
-        tree = etree.fromstring(xml_string, self)
+        tree = etree.fromstring(xml_string.encode(), self)
         if not self.XSDSchema.validate(tree):
             error = self.XSDSchema.error_log.last_error
             return tree, (error.line, error.message)
@@ -1942,7 +1942,7 @@
         factory.NSMAP,
         factory.etreeNamespaceFormat,
         BaseClass[0] if len(BaseClass) == 1 else None,
-        etree.XMLSchema(etree.fromstring(xsdstring)))
+        etree.XMLSchema(etree.fromstring(xsdstring.encode())))
 
     class_lookup = XMLElementClassLookUp(factory.ComputedClassesLookUp)
     parser.set_element_class_lookup(class_lookup)