Fixes to convert filenames to str rather than bytes

This commit is contained in:
Rob McMullen 2018-06-23 22:05:35 -07:00
parent 800c01ccde
commit 1968cd9717
4 changed files with 21 additions and 24 deletions

View File

@ -106,7 +106,7 @@ class AtariDosDirent(Dirent):
@property @property
def filename(self): def filename(self):
ext = (b'.' + self.ext) if self.ext else b'' ext = (b'.' + self.ext) if self.ext else b''
return self.basename + ext return (self.basename + ext).decode('utf-8')
@property @property
def summary(self): def summary(self):

View File

@ -276,7 +276,6 @@ class DiskImageBase(object):
# check if we've been passed a dirent instead of a filename # check if we've been passed a dirent instead of a filename
if hasattr(filename, "filename"): if hasattr(filename, "filename"):
return filename return filename
if type(filename) is not bytes: filename = filename.encode("utf-8")
for dirent in self.files: for dirent in self.files:
if filename == dirent.filename: if filename == dirent.filename:
return dirent return dirent

View File

@ -164,7 +164,7 @@ class Dos33Dirent(Dirent):
self.deleted = False self.deleted = False
self.track = 0 self.track = 0
self.sector = 0 self.sector = 0
self.filename = b'' self.filename = ""
self.num_sectors = 0 self.num_sectors = 0
self.is_sane = True self.is_sane = True
self.current_sector_index = 0 self.current_sector_index = 0
@ -174,7 +174,7 @@ class Dos33Dirent(Dirent):
self.parse_raw_dirent(image, bytes) self.parse_raw_dirent(image, bytes)
def __str__(self): def __str__(self):
return "File #%-2d (%s) %03d %-30s %03d %03d" % (self.file_num, self.summary, self.num_sectors, self.filename.decode("ascii", errors='ignore'), self.track, self.sector) return "File #%-2d (%s) %03d %-30s %03d %03d" % (self.file_num, self.summary, self.num_sectors, self.filename, self.track, self.sector)
def __eq__(self, other): def __eq__(self, other):
return self.__class__ == other.__class__ and self.filename == other.filename and self.track == other.track and self.sector == other.sector and self.num_sectors == other.num_sectors return self.__class__ == other.__class__ and self.filename == other.filename and self.track == other.track and self.sector == other.sector and self.num_sectors == other.num_sectors
@ -226,20 +226,20 @@ class Dos33Dirent(Dirent):
lines.append("sector map: " + str(self.sector_map)) lines.append("sector map: " + str(self.sector_map))
return "\n".join(lines) return "\n".join(lines)
def parse_raw_dirent(self, image, bytes): def parse_raw_dirent(self, image, data):
if bytes is None: if data is None:
return return
values = bytes.view(dtype=self.format)[0] values = data.view(dtype=self.format)[0]
self.track = values[0] self.track = values[0]
if self.track == 0xff: if self.track == 0xff:
self.deleted = True self.deleted = True
self.track = bytes[0x20] self.track = data[0x20]
else: else:
self.deleted = False self.deleted = False
self.sector = values[1] self.sector = values[1]
self._file_type = values[2] & 0x7f self._file_type = values[2] & 0x7f
self.locked = values[2] & 0x80 self.locked = values[2] & 0x80
self.filename = (bytes[3:0x20] - 0x80).tobytes().rstrip() self.filename = (data[3:0x20] - 0x80).tobytes().rstrip().decode("ascii", errors='ignore')
self.num_sectors = int(values[4]) self.num_sectors = int(values[4])
self.is_sane = self.sanity_check(image) self.is_sane = self.sanity_check(image)
@ -250,7 +250,7 @@ class Dos33Dirent(Dirent):
values[1] = self.sector values[1] = self.sector
values[2] = self.flag values[2] = self.flag
n = min(len(self.filename), 30) n = min(len(self.filename), 30)
data[3:3+n] = np.fromstring(self.filename, dtype=np.uint8) | 0x80 data[3:3+n] = np.fromstring(self.filename.encode("ascii"), dtype=np.uint8) | 0x80
data[3+n:] = ord(' ') | 0x80 data[3+n:] = ord(' ') | 0x80
if self.deleted: if self.deleted:
data[0x20] = self.track data[0x20] = self.track
@ -346,8 +346,7 @@ class Dos33Dirent(Dirent):
return self.filename return self.filename
def set_values(self, filename, filetype, index): def set_values(self, filename, filetype, index):
if type(filename) is not bytes: filename = filename.encode("utf-8") self.filename = '%-30s' % filename[0:30]
self.filename = b'%-30s' % filename[0:30]
self._file_type = self.text_to_type.get(filetype, 0x04) self._file_type = self.text_to_type.get(filetype, 0x04)
self.locked = False self.locked = False
self.deleted = False self.deleted = False

View File

