|
| 1 | +# test importing of .mpy files with native code (x64 only) |
| 2 | + |
| 3 | +import sys, uio |
| 4 | + |
| 5 | +try: |
| 6 | + uio.IOBase |
| 7 | + import uos |
| 8 | + uos.mount |
| 9 | +except (ImportError, AttributeError): |
| 10 | + print("SKIP") |
| 11 | + raise SystemExit |
| 12 | + |
| 13 | +if not (sys.platform == 'linux' and sys.maxsize > 2 ** 32): |
| 14 | + print("SKIP") |
| 15 | + raise SystemExit |
| 16 | + |
| 17 | + |
| 18 | +class UserFile(uio.IOBase): |
| 19 | + def __init__(self, data): |
| 20 | + self.data = data |
| 21 | + self.pos = 0 |
| 22 | + def read(self): |
| 23 | + return self.data |
| 24 | + def readinto(self, buf): |
| 25 | + n = 0 |
| 26 | + while n < len(buf) and self.pos < len(self.data): |
| 27 | + buf[n] = self.data[self.pos] |
| 28 | + n += 1 |
| 29 | + self.pos += 1 |
| 30 | + return n |
| 31 | + def ioctl(self, req, arg): |
| 32 | + return 0 |
| 33 | + |
| 34 | + |
| 35 | +class UserFS: |
| 36 | + def __init__(self, files): |
| 37 | + self.files = files |
| 38 | + def mount(self, readonly, mksfs): |
| 39 | + pass |
| 40 | + def umount(self): |
| 41 | + pass |
| 42 | + def stat(self, path): |
| 43 | + if path in self.files: |
| 44 | + return (32768, 0, 0, 0, 0, 0, 0, 0, 0, 0) |
| 45 | + raise OSError |
| 46 | + def open(self, path, mode): |
| 47 | + return UserFile(self.files[path]) |
| 48 | + |
| 49 | + |
| 50 | +# these are the test .mpy files |
| 51 | +user_files = { |
| 52 | + # bad architecture |
| 53 | + '/mod0.mpy': b'M\x04\xff\x00\x10', |
| 54 | + |
| 55 | + # test loading of viper and asm |
| 56 | + '/mod1.mpy': ( |
| 57 | + b'M\x04\x0b\x1f\x20' # header |
| 58 | + |
| 59 | + b'\x38' # n bytes, bytecode |
| 60 | + b'\x01\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\xff' # prelude |
| 61 | + b'\x11' # LOAD_CONST_NONE |
| 62 | + b'\x5b' # RETURN_VALUE |
| 63 | + |
| 64 | + b'\x02m\x02m\x00\x02' # simple_name, source_file, n_obj, n_raw_code |
| 65 | + |
| 66 | + b'\x22' # n bytes, viper code |
| 67 | + b'\x00\x00\x00\x00\x00\x00' # dummy machine code |
| 68 | + b'\x00\x00' # qstr0 |
| 69 | + b'\x01\x0c\x0aprint' # n_qstr, qstr0 |
| 70 | + b'\x00\x00\x00' # scope_flags, n_obj, n_raw_code |
| 71 | + |
| 72 | + b'\x23' # n bytes, asm code |
| 73 | + b'\x00\x00\x00\x00\x00\x00\x00\x00' # dummy machine code |
| 74 | + b'\x00\x00\x00' # scope_flags, n_pos_args, type_sig |
| 75 | + ), |
| 76 | +} |
| 77 | + |
| 78 | +# create and mount a user filesystem |
| 79 | +uos.mount(UserFS(user_files), '/userfs') |
| 80 | +sys.path.append('/userfs') |
| 81 | + |
| 82 | +# import .mpy files from the user filesystem |
| 83 | +for i in range(len(user_files)): |
| 84 | + mod = 'mod%u' % i |
| 85 | + try: |
| 86 | + __import__(mod) |
| 87 | + print(mod, 'OK') |
| 88 | + except ValueError as er: |
| 89 | + print(mod, 'ValueError', er) |
| 90 | + |
| 91 | +# unmount and undo path addition |
| 92 | +uos.umount('/userfs') |
| 93 | +sys.path.pop() |
0 commit comments