2019-12-27 01:20:15 +00:00
|
|
|
# Test VfsFat class and its finaliser
|
|
|
|
|
|
|
|
try:
|
|
|
|
import uerrno, uos
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2019-12-27 01:20:15 +00:00
|
|
|
uos.VfsFat
|
|
|
|
except (ImportError, AttributeError):
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
|
|
|
|
class RAMBlockDevice:
|
|
|
|
def __init__(self, blocks, sec_size=512):
|
|
|
|
self.sec_size = sec_size
|
|
|
|
self.data = bytearray(blocks * self.sec_size)
|
|
|
|
|
|
|
|
def readblocks(self, n, buf):
|
|
|
|
for i in range(len(buf)):
|
|
|
|
buf[i] = self.data[n * self.sec_size + i]
|
|
|
|
|
|
|
|
def writeblocks(self, n, buf):
|
|
|
|
for i in range(len(buf)):
|
|
|
|
self.data[n * self.sec_size + i] = buf[i]
|
|
|
|
|
|
|
|
def ioctl(self, op, arg):
|
2020-03-23 02:26:08 +00:00
|
|
|
if op == 4: # MP_BLOCKDEV_IOCTL_BLOCK_COUNT
|
2019-12-27 01:20:15 +00:00
|
|
|
return len(self.data) // self.sec_size
|
2020-03-23 02:26:08 +00:00
|
|
|
if op == 5: # MP_BLOCKDEV_IOCTL_BLOCK_SIZE
|
2019-12-27 01:20:15 +00:00
|
|
|
return self.sec_size
|
|
|
|
|
|
|
|
|
|
|
|
# Create block device, and skip test if not enough RAM
|
|
|
|
try:
|
|
|
|
bdev = RAMBlockDevice(50)
|
|
|
|
except MemoryError:
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
# Format block device and create VFS object
|
|
|
|
uos.VfsFat.mkfs(bdev)
|
|
|
|
vfs = uos.VfsFat(bdev)
|
|
|
|
|
|
|
|
# Here we test that opening a file with the heap locked fails correctly. This
|
|
|
|
# is a special case because file objects use a finaliser and allocating with a
|
|
|
|
# finaliser is a different path to normal allocation. It would be better to
|
|
|
|
# test this in the core tests but there are no core objects that use finaliser.
|
|
|
|
import micropython
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2019-12-27 01:20:15 +00:00
|
|
|
micropython.heap_lock()
|
|
|
|
try:
|
2020-03-23 02:26:08 +00:00
|
|
|
vfs.open("x", "r")
|
2019-12-27 01:20:15 +00:00
|
|
|
except MemoryError:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("MemoryError")
|
2019-12-27 01:20:15 +00:00
|
|
|
micropython.heap_unlock()
|
|
|
|
|
|
|
|
# Here we test that the finaliser is actually called during a garbage collection.
|
|
|
|
import gc
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2019-12-27 01:20:15 +00:00
|
|
|
N = 4
|
|
|
|
for i in range(N):
|
2020-03-23 02:26:08 +00:00
|
|
|
n = "x%d" % i
|
|
|
|
f = vfs.open(n, "w")
|
2019-12-27 01:20:15 +00:00
|
|
|
f.write(n)
|
2020-03-23 02:26:08 +00:00
|
|
|
f = None # release f without closing
|
|
|
|
[0, 1, 2, 3] # use up Python stack so f is really gone
|
|
|
|
gc.collect() # should finalise all N files by closing them
|
2019-12-27 01:20:15 +00:00
|
|
|
for i in range(N):
|
2020-03-23 02:26:08 +00:00
|
|
|
with vfs.open("x%d" % i, "r") as f:
|
2019-12-27 01:20:15 +00:00
|
|
|
print(f.read())
|