@ -17,13 +17,12 @@ class BaseFilesystemModifyTest(object):
rawdata = SegmentData(self.sample_data.copy()) rawdata = SegmentData(self.sample_data.copy())
self.image = self.diskimage_type(rawdata) self.image = self.diskimage_type(rawdata)
def check_entries(self, entries, prefix=b"TEST", save=None): def check_entries(self, entries, prefix="TEST", save=None):
if type(prefix) is not bytes: prefix = prefix.encode("utf-8")
orig_num_files = len(self.image.files) orig_num_files = len(self.image.files)
filenames = [] filenames = []
count = 1 count = 1
for data in entries: for data in entries:
filename = b"%s%d.BIN" % (prefix, count) filename = "%s%d.BIN" % (prefix, count)
self.image.write_file(filename, None, data) self.image.write_file(filename, None, data)
assert len(self.image.files) == orig_num_files + count assert len(self.image.files) == orig_num_files + count
data2 = np.fromstring(self.image.find_file(filename), dtype=np.uint8) data2 = np.fromstring(self.image.find_file(filename), dtype=np.uint8)
@ -33,7 +32,7 @@ class BaseFilesystemModifyTest(object):
# loop over them again to make sure data wasn't overwritten # loop over them again to make sure data wasn't overwritten
count = 1 count = 1
for data in entries: for data in entries:
filename = b"%s%d.BIN" % (prefix, count) filename = "%s%d.BIN" % (prefix, count)
data2 = np.fromstring(self.image.find_file(filename), dtype=np.uint8) data2 = np.fromstring(self.image.find_file(filename), dtype=np.uint8)
assert np.array_equal(data, data2[0:len(data)]) assert np.array_equal(data, data2[0:len(data)])
count += 1 count += 1
@ -48,20 +47,20 @@ class BaseFilesystemModifyTest(object):
assert len(self.image.files) == self.num_files_in_sample assert len(self.image.files) == self.num_files_in_sample
data = np.asarray([0xff, 0xff, 0x00, 0x60, 0x01, 0x60, 1, 2], dtype=np.uint8) data = np.asarray([0xff, 0xff, 0x00, 0x60, 0x01, 0x60, 1, 2], dtype=np.uint8)
self.image.write_file(b"TEST.XEX", None, data) self.image.write_file("TEST.XEX", None, data)
assert len(self.image.files) == self.num_files_in_sample + 1 assert len(self.image.files) == self.num_files_in_sample + 1
data2 = np.fromstring(self.image.find_file(b"TEST.XEX"), dtype=np.uint8) data2 = np.fromstring(self.image.find_file("TEST.XEX"), dtype=np.uint8)
assert np.array_equal(data, data2[0:len(data)]) assert np.array_equal(data, data2[0:len(data)])
def test_50k(self): def test_50k(self):
assert len(self.image.files) == self.num_files_in_sample assert len(self.image.files) == self.num_files_in_sample
data = np.arange(50*1024, dtype=np.uint8) data = np.arange(50*1024, dtype=np.uint8)
self.image.write_file(b"RAMP50K.BIN", None, data) self.image.write_file("RAMP50K.BIN", None, data)
assert len(self.image.files) == self.num_files_in_sample + 1 assert len(self.image.files) == self.num_files_in_sample + 1
data2 = self.image.find_file(b"RAMP50K.BIN") data2 = self.image.find_file("RAMP50K.BIN")
assert data.tostring() == data2 assert data.tostring() == data2
def test_many_small(self): def test_many_small(self):
@ -78,17 +77,17 @@ class BaseFilesystemModifyTest(object):
np.arange(9*1024, dtype=np.uint8), np.arange(9*1024, dtype=np.uint8),
np.arange(10*1024, dtype=np.uint8), np.arange(10*1024, dtype=np.uint8),
] ]
self.check_entries(entries, save=b"many_small.atr") self.check_entries(entries, save="many_small.atr")
def test_big_failure(self): def test_big_failure(self):
assert len(self.image.files) == self.num_files_in_sample assert len(self.image.files) == self.num_files_in_sample
data = np.arange(50*1024, dtype=np.uint8) data = np.arange(50*1024, dtype=np.uint8)
self.image.write_file(b"RAMP50K.BIN", None, data) self.image.write_file("RAMP50K.BIN", None, data)
assert len(self.image.files) == self.num_files_in_sample + 1 assert len(self.image.files) == self.num_files_in_sample + 1
with pytest.raises(NotEnoughSpaceOnDisk): with pytest.raises(NotEnoughSpaceOnDisk):
huge = np.arange(500*1024, dtype=np.uint8) huge = np.arange(500*1024, dtype=np.uint8)
self.image.write_file(b"RAMP500K.BIN", None, huge) self.image.write_file("RAMP500K.BIN", None, huge)
assert len(self.image.files) == self.num_files_in_sample + 1 assert len(self.image.files) == self.num_files_in_sample + 1
def test_delete(self): def test_delete(self):
@ -118,7 +117,7 @@ class BaseFilesystemModifyTest(object):
self.image.delete_file(filenames[8]) self.image.delete_file(filenames[8])
assert len(self.image.files) == self.num_files_in_sample + 7 assert len(self.image.files) == self.num_files_in_sample + 7
filename = self.check_entries(entries2, b"SECOND", save="test_delete.atr") filename = self.check_entries(entries2, "SECOND", save="test_delete.atr")
assert len(self.image.files) == self.num_files_in_sample + 9 assert len(self.image.files) == self.num_files_in_sample + 9
def test_delete_all(self): def test_delete_all(self):