str encoding fixes python3
authorGP Orcullo <kinsamanka@gmail.com>
Tue, 01 Nov 2022 14:21:16 +0800
branchpython3
changeset 3772 ec2babbd5698
parent 3771 67a0df6478b3
child 3773 ab11852616b8
str encoding fixes
connectors/ConnectorBase.py
controls/DebugVariablePanel/DebugVariableItem.py
runtime/PLCObject.py
--- a/connectors/ConnectorBase.py	Tue Nov 01 14:19:23 2022 +0800
+++ b/connectors/ConnectorBase.py	Tue Nov 01 14:21:16 2022 +0800
@@ -13,7 +13,7 @@
 
     def BlobFromFile(self, filepath, seed):
         s = hashlib.new('md5')
-        s.update(seed)
+        s.update(seed.encode())
         blobID = self.SeedBlob(seed)
         with open(filepath, "rb") as f:
             while blobID == s.digest():
--- a/controls/DebugVariablePanel/DebugVariableItem.py	Tue Nov 01 14:19:23 2022 +0800
+++ b/controls/DebugVariablePanel/DebugVariableItem.py	Tue Nov 01 14:21:16 2022 +0800
@@ -255,7 +255,7 @@
 
                 if self.VariableType in ["STRING", "WSTRING"]:
                     # String data value is CRC
-                    num_value = (binascii.crc32(value) & STRING_CRC_MASK)
+                    num_value = (binascii.crc32(value.encode()) & STRING_CRC_MASK)
                 elif self.VariableType in ["TIME", "TOD", "DT", "DATE"]:
                     # Numeric value of time type variables
                     # is represented in seconds
--- a/runtime/PLCObject.py	Tue Nov 01 14:19:23 2022 +0800
+++ b/runtime/PLCObject.py	Tue Nov 01 14:21:16 2022 +0800
@@ -132,7 +132,7 @@
             msg, = args
         PLCprint(msg)
         if self._LogMessage is not None:
-            return self._LogMessage(level, msg, len(msg))
+            return self._LogMessage(level, msg.encode(), len(msg))
         return None
 
     @RunInMain
@@ -160,8 +160,8 @@
                                      ctypes.byref(tv_sec),
                                      ctypes.byref(tv_nsec))
             if sz and sz <= maxsz:
-                self._log_read_buffer[sz] = '\x00'
-                return self._log_read_buffer.value, tick.value, tv_sec.value, tv_nsec.value
+                return (self._log_read_buffer[:sz].decode(), tick.value,
+                        tv_sec.value, tv_nsec.value)
         elif self._loading_error is not None and level == 0:
             return self._loading_error, 0, 0, 0
         return None
@@ -185,7 +185,7 @@
 
             self.PLC_ID = ctypes.c_char_p.in_dll(self.PLClibraryHandle, "PLC_ID")
             if len(md5) == 32:
-                self.PLC_ID.value = md5
+                self.PLC_ID.value = md5.encode()
 
             self._startPLC = self.PLClibraryHandle.startPLC
             self._startPLC.restype = ctypes.c_int
@@ -422,10 +422,11 @@
         res, cmd, blkid = "None", "None", ctypes.c_void_p()
         compile_cache = {}
         while True:
-            cmd = self._PythonIterator(res, blkid)
+            cmd = self._PythonIterator(res.encode(), blkid)
             FBID = blkid.value
             if cmd is None:
                 break
+            cmd = cmd.decode()
             try:
                 self.python_runtime_vars["FBID"] = FBID
                 ccmd, AST = compile_cache.get(FBID, (None, None))
@@ -489,7 +490,7 @@
         self.PythonThreadCondLock.release()
 
     def _fail(self, msg):
-        self.LogMessage(0, msg)
+        self.LogMessage(0, msg.decode())
         self.PLCStatus = PlcStatus.Broken
         self.StatusChange()
 
@@ -574,7 +575,7 @@
     def SeedBlob(self, seed):
         blob = (mkstemp(dir=self.tmpdir) + (hashlib.new('md5'),))
         _fd, _path, md5sum = blob
-        md5sum.update(seed)
+        md5sum.update(seed.encode())
         newBlobID = md5sum.digest()
         self.blobs[newBlobID] = blob
         return newBlobID
@@ -603,7 +604,8 @@
         blob = self.blobs.pop(blobID, None)
 
         if blob is None:
-            raise Exception(_("Missing data to create file: {}").format(newpath))
+            raise Exception(
+                _(f"Missing data to create file: {newpath}").decode())
 
         self._BlobAsFile(blob, newpath)