Skip to content

Commit

Permalink
Basic fixes for Micropython compatibility
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Buesch <[email protected]>
  • Loading branch information
mbuesch committed Feb 7, 2020
1 parent 71b3873 commit 36f0450
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 17 deletions.
3 changes: 2 additions & 1 deletion awlsim-test
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def usage():
def writeStdout(message):
if Logging.loglevel >= Logging.LOG_INFO:
sys.stdout.write(message)
sys.stdout.flush()
if not isMicroPython:
sys.stdout.flush()

nextScreenUpdate = 0.0
lastDump = ""
Expand Down
10 changes: 10 additions & 0 deletions awlsim/common/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,13 @@ def __exit__(self, *unused):
lambda d: d.keys())
dictValues = py23(lambda d: d.viewvalues(),
lambda d: d.values())

if isMicroPython: #@nocov
import select
if not hasattr(select, "select"):
select.select = None # Dummy

try:
IOError
except NameError:
IOError = OSError
9 changes: 6 additions & 3 deletions awlsim/common/dynamic_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ def importModule(moduleName):
May raise importError."""

import awlsim_loader.cython_helper as cython_helper
try:
import importlib
except ImportError as e: #@nocov
if isMicroPython:
importlib = None
else:
try:
import importlib
except ImportError as e: #@nocov
importlib = None

# If we are running in cython module,
# translate the moduleName to its cython name.
Expand Down
5 changes: 2 additions & 3 deletions awlsim/common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def __print(cls, stream, text):
stream.write("[%.3f] " % cls._getUptime())
stream.write(text)
stream.write("\n")
stream.flush()
if not isMicroPython:
stream.flush()

@classmethod
def printDebug(cls, text): #@nocov
Expand Down Expand Up @@ -188,7 +189,6 @@ def safeFileRead(filename):
try:
with open(filename, "rb") as fd:
data = fd.read()
fd.close()
except IOError as e: #@nocov
raise AwlSimError("Failed to read '%s': %s" %\
(filename, str(e)))
Expand All @@ -206,7 +206,6 @@ def safeFileWrite(filename, data):
with open(tmpFile, "wb") as fd:
fd.write(data)
fd.flush()
fd.close()
if not osIsPosix: #@nocov
# Can't use safe rename on non-POSIX.
# Must unlink first.
Expand Down
4 changes: 2 additions & 2 deletions awlsim/core/systemblocks/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#from awlsim.common.cython_support cimport * #@cy
from awlsim.common.compat import *

from awlsim.core.systemblocks.system_sfb import * #+cimport
from awlsim.core.systemblocks.system_sfc import * #+cimport
from awlsim.core.systemblocks.system_sfc import _SFC_table #+cimport
from awlsim.core.systemblocks.system_sfb import _SFB_table #+cimport

__all__ = [
"SFC_table",
Expand Down
19 changes: 11 additions & 8 deletions awlsim/coreserver/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,11 +702,11 @@ def fromBytes(cls, payload):
class AwlSimMessage_BUILD(AwlSimMessage):
msgId = AwlSimMessage.MSG_ID_BUILD

plStruct = struct.Struct(str(">32x"))
plStruct = struct.Struct(str(">8I"))

def toBytes(self):
try:
pl = self.plStruct.pack()
pl = self.plStruct.pack(0, 0, 0, 0, 0, 0, 0, 0)
return AwlSimMessage.toBytes(self, len(pl)) + pl
except (ValueError, struct.error) as e:
raise TransferError("BUILD: Invalid data format")
Expand Down Expand Up @@ -1536,14 +1536,14 @@ class AwlSimMessage_IDENTS(AwlSimMessage):
# reserved (32 bit)
# reserved (32 bit)
# reserved (32 bit)
plHdrStruct = struct.Struct(str(">6I40x"))
plHdrStruct = struct.Struct(str(">16I"))

# Payload module header struct:
# Number of parameters (32 bit)
# reserved (32 bit)
# reserved (32 bit)
# reserved (32 bit)
plModStruct = struct.Struct(str(">I12x"))
plModStruct = struct.Struct(str(">4I"))

# awlSources: List of AwlSource()s
# symTabSources: List of SymTabSource()s
Expand All @@ -1563,7 +1563,9 @@ def toBytes(self):
len(self.hwMods),
len(self.libSelections),
len(self.fupSources),
len(self.kopSources)) ]
len(self.kopSources),
0, 0, 0, 0, 0,
0, 0, 0, 0, 0) ]
def addSrcs(srcs):
for src in srcs:
payload.append(self.packString(src.name))
Expand All @@ -1573,7 +1575,7 @@ def addSrcs(srcs):
addSrcs(self.symTabSources)
for hwmodDesc in self.hwMods:
params = hwmodDesc.getParameters()
payload.append(self.plModStruct.pack(len(params)))
payload.append(self.plModStruct.pack(len(params), 0, 0, 0))
payload.append(self.packString(hwmodDesc.getModuleName()))
for pName, pVal in dictItems(params):
payload.append(self.packString(pName))
Expand All @@ -1595,7 +1597,8 @@ def fromBytes(cls, payload):
fupSources = []
kopSources = []
offset = 0
nrAwl, nrSym, nrHw, nrLib, nrFup, nrKop = cls.plHdrStruct.unpack_from(
nrAwl, nrSym, nrHw, nrLib, nrFup, nrKop,\
_, _, _, _, _, _, _, _, _, _ = cls.plHdrStruct.unpack_from(
payload, offset)
offset += cls.plHdrStruct.size
def unpackSrcs(srcClass, sourcesList, count, offset):
Expand All @@ -1613,7 +1616,7 @@ def unpackSrcs(srcClass, sourcesList, count, offset):
offset = unpackSrcs(AwlSource, awlSources, nrAwl, offset)
offset = unpackSrcs(SymTabSource, symTabSources, nrSym, offset)
for i in range(nrHw):
(nrParam, ) = cls.plModStruct.unpack_from(
nrParam, _, _, _ = cls.plModStruct.unpack_from(
payload, offset)
offset += cls.plModStruct.size
modName, count = cls.unpackString(payload, offset)
Expand Down

0 comments on commit 36f0450

Please sign in to comment.