From 8da40baa47ee9fe7aac228af2c0addd1f4ce3646 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 26 Jul 2020 14:43:13 +1000 Subject: [PATCH] tests/micropython: Improve .mpy import tests to run on more targets. All imports are now tested to see if the test should be skipped, UserFile.read is removed, and UserFile.readinto is made more efficient. Signed-off-by: Damien George --- tests/micropython/import_mpy_invalid.py | 19 ++++++------------- tests/micropython/import_mpy_native_x64.py | 19 ++++++------------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/tests/micropython/import_mpy_invalid.py b/tests/micropython/import_mpy_invalid.py index d6d01e7f1f..00973fe3f9 100644 --- a/tests/micropython/import_mpy_invalid.py +++ b/tests/micropython/import_mpy_invalid.py @@ -1,11 +1,9 @@ # test importing of invalid .mpy files -import sys, uio - try: - uio.IOBase - import uos + import sys, uio, uos + uio.IOBase uos.mount except (ImportError, AttributeError): print("SKIP") @@ -14,18 +12,13 @@ except (ImportError, AttributeError): class UserFile(uio.IOBase): def __init__(self, data): - self.data = data + self.data = memoryview(data) self.pos = 0 - def read(self): - return self.data - def readinto(self, buf): - n = 0 - while n < len(buf) and self.pos < len(self.data): - buf[n] = self.data[self.pos] - n += 1 - self.pos += 1 + n = min(len(buf), len(self.data) - self.pos) + buf[:n] = self.data[self.pos : self.pos + n] + self.pos += n return n def ioctl(self, req, arg): diff --git a/tests/micropython/import_mpy_native_x64.py b/tests/micropython/import_mpy_native_x64.py index 5d7bdab4a2..d0de507d44 100644 --- a/tests/micropython/import_mpy_native_x64.py +++ b/tests/micropython/import_mpy_native_x64.py @@ -1,11 +1,9 @@ # test importing of .mpy files with native code (x64 only) -import sys, uio - try: - uio.IOBase - import uos + import sys, uio, uos + uio.IOBase uos.mount except (ImportError, AttributeError): print("SKIP") @@ -18,18 +16,13 @@ if not (sys.platform == "linux" and sys.maxsize > 2 ** 32): class UserFile(uio.IOBase): def __init__(self, data): - self.data = data + self.data = memoryview(data) self.pos = 0 - def read(self): - return self.data - def readinto(self, buf): - n = 0 - while n < len(buf) and self.pos < len(self.data): - buf[n] = self.data[self.pos] - n += 1 - self.pos += 1 + n = min(len(buf), len(self.data) - self.pos) + buf[:n] = self.data[self.pos : self.pos + n] + self.pos += n return n def ioctl(self, req, arg):