Commit 8da40baa authored by Damien George's avatar Damien George

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: default avatarDamien George <damien@micropython.org>
parent 0c0cef98
# test importing of invalid .mpy files # test importing of invalid .mpy files
import sys, uio
try: try:
uio.IOBase import sys, uio, uos
import uos
uio.IOBase
uos.mount uos.mount
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
...@@ -14,18 +12,13 @@ except (ImportError, AttributeError): ...@@ -14,18 +12,13 @@ except (ImportError, AttributeError):
class UserFile(uio.IOBase): class UserFile(uio.IOBase):
def __init__(self, data): def __init__(self, data):
self.data = data self.data = memoryview(data)
self.pos = 0 self.pos = 0
def read(self):
return self.data
def readinto(self, buf): def readinto(self, buf):
n = 0 n = min(len(buf), len(self.data) - self.pos)
while n < len(buf) and self.pos < len(self.data): buf[:n] = self.data[self.pos : self.pos + n]
buf[n] = self.data[self.pos] self.pos += n
n += 1
self.pos += 1
return n return n
def ioctl(self, req, arg): def ioctl(self, req, arg):
......
# test importing of .mpy files with native code (x64 only) # test importing of .mpy files with native code (x64 only)
import sys, uio
try: try:
uio.IOBase import sys, uio, uos
import uos
uio.IOBase
uos.mount uos.mount
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
...@@ -18,18 +16,13 @@ if not (sys.platform == "linux" and sys.maxsize > 2 ** 32): ...@@ -18,18 +16,13 @@ if not (sys.platform == "linux" and sys.maxsize > 2 ** 32):
class UserFile(uio.IOBase): class UserFile(uio.IOBase):
def __init__(self, data): def __init__(self, data):
self.data = data self.data = memoryview(data)
self.pos = 0 self.pos = 0
def read(self):
return self.data
def readinto(self, buf): def readinto(self, buf):
n = 0 n = min(len(buf), len(self.data) - self.pos)
while n < len(buf) and self.pos < len(self.data): buf[:n] = self.data[self.pos : self.pos + n]
buf[n] = self.data[self.pos] self.pos += n
n += 1
self.pos += 1
return n return n
def ioctl(self, req, arg): def ioctl(self, req, arg):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment