From d5771aeba97c08ecbe6fa3350c4e8ecdfcbb07a1 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Thu, 6 Jul 2023 00:46:59 +0200 Subject: [PATCH 01/15] Fixing exceptions Making exceptions compatible with Python3 syntax --- gui.py | 38 ++++++++-------- lzari.py | 4 +- mymc.py | 32 ++++++------- ps2mc.py | 129 ++++++++++++++++++++++++++--------------------------- ps2save.py | 40 ++++++++--------- 5 files changed, 121 insertions(+), 122 deletions(-) diff --git a/gui.py b/gui.py index d14324e..357462f 100755 --- a/gui.py +++ b/gui.py @@ -634,22 +634,22 @@ def _close_mc(self): if self.mc != None: try: self.mc.close() - except EnvironmentError, value: - self.mc_error(value) + except EnvironmentError as e: + self.mc_error(e) self.mc = None if self.f != None: try: self.f.close() - except EnvironmentError, value: - self.mc_error(value) + except EnvironmentError as e: + self.mc_error(e) self.f = None self.mcname = None def refresh(self): try: self.dirlist.update(self.mc) - except EnvironmentError, value: - self.mc_error(value) + except EnvironmentError as e: + self.mc_error(e) self._close_mc() self.dirlist.update(None) @@ -676,7 +676,7 @@ def open_mc(self, filename): try: f = file(filename, "r+b") mc = ps2mc.ps2mc(f) - except EnvironmentError, value: + except EnvironmentError as e: if f != None: f.close() self.mc_error(value, filename) @@ -718,8 +718,8 @@ def evt_dirlist_item_focused(self, event): icon = f.read() finally: f.close() - except EnvironmentError, value: - print "icon failed to load", value + except EnvironmentError as e: + print "icon failed to load", e self.icon_win.load_icon(None, None) return @@ -758,8 +758,8 @@ def evt_cmd_export(self, event): sf = mc.export_save_file("/" + dirname) longname = ps2save.make_longname(dirname, sf) sfiles.append((dirname, sf, longname)) - except EnvironmentError, value: - self.mc_error(value. dirname) + except EnvironmentError as e: + self.mc_error(e.dirname) if len(sfiles) == 0: return @@ -786,8 +786,8 @@ def evt_cmd_export(self, event): sf.save_ems(f) finally: f.close() - except EnvironmentError, value: - self.mc_error(value, fn) + except EnvironmentError as e: + self.mc_error(e, fn) return dir = os.path.dirname(fn) @@ -808,8 +808,8 @@ def evt_cmd_export(self, event): sf.save_ems(f) f.close() count += 1 - except EnvironmentError, value: - self.mc_error(value, fn) + except EnvironmentError as e: + self.mc_error(e, fn) if count > 0: if os.path.isabs(dir): self.config.set_savefile_dir(dir) @@ -868,8 +868,8 @@ def evt_cmd_import(self, event): try: self._do_import(fn) success = fn - except EnvironmentError, value: - self.mc_error(value, fn) + except EnvironmentError as e: + self.mc_error(e, fn) if success != None: dir = os.path.dirname(success) @@ -904,8 +904,8 @@ def evt_cmd_delete(self, event): for dn in dirnames: try: mc.rmdir("/" + dn) - except EnvironmentError, value: - self.mc_error(value, dn) + except EnvironmentError as e: + self.mc_error(e, dn) mc.check() self.refresh() diff --git a/lzari.py b/lzari.py index de71086..e1c7c7c 100755 --- a/lzari.py +++ b/lzari.py @@ -646,7 +646,7 @@ def decode(src, out_length, progress = None): out = ctypes.create_string_buffer(out_length) if (mylzari_decode(src, len(src), out, out_length, progress) == -1): - raise ValueError, "compressed input is corrupt" + raise ValueError("compressed input is corrupt") return ctypes.string_at(out, out_length) def encode(src, progress = None): @@ -654,7 +654,7 @@ def encode(src, progress = None): progress) # print r, compressed.value, comp_len if r == -1: - raise MemoryError, "out of memory during compression" + raise MemoryError("out of memory during compression") if compressed.value == None: return "" ret = ctypes.string_at(compressed.value, comp_len.value) diff --git a/mymc.py b/mymc.py index 51068ec..122139c 100755 --- a/mymc.py +++ b/mymc.py @@ -50,7 +50,7 @@ def __getattribute__(self, name): def __setattr__(self, name, value): if name == "encoding": - raise TypeError, "readonly attribute" + raise TypeError("readonly attribute") return setattr(object.__getattribute__(self, "_f"), name, value) @@ -218,12 +218,12 @@ def do_import(cmd, mc, opts, args, opterr): elif ftype == "sps": sf.load_sharkport(f) elif ftype == "npo": - raise io_error, (EIO, "nPort saves" + raise io_error((EIO, "nPort saves" " are not supported.", - filename) + filename)) else: - raise io_error, (EIO, "Save file format not" - " recognized", filename) + raise io_error((EIO, "Save file format not" + " recognized", filename)) finally: f.close() dirname = opts.directory @@ -447,7 +447,7 @@ def do_format(cmd, mcname, opts, args, opterr): except EnvironmentError: exists = False if exists: - raise io_error, (EEXIST, "file exists", mcname) + raise io_error(EEXIST, "file exists", mcname) f = file(mcname, "w+b") try: @@ -688,7 +688,7 @@ class suboption_parser(optparse.OptionParser): def exit(self, status = 0, msg = None): if msg: sys.stderr.write(msg) - raise subopt_error, status + raise subopt_error(status) class my_help_formatter(optparse.IndentedHelpFormatter): """A better formatter for optparser's help message""" @@ -791,12 +791,12 @@ def main(): # print "f.close()" f.close() - except EnvironmentError, value: - if getattr(value, "filename", None) != None: - write_error(value.filename, value.strerror) + except EnvironmentError as e: + if getattr(e, "filename", None) != None: + write_error(e.filename, e.strerror) ret = 1 - elif getattr(value, "strerror", None) != None: - write_error(mcname, value.strerror) + elif getattr(e, "strerror", None) != None: + write_error(mcname, e.strerror) ret = 1 else: # something weird @@ -804,14 +804,14 @@ def main(): if opts.debug: raise - except subopt_error, (ret,): + except subopt_error: pass - except (ps2mc.error, ps2save.error), value: - fn = getattr(value, "filename", None) + except (ps2mc.error, ps2save.error) as e: + fn = getattr(e, "filename", None) if fn == None: fn = mcname - write_error(fn, str(value)) + write_error(fn, str(e)) if opts.debug: raise ret = 1 diff --git a/ps2mc.py b/ps2mc.py index 8e3ca21..1f86ab0 100755 --- a/ps2mc.py +++ b/ps2mc.py @@ -336,7 +336,7 @@ def write_file_cluster(self, n, buf): if (cluster < file_cluster_end or len(self.fat_chain) != file_cluster_end): - raise corrupt, ("file length doesn't match cluster" + raise corrupt("file length doesn't match cluster" " chain length", mc.f) for i in range(file_cluster_end, n): @@ -370,7 +370,7 @@ def update_notify(self, first_cluster, length): def read(self, size = None, eol = None): if self.closed: - raise ValueError, "file is closed" + raise ValueError("file is closed") pos = self._pos cluster_size = self.mc.cluster_size @@ -397,14 +397,14 @@ def read(self, size = None, eol = None): def write(self, out, _set_modified = True): if self.closed: - raise ValueError, "file is closed" + raise ValueError("file is closed") cluster_size = self.mc.cluster_size pos = self._pos if self._append: pos = self.length elif not self._write: - raise io_error, (EACCES, "file not opened for writing", + raise io_error(EACCES, "file not opened for writing", self.name) size = len(out) @@ -424,7 +424,7 @@ def write(self, out, _set_modified = True): buf = "\0" * cluster_size buf = buf[:off] + s + buf[off + l:] if not self.write_file_cluster(cluster, buf): - raise io_error, (ENOSPC, + raise io_error(ENOSPC, "out of space on image", self.name) self._pos = pos @@ -460,7 +460,7 @@ def readlines(self, sizehint): def seek(self, offset, whence = 0): if self.closed: - raise ValueError, "file is closed" + raise ValueError("file is closed") if whence == 1: base = self._pos @@ -473,7 +473,7 @@ def seek(self, offset, whence = 0): def tell(self): if self.closed: - raise ValueError, "file is closed" + raise ValueError("file is closed") return self._pos def __enter__(self): @@ -617,8 +617,7 @@ def __init__(self, f, ignore_ecc = False, params = None): s = f.read(0x154) if len(s) != 0x154 or not s.startswith(PS2MC_MAGIC): if (params == None): - raise corrupt, ("Not a PS2 memory card image", - f) + raise corrupt("Not a PS2 memory card image", f) self.f = f self.format(params) else: @@ -658,7 +657,7 @@ def __init__(self, f, ignore_ecc = False, params = None): root.close() if (dot[8] != "." or dotdot[8] != ".." or not mode_is_dir(dot[0]) or not mode_is_dir(dotdot[0])): - raise corrupt, "Root directory damaged." + raise corrupt("Root directory damaged.") self.fat_cursor = 0 self.curdir = (0, 0) @@ -699,7 +698,7 @@ def format(self, params): pages_per_erase_block, param_pages_per_card) = params if pages_per_erase_block < 1: - raise error, ("invalid pages per erase block (%d)" + raise error("invalid pages per erase block (%d)" % page_size) pages_per_card = round_down(param_pages_per_card, @@ -715,7 +714,7 @@ def format(self, params): if (page_size < PS2MC_DIRENT_LENGTH or pages_per_cluster < 1 or pages_per_cluster * page_size != cluster_size): - raise error, "invalid page size (%d)" % page_size + raise error("invalid page size (%d)" % page_size) good_block1 = erase_blocks_per_card - 1 good_block2 = erase_blocks_per_card - 2 @@ -737,7 +736,7 @@ def format(self, params): * clusters_per_erase_block - allocatable_cluster_offset) if allocatable_cluster_end < 1: - raise error, ("memory card image too small" + raise error("memory card image too small" " to be formatted") ifc_list = unpack_fat("\0\0\0\0" @@ -819,17 +818,17 @@ def read_page(self, n): f.seek(self.raw_page_size * n) page = f.read(self.page_size) if len(page) != self.page_size: - raise corrupt, ("attempted to read past EOF" + raise corrupt("attempted to read past EOF" " (page %05X)" % n, f) if self.ignore_ecc: return page spare = f.read(self.spare_size) if len(spare) != self.spare_size: - raise corrupt, ("attempted to read past EOF" + raise corrupt("attempted to read past EOF" " (page %05X)" % n, f) (status, page, spare) = ecc_check_page(page, spare) if status == ECC_CHECK_FAILED: - raise ecc_error, ("Unrecoverable ECC error (page %d)" + raise ecc_error("Unrecoverable ECC error (page %d)" % n) return page @@ -838,7 +837,7 @@ def write_page(self, n, buf): f.seek(self.raw_page_size * n) self.modified = True if len(buf) != self.page_size: - raise error, ("internal error: write_page:" + raise error("internal error: write_page:" " %d != %d" % (len(buf), self.page_size)) f.write(buf) if self.spare_size != 0: @@ -866,7 +865,7 @@ def write_cluster(self, n, buf): if self.spare_size == 0: self.f.seek(cluster_size * n) if len(buf) != cluster_size: - raise error, ("internal error: write_cluster:" + raise error("internal error: write_cluster:" " %d != %d" % (len(buf), cluster_size)) return self.f.write(buf) @@ -947,7 +946,7 @@ def read_fat_cluster(self, n): def read_fat(self, n): if n < 0 or n >= self.allocatable_cluster_end: - raise io_error, (EIO, + raise io_error(EIO, "FAT cluster index out of range" " (%d)" % n) offset = n % self.entries_per_cluster @@ -1162,7 +1161,7 @@ def create_dir_entry(self, parent_dirloc, name, mode): """Create a new directory entry in a directory.""" if name == "": - raise file_not_found, name + raise file_not_found(name) # print "@@@ create_dir_ent", parent_dirloc, name dir_ent = self._dirloc_to_ent(parent_dirloc) @@ -1226,16 +1225,16 @@ def delete_dirloc(self, dirloc, truncate, name): """Delete or truncate the file or directory given by dirloc.""" if dirloc == (0, 0): - raise io_error, (EACCES, + raise io_error(EACCES, "cannot remove root directory", name) if dirloc[1] in [0, 1]: - raise io_error, (EACCES, + raise io_error(EACCES, 'cannot remove "." or ".." entries', name) if dirloc in self.open_files: - raise io_error, (EBUSY, + raise io_error(EBUSY, "cannot remove open file", filename) epc = self.entries_per_cluster @@ -1351,13 +1350,13 @@ def open(self, filename, mode = "r"): (dirloc, ent, is_dir) = self.path_search(filename) # print "@@@ open", (dirloc, ent) if dirloc == None: - raise path_not_found, filename + raise path_not_found(filename) if is_dir: - raise io_error, (EISDIR, "not a regular file", + raise io_error(EISDIR, "not a regular file", filename) if ent[0] == 0: if mode[0] not in "wa": - raise file_not_found, filename + raise file_not_found(filename) name = ent[8] (dirloc, ent) = self.create_dir_entry(dirloc, name, DF_FILE | DF_RWX @@ -1372,19 +1371,19 @@ def open(self, filename, mode = "r"): def dir_open(self, filename, mode = "rb"): (dirloc, ent, is_dir) = self.path_search(filename) if dirloc == None: - raise path_not_found, filename + raise path_not_found(filename) if ent[0] == 0: - raise dir_not_found, filename + raise dir_not_found(filename) if not is_dir: - raise io_error, (ENOTDIR, "not a directory", filename) + raise io_error(ENOTDIR, "not a directory", filename) return self.directory(dirloc, ent[4], ent[2], mode, filename) def mkdir(self, filename): (dirloc, ent, is_dir) = self.path_search(filename) if dirloc == None: - raise path_not_found, filename + raise path_not_found(filename) if ent[0] != 0: - raise io_error, (EEXIST, "directory exists", filename) + raise io_error(EEXIST, "directory exists", filename) name = ent[8] self.create_dir_entry(dirloc, name, DF_DIR | DF_RWX | DF_0400) self.flush() @@ -1407,16 +1406,16 @@ def remove(self, filename): (dirloc, ent, is_dir) = self.path_search(filename) if dirloc == None: - raise path_not_found, filename + raise path_not_found(filename) if ent[0] == 0: - raise file_not_found, filename + raise file_not_found(filename) if is_dir: if ent[4] == 0: - raise io_error, (EACCES, + raise io_error(EACCES, "cannot remove" " root directory") if not self._is_empty(dirloc, ent, filename): - raise io_error, (ENOTEMPTY, + raise io_error(ENOTEMPTY, "directory not empty", filename) self.delete_dirloc(dirloc, False, filename) @@ -1425,11 +1424,11 @@ def remove(self, filename): def chdir(self, filename): (dirloc, ent, is_dir) = self.path_search(filename) if dirloc == None: - raise path_not_found, filename + raise path_not_found(filename) if ent[0] == 0: - raise dir_not_found, filename + raise dir_not_found(filename) if not is_dir: - raise io_error, (ENOTDIR, "not a directory", filename) + raise io_error(ENOTDIR, "not a directory", filename) self.curdir = dirloc def get_mode(self, filename): @@ -1448,9 +1447,9 @@ def get_dirent(self, filename): (dirloc, ent, is_dir) = self.path_search(filename) if dirloc == None: - raise path_not_found, filename + raise path_not_found(filename) if ent[0] == 0: - raise file_not_found, filename + raise file_not_found(filename) return ent def set_dirent(self, filename, new_ent): @@ -1461,9 +1460,9 @@ def set_dirent(self, filename, new_ent): (dirloc, ent, is_dir) = self.path_search(filename) if dirloc == None: - raise path_not_found, filename + raise path_not_found(filename) if ent[0] == 0: - raise file_not_found, filename + raise file_not_found(filename) dir = self._opendir_parent_dirloc(dirloc, "r+b") try: new_ent = list(new_ent) @@ -1485,23 +1484,23 @@ def is_ancestor(self, dirloc, olddirloc): def rename(self, oldpathname, newpathname): (olddirloc, oldent, is_dir) = self.path_search(oldpathname) if olddirloc == None: - raise path_not_found, oldpathname + raise path_not_found(oldpathname) if oldent[0] == 0: - raise file_not_found, oldpathname + raise file_not_found(oldpathname) if olddirloc == (0, 0): - raise io_error, (EINVAL, + raise io_error(EINVAL, "cannot rename root directory", oldpathname) if olddirloc in self.open_files: - raise io_error, (EBUSY, "cannot rename open file", + raise io_error(EBUSY, "cannot rename open file", newname) (newparentdirloc, newent, x) = self.path_search(newpathname) if newparentdirloc == None: - raise path_not_found, newpathname + raise path_not_found(newpathname) if newent[0] != 0: - raise io_error, (EEXIST, "file exists", newpathname) + raise io_error(EEXIST, "file exists", newpathname) newname = newent[8] oldparentdirloc = self._get_parent_dirloc(olddirloc) @@ -1516,7 +1515,7 @@ def rename(self, oldpathname, newpathname): return if is_dir and self.is_ancestor(newparentdirloc, olddirloc): - raise io_error, (EINVAL, "cannot move directory" + raise io_error(EINVAL, "cannot move directory" " beneath itself", oldpathname) @@ -1573,11 +1572,11 @@ def import_save_file(self, sf, ignore_existing, dirname = None): (root_dirloc, ent, is_dir) = self.path_search(dirname) if root_dirloc == None: - raise path_not_found, dirname + raise path_not_found(dirname) if ent[0] != 0: if ignore_existing: return False - raise io_error, (EEXIST, "directory exists", dirname) + raise io_error(EEXIST, "directory exists", dirname) name = ent[8] mode = DF_DIR | (dir_ent[0] & ~DF_FILE) @@ -1607,17 +1606,17 @@ def import_save_file(self, sf, ignore_existing, dirname = None): (ent, data) = sf.get_file(i) # print "@@@ remove", ent[8] self.remove(dirname + ent[8]) - except EnvironmentError, why: - # print "@@@ failed", why + except EnvironmentError as e: + # print "@@@ failed", e pass try: # print "@@@ remove dir", dirname self.remove(dirname) - except EnvironmentError, why: - # print "@@@ failed", why + except EnvironmentError as e: + # print "@@@ failed", e pass - raise type, what, where + raise Exception(type, what, where) finally: del where @@ -1644,13 +1643,13 @@ def import_save_file(self, sf, ignore_existing, dirname = None): def export_save_file(self, filename): (dir_dirloc, dirent, is_dir) = self.path_search(filename) if dir_dirloc == None: - raise path_not_found, filename + raise path_not_found(filename) if dirent[0] == 0: - raise dir_not_found, filename + raise dir_not_found(filename) if not is_dir: - raise io_error, (ENOTDIR, "not a directory", filename) + raise io_error(ENOTDIR, "not a directory", filename) if dir_dirloc == (0, 0): - raise io_error, (EACCES, "can't export root directory", + raise io_error(EACCES, "can't export root directory", filename) sf = ps2save.ps2_save_file() files = [] @@ -1711,13 +1710,13 @@ def rmdir(self, dirname): (dirloc, ent, is_dir) = self.path_search(dirname) if dirloc == None: - raise path_not_found, dirname + raise path_not_found(dirname) if ent[0] == 0: - raise dir_not_found, dirname + raise dir_not_found(dirname) if not is_dir: - raise io_error, (ENOTDIR, "not a directory", dirname) + raise io_error(ENOTDIR, "not a directory", dirname) if dirloc == (0, 0): - raise io_error, (EACCES, "can't delete root directory", + raise io_error(EACCES, "can't delete root directory", dirname) if dirname != "" and dirname[-1] != "/": @@ -1812,7 +1811,7 @@ def check(self): fat_len = int(str(self.allocatable_cluster_end)) if not isinstance(fat_len, int): - raise error, "Memory card image too big to check." + raise error("Memory card image too big to check.") fat = array.array('B', [0]) * fat_len diff --git a/ps2save.py b/ps2save.py index 465de20..41042c4 100755 --- a/ps2save.py +++ b/ps2save.py @@ -251,7 +251,7 @@ def _read_fixed(f, n): s = f.read(n) if len(s) != n: - raise eof, f + raise eof(f) return s def _read_long_string(f): @@ -314,7 +314,7 @@ def load_ems(self, f): or not mode_is_dir(dotent[0]) or not mode_is_dir(dotdotent[0]) or dirent[2] < 2): - raise corrupt, ("Not a EMS (.psu) save file.", f) + raise corrupt("Not a EMS (.psu) save file.", f) dirent[2] -= 2 self.set_directory(dirent) @@ -323,7 +323,7 @@ def load_ems(self, f): ent = unpack_dirent(_read_fixed(f, PS2MC_DIRENT_LENGTH)) if not mode_is_file(ent[0]): - raise subdir, f + raise subdir(f) flen = ent[2] self.set_file(i, ent, _read_fixed(f, flen)) _read_fixed(f, round_up(flen, cluster_size) - flen) @@ -348,7 +348,7 @@ def save_ems(self, f): if not mode_is_file(ent[0]): # print ent # print hex(ent[0]) - raise error, "Directory has a subdirectory." + raise error("Directory has a subdirectory.") f.write(data) f.write("\0" * (round_up(len(data), cluster_size) - len(data))) @@ -359,7 +359,7 @@ def _load_max_drive_2(self): self._compressed = None if lzari == None: - raise error, ("The lzari module is needed to " + raise error("The lzari module is needed to " " decompress MAX Drive saves.") s = lzari.decode(s, length, "decompressing " + self.dirent[8] + ": ") @@ -368,14 +368,14 @@ def _load_max_drive_2(self): off = 0 for i in range(dirlen): if len(s) - off < 36: - raise eof, f + raise eof(f) (l, name) = struct.unpack(" Date: Thu, 6 Jul 2023 01:01:11 +0200 Subject: [PATCH 02/15] Fixing prints Replacing python2 style print calls with python3 ones --- gui.py | 14 ++--- lzari.py | 28 +++++----- mymc.py | 34 ++++++------ ps2mc.py | 142 +++++++++++++++++++++++++-------------------------- ps2mc_ecc.py | 16 +++--- ps2save.py | 6 +-- 6 files changed, 120 insertions(+), 120 deletions(-) diff --git a/gui.py b/gui.py index 357462f..f930f9b 100755 --- a/gui.py +++ b/gui.py @@ -347,7 +347,7 @@ def __init__(self, parent, focus): r = mymcicon.init_icon_renderer(focus.GetHandle(), self.GetHandle()) if r == -1: - print "init_icon_renderer failed" + print("init_icon_renderer failed") self.failed = True return @@ -384,7 +384,7 @@ def load_icon(self, icon_sys, icon): r = mymcicon.load_icon(icon_sys, len(icon_sys), icon, len(icon)) if r != 0: - print "load_icon", r + print("load_icon", r) self.failed = True def _set_lighting(self, lighting, vertex_diffuse, alt_lighting, @@ -719,7 +719,7 @@ def evt_dirlist_item_focused(self, event): finally: f.close() except EnvironmentError as e: - print "icon failed to load", e + print("icon failed to load", e) self.icon_win.load_icon(None, None) return @@ -936,11 +936,11 @@ def run(filename = None): gc.collect() for o in gc.garbage: - print - print o + print() + print(o) if type(o) == ps2mc.ps2mc_file: for m in dir(o): - print m, getattr(o, m) + print(m, getattr(o, m)) # while True: @@ -954,7 +954,7 @@ def run(filename = None): # or m == "__dict__" # or m == "__weakref__"): # continue -# print m +# print(m) # setattr(o, m, None) # o = None # break diff --git a/lzari.py b/lzari.py index e1c7c7c..cf68b1b 100755 --- a/lzari.py +++ b/lzari.py @@ -56,7 +56,7 @@ MAX_SUFFIX_CHAIN = 50 # limit on how many identical suffixes to try to match #def debug(value, msg): -# print "@@@ %s %04x" % (msg, value) +# print("@@@ %s %04x" % (msg, value)) debug = lambda value, msg: None _tr_16 = string.maketrans("0123456789abcdef", @@ -437,11 +437,11 @@ def _add_suffix(self, pos, find): r = self.add_suffix_2(pos, find) start_pos = self.start_pos if find and r[0] != None: - print ("%4d %02x %4d %2d" + print("%4d %02x %4d %2d" % (pos - start_pos, ord(self.src[pos]), r[0] - start_pos, r[1])) else: - print ("%4d %02x" + print("%4d %02x" % (pos - start_pos, ord(self.src[pos]))) return r @@ -570,7 +570,7 @@ def encode(self, src, progress = None): #for k, v in sorted(self.suffix_table.items()): # count, head, table2, chars = v - # print hexlify(k), count, head, len(table2), chars + # print(hexlify(k), count, head, len(table2), chars) if progress: sys.stderr.write("%s100%%\n" % progress) @@ -652,7 +652,7 @@ def decode(src, out_length, progress = None): def encode(src, progress = None): (r, compressed, comp_len) = mylzari_encode(src, len(src), progress) - # print r, compressed.value, comp_len + # print(r, compressed.value, comp_len) if r == -1: raise MemoryError("out of memory during compression") if compressed.value == None: @@ -679,7 +679,7 @@ def main2(args): now = os.times() out.write(dest) out.close() - print "time:", now[0] - start[0], now[1] - start[1], now[4] - start[4] + print("time:", now[0] - start[0], now[1] - start[1], now[4] - start[4]) def _get_hotshot_lineinfo(filename): @@ -703,10 +703,10 @@ def _dump_hotshot_lineinfo(log): total_time = sum((time[1] for (loc, time) in a)) for (loc, [count, time]) in a: - print ("%8d %6.3f%% %8d %6.3f%%" + print("%8d %6.3f%% %8d %6.3f%%" % (time, time * 100.0 / total_time, count, count * 100.0 / total_count)), - print "%s:%d(%s)" % loc + print("%s:%d(%s)" % loc) def _dump_hotshot_lineinfo2(log): cur = None @@ -719,7 +719,7 @@ def _dump_hotshot_lineinfo2(log): if cur != filename: if cur != None and f != None: for line in f: - print line[:-1] + print(line[:-1]) f.close() try: f = file(filename, "r") @@ -727,17 +727,17 @@ def _dump_hotshot_lineinfo2(log): f = None cur = filename l = 0 - print "#", filename + print("#", filename) if f != None: while l < lineno: - print f.readline()[:-1] + print(f.readline()[:-1]) l += 1 - print ("# %8d %6.3f%% %8d %6.3f%%" + print("# %8d %6.3f%% %8d %6.3f%%" % (time, time * 100.0 / total_time, count, count * 100.0 / total_count)) if cur != None and f != None: for line in f: - print line[:-1] + print(line[:-1]) f.close() def main(args): @@ -747,7 +747,7 @@ def main(args): import profile pr = profile.Profile() for i in range(5): - print pr.calibrate(100000) + print(pr.calibrate(100000)) return elif args[1] == "p": import profile diff --git a/mymc.py b/mymc.py index 122139c..ee5d504 100755 --- a/mymc.py +++ b/mymc.py @@ -229,10 +229,10 @@ def do_import(cmd, mc, opts, args, opterr): dirname = opts.directory if dirname == None: dirname = sf.get_directory()[8] - print "Importing", filename, "to", dirname + print("Importing", filename, "to", dirname) if not mc.import_save_file(sf, opts.ignore_existing, opts.directory): - print (filename + ": already in memory card image," + print(filename + ": already in memory card image," " ignored.") #re_num = re.compile("[0-9]+") @@ -277,7 +277,7 @@ def do_export(cmd, mc, opts, args, opterr): f = file(filename, "wb") try: - print "Exporing", dirname, "to", filename + print("Exporing", dirname, "to", filename) if opts.type == "max": sf.save_max_drive(f) @@ -326,7 +326,7 @@ def do_setmode(cmd, mc, opts, args, opterr): ent = mc.get_dirent(arg) if value == None: ent[0] = (ent[0] & clear_mask) | set_mask - # print "new %04x" % ent[0] + # print("new %04x" % ent[0]) else: ent[0] = value mc.set_dirent(arg, ent) @@ -394,10 +394,10 @@ def do_dir(cmd, mc, opts, args, opterr): if type != None: protection = type - print "%-32s %s" % (ent[8], title[0]) - print ("%4dKB %-25s %s" + print("%-32s %s" % (ent[8], title[0])) + print("%4dKB %-25s %s" % (length / 1024, protection, title[1])) - print + print() finally: if f != None: f.close() @@ -412,18 +412,18 @@ def do_dir(cmd, mc, opts, args, opterr): else: free = "%d" % free - print free + " KB Free" + print(free + " KB Free") def do_df(cmd, mc, opts, args, opterr): if len(args) != 0: opterr("Incorrect number of arguments.") - print mc.f.name + ":", mc.get_free_space(), "bytes free." + print(mc.f.name + ":", mc.get_free_space(), "bytes free.") def do_check(cmd, mc, opts, args, opterr): if len(args) != 0: opterr("Incorrect number of arguments.") if mc.check(): - print "No errors found." + print("No errors found.") return 0 return 1 @@ -488,11 +488,11 @@ def do_frob(cmd, mc, opts, args, opterr): def _print_bin(base, s): for off in range(0, len(s), 16): - print "%04X" % (base + off), + print("%04X" % (base + off)) a = s[off : off + 16] for b in a: - print "%02X" % ord(b), - print "", a.translate(_trans) + print("%02X" % ord(b)) + print("", a.translate(_trans)) def _print_erase_block(mc, n): ppb = mc.pages_per_erase_block @@ -503,9 +503,9 @@ def _print_erase_block(mc, n): print def do_print_good_blocks(cmd, mc, opts, args, opterr): - print "good_block2:" + print("good_block2:") _print_erase_block(mc, mc.good_block2) - print "good_block1:" + print("good_block1:") _print_erase_block(mc, mc.good_block1) def do_ecc_check(cmd, mc, opts, args, opterr): @@ -513,7 +513,7 @@ def do_ecc_check(cmd, mc, opts, args, opterr): try: mc.read_page(i) except ps2mc.ecc_error: - print "bad: %05x" % i + print("bad: %05x" % i) opt = optparse.make_option @@ -788,7 +788,7 @@ def main(): if mc != None: mc.close() if f != None: - # print "f.close()" + # print("f.close()") f.close() except EnvironmentError as e: diff --git a/ps2mc.py b/ps2mc.py index 1f86ab0..647162e 100755 --- a/ps2mc.py +++ b/ps2mc.py @@ -135,10 +135,10 @@ def dump(self): lru_list = self._lru_list i = 0 while i != len(self._lru_list): - print "%d: %s, " % (i, str(lru_list[i][1])), - i = lru_list[i][3] - print - print self._index_map + print("%d: %s, " % (i, str(lru_list[i][1])), + i = lru_list[i][3]) + print() + print(self._index_map) def _move_to_front(self, i): lru_list = self._lru_list @@ -161,10 +161,10 @@ def add(self, key, value): ret = None if key in index_map: i = index_map[key] - # print "add hit ", key, i + # print("add hit ", key, i) elt = lru_list[i] else: - # print "add miss", key + # print("add miss", key) i = lru_list[-1][0] elt = lru_list[i] old_key = elt[1] @@ -181,9 +181,9 @@ def add(self, key, value): def get(self, key, default = None): i = self._index_map.get(key) if i == None: - # print "get miss", key + # print("get miss", key) return default - # print "get hit ", key, i + # print("get hit ", key, i) ret = self._lru_list[i][2] self._move_to_front(i) return ret @@ -207,15 +207,15 @@ def __getitem__(self, i): # not iterable offset = self.offset if i == offset: - # print "@@@ fat_chain[] cur:", i, self._cur + # print("@@@ fat_chain[] cur:", i, self._cur) return self._cur elif i == offset - 1: assert self._prev != None - # print "@@@ fat_chain[] prev:", i, self._prev + # print("@@@ fat_chain[] prev:", i, self._prev) return self._prev if i < offset: if i == 0: - # print "@@@ fat_chain[] first", i, self._first + # print("@@@ fat_chain[] first", i, self._first) return self._first offset = 0 prev = None @@ -223,7 +223,7 @@ def __getitem__(self, i): else: prev = self._prev cur = self._cur - # print "@@@ fat_chain[] distance", i - offset + # print("@@@ fat_chain[] distance", i - offset) while offset != i: next = self.lookup_fat(cur) if next == PS2MC_FAT_CHAIN_END: @@ -241,8 +241,8 @@ def __getitem__(self, i): self.offset = offset self._prev = prev self._cur = cur - # print "@@@ offset, prev, cur:", offset, prev, cur - # print "@@@ fat_chain[]", i, next + # print("@@@ offset, prev, cur:", offset, prev, cur) + # print("@@@ fat_chain[]", i, next) return next def __len__(self): @@ -262,7 +262,7 @@ class ps2mc_file(object): def __init__(self, mc, dirloc, first_cluster, length, mode, name = None): - # print "ps2mc_file.__init__", name, self + # print("ps2mc_file.__init__", name, self) self.mc = mc self.length = length self.first_cluster = first_cluster @@ -297,7 +297,7 @@ def read_file_cluster(self, n): if n == self.buffer_cluster: return self.buffer cluster = self._find_file_cluster(n) - # print "@@@ read_file_cluster", self.dirloc, n, cluster, repr(self.name) + # print("@@@ read_file_cluster", self.dirloc, n, cluster, repr(self.name)) if cluster == PS2MC_FAT_CHAIN_END: return None self.buffer = self.mc.read_allocatable_cluster(cluster) @@ -307,18 +307,18 @@ def read_file_cluster(self, n): def _extend_file(self, n): mc = self.mc cluster = mc.allocate_cluster() - # print "@@@ extending file", n, cluster + # print("@@@ extending file", n, cluster) if cluster == None: return None if n == 0: self.first_cluster = cluster self.fat_chain = None - # print "@@@ linking", self.dirloc, "->", cluster + # print("@@@ linking", self.dirloc, "->", cluster) mc.update_dirent(self.dirloc, self, cluster, None, False) else: prev = self.fat_chain[n - 1] - # print "@@@ linking", prev, "->", cluster + # print("@@@ linking", prev, "->", cluster) mc.set_fat(prev, cluster | PS2MC_FAT_ALLOCATED_BIT) return cluster @@ -408,7 +408,7 @@ def write(self, out, _set_modified = True): self.name) size = len(out) - # print "@@@ write", pos, size + # print("@@@ write", pos, size) i = 0 while size > 0: cluster = pos / cluster_size @@ -428,7 +428,7 @@ def write(self, out, _set_modified = True): "out of space on image", self.name) self._pos = pos - # print "@@@ pos", pos + # print("@@@ pos", pos) new_length = None if pos > self.length: new_length = self.length = pos @@ -439,7 +439,7 @@ def write(self, out, _set_modified = True): size -= l def close(self): - # print "ps2mc_file.close", self.name, self + # print("ps2mc_file.close", self.name, self) if self.mc != None: self.mc.notify_closed(self.dirloc, self) self.mc = None @@ -484,7 +484,7 @@ def __exit__(self, a, b, c): return # def __del__(self): - # # print "ps2mc_file.__del__", self + # # print("ps2mc_file.__del__", self) # if self.mc != None: # self.mc.notify_closed(self.dirloc, self) # self.mc = None @@ -507,13 +507,13 @@ def __iter__(self): return self def write_raw_ent(self, index, ent, set_modified): - # print "@@@ write_raw_ent", index + # print("@@@ write_raw_ent", index) self.seek(index) self.f.write(pack_dirent(ent), _set_modified = set_modified) def next(self): - # print "@@@ next", self.tell(), self.f.name + # print("@@@ next", self.tell(), self.f.name) dirent = self.f.read(PS2MC_DIRENT_LENGTH) if dirent == "": if 0 == self._iter_end: @@ -534,7 +534,7 @@ def __len__(self): return self.f.length / PS2MC_DIRENT_LENGTH def __getitem__(self, index): - # print "@@@ getitem", index, self.f.name + # print("@@@ getitem", index, self.f.name) self.seek(index) dirent = self.f.read(PS2MC_DIRENT_LENGTH) if len(dirent) != PS2MC_DIRENT_LENGTH: @@ -556,12 +556,12 @@ def __setitem__(self, index, new_ent): self.write_raw_ent(index, ent, False) def close(self): - # print "ps2mc_directory.close", self + # print("ps2mc_directory.close", self) self.f.close() self.f = None def __del__(self): - # print "ps2mc_directory.__del__", self + # print("ps2mc_directory.__del__", self) if self.f != None: self.f.close() self.f = None @@ -813,7 +813,7 @@ def format(self, params): self.flush() def read_page(self, n): - # print "@@@ page", n + # print("@@@ page", n) f = self.f f.seek(self.raw_page_size * n) page = f.read(self.page_size) @@ -886,9 +886,9 @@ def _add_fat_cluster_to_cache(self, n, fat, dirty): def _read_fat_cluster(self, n): v = self.fat_cache.get(n) if v != None: - # print "@@@ fat hit", n + # print("@@@ fat hit", n) return v[0] - # print "@@@ fat miss", n + # print("@@@ fat miss", n) fat = unpack_fat(self.read_cluster(n)) self._add_fat_cluster_to_cache(n, fat, False) return fat @@ -916,9 +916,9 @@ def _add_alloc_cluster_to_cache(self, n, buf, dirty): def read_allocatable_cluster(self, n): a = self.alloc_cluster_cache.get(n) if a != None: - # print "@@@ cache hit", n + # print("@@@ cache hit", n) return a[0] - # print "@@@ cache miss", n + # print("@@@ cache miss", n) buf = self.read_cluster(n + self.allocatable_cluster_offset) self._add_alloc_cluster_to_cache(n, buf, False) return buf @@ -982,7 +982,7 @@ def allocate_cluster(self): fat[offset] = PS2MC_FAT_CHAIN_END self._write_fat_cluster(cluster, fat) ret = self.fat_cursor * epc + offset - # print "@@@ allocated", ret + # print("@@@ allocated", ret) return ret self.fat_cursor += 1 return None @@ -1010,7 +1010,7 @@ def directory(self, dirloc, first_cluster, length, def _directory(self, dirloc, first_cluster, length, mode = None, name = None): - # print "@@@ _directory", dirloc, first_cluster, length + # print("@@@ _directory", dirloc, first_cluster, length) if first_cluster != 0: return self.directory(dirloc, first_cluster, length, mode, name) @@ -1060,8 +1060,8 @@ def _opendir_parent_dirloc(self, dirloc, mode = "rb"): mode) def update_dirent_all(self, dirloc, thisf, new_ent): - # print "@@@ update_dirent", dirloc - # print "@@@ new_ent", new_ent + # print("@@@ update_dirent", dirloc) + # print("@@@ new_ent", new_ent) opened = self.open_files.get(dirloc, None) if opened == None: files = [] @@ -1074,7 +1074,7 @@ def update_dirent_all(self, dirloc, thisf, new_ent): opened[0] = dir ent = dir[dirloc[1]] - # print "@@@ old_ent", ent + # print("@@@ old_ent", ent) is_dir = ent[0] & DF_DIR @@ -1082,7 +1082,7 @@ def update_dirent_all(self, dirloc, thisf, new_ent): new_ent = list(new_ent) new_ent[2] /= PS2MC_DIRENT_LENGTH - # print "len: ", ent[2], new_ent[2] + # print("len: ", ent[2], new_ent[2]) modified = changed = notify = False for i in range(len(ent)): @@ -1135,7 +1135,7 @@ def notify_closed(self, dirloc, thisf): dir, files = a files.discard(thisf) if len(files) == 0: - # print "@@@ notify_closed", dir + # print("@@@ notify_closed", dir) if dir != None: dir.close() del self.open_files[dirloc] @@ -1163,12 +1163,12 @@ def create_dir_entry(self, parent_dirloc, name, mode): if name == "": raise file_not_found(name) - # print "@@@ create_dir_ent", parent_dirloc, name + # print("@@@ create_dir_ent", parent_dirloc, name) dir_ent = self._dirloc_to_ent(parent_dirloc) dir = self._directory(parent_dirloc, dir_ent[4], dir_ent[2], "r+b") l = len(dir) - # print "@@@ len", l + # print("@@@ len", l) assert l >= 2 for i in range(l): ent = dir[i] @@ -1178,7 +1178,7 @@ def create_dir_entry(self, parent_dirloc, name, mode): i = l dirloc = (dir_ent[4], i) - # print "@@@ dirloc", dirloc + # print("@@@ dirloc", dirloc) now = tod_now() if mode & DF_DIR: mode &= ~DF_FILE @@ -1202,7 +1202,7 @@ def create_dir_entry(self, parent_dirloc, name, mode): dir.close() if mode & DF_FILE: - # print "@@@ ret", dirloc, ent + # print("@@@ ret", dirloc, ent) return (dirloc, ent) dirent = pack_dirent((DF_RWX | DF_0400 | DF_DIR | DF_EXISTS, @@ -1218,7 +1218,7 @@ def create_dir_entry(self, parent_dirloc, name, mode): now, 0, ".."), False) dir.close() ent[2] = 2 - # print "@@@ ret", dirloc, ent + # print("@@@ ret", dirloc, ent) return (dirloc, ent) def delete_dirloc(self, dirloc, truncate, name): @@ -1276,7 +1276,7 @@ def path_search(self, pathname): is a boolean value that's true if the pathname refers a directory.""" - # print "@@@ path_search", repr(pathname) + # print("@@@ path_search", repr(pathname)) if pathname == "": return (None, None, False) @@ -1301,7 +1301,7 @@ def path_search(self, pathname): name = tmpname) for s in components: - # print "@@@", dirloc, repr(s), dir == None, ent + # print("@@@", dirloc, repr(s), dir == None, ent) if dir == None: # tried to traverse a file or a @@ -1348,7 +1348,7 @@ def open(self, filename, mode = "r"): """Open a file, returning a new file-like object for it.""" (dirloc, ent, is_dir) = self.path_search(filename) - # print "@@@ open", (dirloc, ent) + # print("@@@ open", (dirloc, ent)) if dirloc == None: raise path_not_found(filename) if is_dir: @@ -1591,7 +1591,7 @@ def import_save_file(self, sf, ignore_existing, dirname = None): (dirloc, ent) \ = self.create_dir_entry(dir_dirloc, ent[8], mode) - # print "@@@ file", dirloc, ent[4], ent[2] + # print("@@@ file", dirloc, ent[4], ent[2]) f = self.file(dirloc, ent[4], ent[2], "wb", dirname + ent[8]) try: @@ -1604,17 +1604,17 @@ def import_save_file(self, sf, ignore_existing, dirname = None): try: for i in range(dir_ent[2]): (ent, data) = sf.get_file(i) - # print "@@@ remove", ent[8] + # print("@@@ remove", ent[8]) self.remove(dirname + ent[8]) except EnvironmentError as e: - # print "@@@ failed", e + # print("@@@ failed", e) pass try: - # print "@@@ remove dir", dirname + # print("@@@ remove dir", dirname) self.remove(dirname) except EnvironmentError as e: - # print "@@@ failed", e + # print("@@@ failed", e) pass raise Exception(type, what, where) finally: @@ -1660,7 +1660,7 @@ def export_save_file(self, filename): for i in range(2, dirent[2]): ent = dir[i] if not mode_is_file(ent[0]): - print ("warning: %s/%s is not a file," + print("warning: %s/%s is not a file," " ingored." % (dirent[8], ent[8])) continue @@ -1700,7 +1700,7 @@ def _remove_dir(self, dirloc, ent, dirname): self._remove_dir((first_cluster, i), ent, dirname + ent[8] + "/") else: - # print "deleting", dirname + ent[8] + # print("deleting", dirname + ent[8]) self.delete_dirloc((first_cluster, i), False, dirname + ent[8]) self.delete_dirloc(dirloc, False, dirname) @@ -1745,7 +1745,7 @@ def _check_file(self, fat, first_cluster, length): if fat[cluster]: return "cross linked chain" i += 1 - # print cluster, + # print(cluster) fat[cluster] = 1 next = self.lookup_fat(cluster) if next == PS2MC_FAT_CHAIN_END: @@ -1764,7 +1764,7 @@ def _check_dir(self, fat, dirloc, dirname, ent): why = self._check_file(fat, ent[4], ent[2] * PS2MC_DIRENT_LENGTH) if why != None: - print "bad directory:", dirname + ":", why + print("bad directory:", dirname + ":", why) return False ret = True first_cluster = ent[4] @@ -1773,14 +1773,14 @@ def _check_dir(self, fat, dirloc, dirname, ent): "rb", dirname) dot_ent = dir[0] if dot_ent[8] != ".": - print "bad directory:", dirname + ': missing "." entry' + print("bad directory:", dirname + ': missing "." entry') ret = False if (dot_ent[4], dot_ent[5]) != dirloc: - print "bad directory:", dirname + ': bad "." entry' + print("bad directory:", dirname + ': bad "." entry') ret = False if dir[1][8] != "..": - print "bad directory:", (dirname - + ': missing ".." entry') + print("bad directory:", (dirname + + ': missing ".." entry')) ret = False for i in xrange(2, length): ent = dir[i] @@ -1795,8 +1795,8 @@ def _check_dir(self, fat, dirloc, dirname, ent): else: why = self._check_file(fat, ent[4], ent[2]) if why != None: - print "bad file:", (dirname + ent[8] - + ":"), why + print("bad file:", (dirname + ent[8] + + ":"), why) ret = False dir.close() @@ -1823,11 +1823,11 @@ def check(self): for i in xrange(self.allocatable_cluster_end): a = self.lookup_fat(i) if (a & PS2MC_FAT_ALLOCATED_BIT) and not fat[i]: - print i, + print(i,) lost_clusters += 1 if lost_clusters > 0: - print - print "found", lost_clusters, "lost clusters" + print() + print("found", lost_clusters, "lost clusters") ret = False return ret @@ -1895,7 +1895,7 @@ def glob(self, pattern): ret = self._globdir(dirname, components, isdir) else: ret = self._glob(dirname, components, isdir) - # print pattern, "->", ret + # print(pattern, "->", ret) return ret def get_icon_sys(self, dirname): @@ -1945,14 +1945,14 @@ def close(self): access the raw image. After this method has been called on a ps2mc object, it can no longer be used.""" - # print "ps2mc.close" + # print("ps2mc.close") try: f = self.f if f == None or getattr(f, "closed", False): - # print "closed" + # print("closed") return open_files = self.open_files - # print "open_files", open_files + # print("open_files", open_files) if open_files != None: # this is complicated by the fact as # files are closed they will remove @@ -1976,7 +1976,7 @@ def close(self): self.rootdir = None def __del__(self): - # print "ps2mc.__del__" + # print("ps2mc.__del__") try: self.close() except: diff --git a/ps2mc_ecc.py b/ps2mc_ecc.py index 396e4be..247dab7 100755 --- a/ps2mc_ecc.py +++ b/ps2mc_ecc.py @@ -88,10 +88,10 @@ def _ecc_check(s, ecc): if computed == ecc: return ECC_CHECK_OK - #print + #print() #_print_bin(0, s.tostring()) - #print "computed %02x %02x %02x" % tuple(computed) - #print "actual %02x %02x %02x" % tuple(ecc) + #print("computed %02x %02x %02x" % tuple(computed)) + #print("actual %02x %02x %02x" % tuple(ecc)) # ECC mismatch @@ -101,17 +101,17 @@ def _ecc_check(s, ecc): lp_comp = lp0_diff ^ lp1_diff cp_comp = (cp_diff >> 4) ^ (cp_diff & 0x07) - #print "%02x %02x %02x %02x %02x" % (cp_diff, lp0_diff, lp1_diff, - # lp_comp, cp_comp) + #print("%02x %02x %02x %02x %02x" % (cp_diff, lp0_diff, lp1_diff, + # lp_comp, cp_comp)) if lp_comp == 0x7F and cp_comp == 0x07: - print "corrected 1" + print("corrected 1") # correctable 1 bit error in data s[lp1_diff] ^= 1 << (cp_diff >> 4) return ECC_CHECK_CORRECTED if ((cp_diff == 0 and lp0_diff == 0 and lp1_diff == 0) or _popcount(lp_comp) + _popcount(cp_comp) == 1): - print "corrected 2" + print("corrected 2") # correctable 1 bit error in ECC # (and/or one of the unused bits was set) ecc[0] = computed[0] @@ -171,7 +171,7 @@ def ecc_calculate(s): def ecc_check(s, ecc): cs = ctypes.c_ubyte.from_address(s.buffer_info()[0]) - # print "%08X" % s.buffer_info()[0] + # print("%08X" % s.buffer_info()[0]) aecc = array.array('B', ecc) cecc = ctypes.c_ubyte.from_address(aecc.buffer_info()[0]) ret = mymcsup.ecc_check(cs, len(s), cecc) diff --git a/ps2save.py b/ps2save.py index 41042c4..01fc935 100755 --- a/ps2save.py +++ b/ps2save.py @@ -346,8 +346,8 @@ def save_ems(self, f): (ent, data) = self.get_file(i) f.write(pack_dirent(ent)) if not mode_is_file(ent[0]): - # print ent - # print hex(ent[0]) + # print(ent) + # print(hex(ent[0])) raise error("Directory has a subdirectory.") f.write(data) f.write("\0" * (round_up(len(data), cluster_size) @@ -371,7 +371,7 @@ def _load_max_drive_2(self): raise eof(f) (l, name) = struct.unpack(" Date: Thu, 6 Jul 2023 01:58:46 +0200 Subject: [PATCH 03/15] Fixing incorrect indentation Apparently there was mixed spaces/tabs in these lines --- gui.py | 10 +++++----- lzari.py | 14 +++++++------- ps2save.py | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/gui.py b/gui.py index f930f9b..13d2af6 100755 --- a/gui.py +++ b/gui.py @@ -934,11 +934,11 @@ def run(filename = None): run("test.ps2") - gc.collect() - for o in gc.garbage: - print() - print(o) - if type(o) == ps2mc.ps2mc_file: + gc.collect() + for o in gc.garbage: + print() + print(o) + if type(o) == ps2mc.ps2mc_file: for m in dir(o): print(m, getattr(o, m)) diff --git a/lzari.py b/lzari.py index cf68b1b..d243ac7 100755 --- a/lzari.py +++ b/lzari.py @@ -151,7 +151,7 @@ def init(self, decode): def search(self, table, x): c = 1 - s = len(table) - 1 + s = len(table) - 1 while True: a = (s + c) / 2 if table[a] <= x: @@ -179,11 +179,11 @@ def update_model_decode(self, symbol): freq = sym_freq[symbol] new_symbol = symbol while self.sym_freq[new_symbol - 1] == freq: - new_symbol -= 1 + new_symbol -= 1 # new_symbol = sym_freq.index(freq) if new_symbol != symbol: symbol_to_char = self.symbol_to_char - swap_char = symbol_to_char[new_symbol] + swap_char = symbol_to_char[new_symbol] char = symbol_to_char[symbol] symbol_to_char[new_symbol] = char symbol_to_char[symbol] = swap_char @@ -195,7 +195,7 @@ def update_model_encode(self, symbol): sym_freq = self.sym_freq sym_cum = self.sym_cum - if sym_cum[0] >= MAX_CUM: + if sym_cum[0] >= MAX_CUM: c = 0 for i in range(MAX_CHAR, 0, -1): sym_cum[i] = c @@ -206,10 +206,10 @@ def update_model_encode(self, symbol): freq = sym_freq[symbol] new_symbol = symbol while sym_freq[new_symbol - 1] == freq: - new_symbol -= 1 + new_symbol -= 1 if new_symbol != symbol: debug(new_symbol, "a") - swap_char = self.symbol_to_char[new_symbol] + swap_char = self.symbol_to_char[new_symbol] char = self.symbol_to_char[symbol] self.symbol_to_char[new_symbol] = char self.symbol_to_char[symbol] = swap_char @@ -272,7 +272,7 @@ def decode_position(self): or self.high > QUADRANT3): if self.high > QUADRANT2: return pos - else: + else: self.low -= QUADRANT1 self.code -= QUADRANT1 self.high -= QUADRANT1 diff --git a/ps2save.py b/ps2save.py index 01fc935..7314bed 100755 --- a/ps2save.py +++ b/ps2save.py @@ -620,7 +620,7 @@ def make_longname(dirname, sf): crc = binascii.crc32("") for (ent, data) in sf: crc = binascii.crc32(data, crc) - if len(dirname) >= 12 and (dirname[0:2] in ("BA", "BJ", "BE", "BK")): + if len(dirname) >= 12 and (dirname[0:2] in ("BA", "BJ", "BE", "BK")): if dirname[2:6] == "DATA": title = "" else: From 667c4c40db6c9405d47dd852d94cde34bb652eeb Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Thu, 6 Jul 2023 02:01:24 +0200 Subject: [PATCH 04/15] Fixing `maketrans` calls Python3 uses str as the type, not string. --- lzari.py | 12 ++++++------ mymc.py | 2 +- ps2save.py | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lzari.py b/lzari.py index d243ac7..fbddb80 100755 --- a/lzari.py +++ b/lzari.py @@ -59,15 +59,15 @@ # print("@@@ %s %04x" % (msg, value)) debug = lambda value, msg: None -_tr_16 = string.maketrans("0123456789abcdef", +_tr_16 = str.maketrans("0123456789abcdef", "\x00\x01\x02\x03" "\x10\x11\x12\x13" "\x20\x21\x22\x23" "\x30\x31\x32\x33") -_tr_4 = string.maketrans("0123", +_tr_4 = str.maketrans("0123", "\x00\x01" "\x10\x11") -_tr_2 = string.maketrans("01", "\x00\x01") +_tr_2 = str.maketrans("01", "\x00\x01") def string_to_bit_array(s): """Convert a string to an array containing a sequence of bits.""" @@ -77,11 +77,11 @@ def string_to_bit_array(s): a = array.array('B', s) return a -_tr_rev_2 = string.maketrans("\x00\x01", "01") -_tr_rev_4 = string.maketrans("\x00\x01" +_tr_rev_2 = str.maketrans("\x00\x01", "01") +_tr_rev_4 = str.maketrans("\x00\x01" "\x10\x11", "0123") -_tr_rev_16 = string.maketrans("\x00\x01\x02\x03" +_tr_rev_16 = str.maketrans("\x00\x01\x02\x03" "\x10\x11\x12\x13" "\x20\x21\x22\x23" "\x30\x31\x32\x33", diff --git a/mymc.py b/mymc.py index ee5d504..669644b 100755 --- a/mymc.py +++ b/mymc.py @@ -484,7 +484,7 @@ def do_create_pad(cmd, mc, opts, args, opterr): def do_frob(cmd, mc, opts, args, opterr): mc.write_superblock() -_trans = string.maketrans("".join(map(chr, range(32))), " " * 32) +_trans = str.maketrans("".join(map(chr, range(32))), " " * 32) def _print_bin(base, s): for off in range(0, len(s), 16): diff --git a/ps2save.py b/ps2save.py index 7314bed..02af387 100755 --- a/ps2save.py +++ b/ps2save.py @@ -601,8 +601,8 @@ def detect_file_type(f): _bad_filename_chars2 = _bad_filename_chars + "?*'&|:[<>] \\\"" _bad_filename_repl2 = _bad_filename_repl + "______(())___" -_filename_trans = string.maketrans(_bad_filename_chars, _bad_filename_repl); -_filename_trans2 = string.maketrans(_bad_filename_chars2, _bad_filename_repl2); +_filename_trans = str.maketrans(_bad_filename_chars, _bad_filename_repl); +_filename_trans2 = str.maketrans(_bad_filename_chars2, _bad_filename_repl2); def fix_filename(filename): """Replace illegal or problematic characters from a filename.""" From cdf6cc5e23802252781b21854c06f144c824f038 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Thu, 6 Jul 2023 02:05:48 +0200 Subject: [PATCH 05/15] Remove unnecessary `decode` call --- mymc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mymc.py b/mymc.py index 669644b..8763b01 100755 --- a/mymc.py +++ b/mymc.py @@ -713,7 +713,7 @@ def format_description(self, description): return "\n".join(lines) + "\n" def main(): - prog = sys.argv[0].decode(sys.getdefaultencoding(), "replace") + prog = sys.argv[0] usage = "usage: %prog [-ih] memcard.ps2 command [...]" description = ("Manipulate PS2 memory card images.\n\n" "Supported commands: ") From c690e3fe49b24a74cd14480be095c50562a8d02f Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Thu, 6 Jul 2023 02:10:21 +0200 Subject: [PATCH 06/15] Replacing deprecated cStringIO Python3 uses StringIO from io package directly --- gui.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gui.py b/gui.py index 13d2af6..d581603 100755 --- a/gui.py +++ b/gui.py @@ -12,7 +12,7 @@ import os import sys import struct -import cStringIO +from io import StringIO import time from functools import partial @@ -101,7 +101,7 @@ def single_title(title): def _get_icon_resource_as_images(name): ico = guires.resources[name] images = [] - f = cStringIO.StringIO(ico) + f = StringIO(ico) count = struct.unpack(" Date: Thu, 6 Jul 2023 02:22:55 +0200 Subject: [PATCH 07/15] Fixing resource loading When decoding base64, python3 works with bytes, so we need in fact BytesIO, instead of StringIO --- gui.py | 4 +- guires.py | 368 +++++++++++++++++++++++++++--------------------------- 2 files changed, 187 insertions(+), 185 deletions(-) diff --git a/gui.py b/gui.py index d581603..e083d11 100755 --- a/gui.py +++ b/gui.py @@ -12,7 +12,7 @@ import os import sys import struct -from io import StringIO +from io import BytesIO import time from functools import partial @@ -101,7 +101,7 @@ def single_title(title): def _get_icon_resource_as_images(name): ico = guires.resources[name] images = [] - f = StringIO(ico) + f = BytesIO(ico) count = struct.unpack(" Date: Thu, 6 Jul 2023 02:27:05 +0200 Subject: [PATCH 08/15] Correct file open function in python3 --- gui.py | 10 +++++----- lzari.py | 6 +++--- mymc.py | 14 +++++++------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/gui.py b/gui.py index e083d11..88f39ad 100755 --- a/gui.py +++ b/gui.py @@ -674,12 +674,12 @@ def open_mc(self, filename): f = None try: - f = file(filename, "r+b") + f = open(filename, "r+b") mc = ps2mc.ps2mc(f) except EnvironmentError as e: if f != None: f.close() - self.mc_error(value, filename) + self.mc_error(e, filename) self.SetTitle(self.title) self.refresh() return @@ -778,7 +778,7 @@ def evt_cmd_export(self, event): if fn == "": return try: - f = file(fn, "wb") + f = open(fn, "wb") try: if fn.endswith(".max"): sf.save_max_drive(f) @@ -804,7 +804,7 @@ def evt_cmd_export(self, event): for (dirname, sf, longname) in sfiles: fn = os.path.join(dir, longname) + ".psu" try: - f = file(fn, "wb") + f = open(fn, "wb") sf.save_ems(f) f.close() count += 1 @@ -819,7 +819,7 @@ def evt_cmd_export(self, event): def _do_import(self, fn): sf = ps2save.ps2_save_file() - f = file(fn, "rb") + f = open(fn, "rb") try: ft = ps2save.detect_file_type(f) f.seek(0) diff --git a/lzari.py b/lzari.py index fbddb80..9c12516 100755 --- a/lzari.py +++ b/lzari.py @@ -665,9 +665,9 @@ def main2(args): import struct import os - src = file(args[2], "rb").read() + src = open(args[2], "rb").read() lzari = lzari_codec() - out = file(args[3], "wb") + out = open(args[3], "wb") start = os.times() if args[1] == "c": dest = lzari.encode(src) @@ -722,7 +722,7 @@ def _dump_hotshot_lineinfo2(log): print(line[:-1]) f.close() try: - f = file(filename, "r") + f = open(filename, "r") except OSError: f = None cur = filename diff --git a/mymc.py b/mymc.py index 8763b01..4d5c119 100755 --- a/mymc.py +++ b/mymc.py @@ -157,7 +157,7 @@ def do_extract(cmd, mc, opts, args, opterr): if opts.use_stdout: opterr("The -o and -p options are mutually exclusive.") dont_close_out = True - out = file(opts.output, "wb") + out = open(opts.output, "wb") elif opts.use_stdout: out = sys.stdout @@ -169,7 +169,7 @@ def do_extract(cmd, mc, opts, args, opterr): _copy(out, f) continue a = filename.split("/") - o = file(a[-1], "wb") + o = open(a[-1], "wb") try: _copy(o, f) finally: @@ -205,7 +205,7 @@ def do_import(cmd, mc, opts, args, opterr): for filename in args: sf = ps2save.ps2_save_file() - f = file(filename, "rb") + f = open(filename, "rb") try: ftype = ps2save.detect_file_type(f) f.seek(0) @@ -275,7 +275,7 @@ def do_export(cmd, mc, opts, args, opterr): continue raise io_error(EEXIST, "File exists", filename) - f = file(filename, "wb") + f = open(filename, "wb") try: print("Exporing", dirname, "to", filename) @@ -443,13 +443,13 @@ def do_format(cmd, mcname, opts, args, opterr): if not opts.overwrite_existing: exists = True try: - file(mcname, "rb").close() + open(mcname, "rb").close() except EnvironmentError: exists = False if exists: raise io_error(EEXIST, "file exists", mcname) - f = file(mcname, "w+b") + f = open(mcname, "w+b") try: ps2mc.ps2mc(f, True, params).close() finally: @@ -780,7 +780,7 @@ def main(): ret = fn(cmd, mcname, subopts, subargs, subopt_parser.error) else: - f = file(mcname, mode) + f = open(mcname, mode) mc = ps2mc.ps2mc(f, opts.ignore_ecc) ret = fn(cmd, mc, subopts, subargs, subopt_parser.error) From 4be994f4b8f64ebac734c6728db4550a6169d5f7 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Thu, 6 Jul 2023 19:18:49 +0200 Subject: [PATCH 09/15] Fixing integer divisions Python3 automatically converts these divisions to floats, which causes incorrect typing issues as well as rounding/off-by-one issues. These need to be explicitly converted to integer divisions --- ps2mc.py | 40 ++++++++++++++++++++-------------------- round.py | 6 +++--- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/ps2mc.py b/ps2mc.py index 647162e..784ef98 100755 --- a/ps2mc.py +++ b/ps2mc.py @@ -379,9 +379,9 @@ def read(self, size = None, eol = None): size = max(min(self.length - pos, size), 0) ret = "" while size > 0: - off = pos % cluster_size + off = int(pos % cluster_size) l = min(cluster_size - off, size) - buf = self.read_file_cluster(pos / cluster_size) + buf = self.read_file_cluster(int(pos / cluster_size)) if buf == None: break if eol != None: @@ -411,8 +411,8 @@ def write(self, out, _set_modified = True): # print("@@@ write", pos, size) i = 0 while size > 0: - cluster = pos / cluster_size - off = pos % cluster_size + cluster = int(pos / cluster_size) + off = int(pos % cluster_size) l = min(cluster_size - off, size) s = out[i : i + l] pos += l @@ -499,7 +499,7 @@ def __init__(self, mc, dirloc, first_cluster, length, length * PS2MC_DIRENT_LENGTH, mode, name) def __iter__(self): - start = self.tell() + start = int(self.tell()) if start != 0: start -= 1 self.seek(start) @@ -528,10 +528,10 @@ def seek(self, offset, whence = 0): self.f.seek(offset * PS2MC_DIRENT_LENGTH, whence) def tell(self): - return self.f.tell() / PS2MC_DIRENT_LENGTH + return int(self.f.tell() / PS2MC_DIRENT_LENGTH) def __len__(self): - return self.f.length / PS2MC_DIRENT_LENGTH + return int(self.f.length / PS2MC_DIRENT_LENGTH) def __getitem__(self, index): # print("@@@ getitem", index, self.f.name) @@ -596,7 +596,7 @@ def _calculate_derived(self): self.spare_size = div_round_up(self.page_size, 128) * 4 self.raw_page_size = self.page_size + self.spare_size self.cluster_size = self.page_size * self.pages_per_cluster - self.entries_per_cluster = (self.page_size + self.entries_per_cluster = int(self.page_size * self.pages_per_cluster / 4) limit = (min(self.good_block2, self.good_block1) @@ -704,12 +704,12 @@ def format(self, params): pages_per_card = round_down(param_pages_per_card, pages_per_erase_block) cluster_size = PS2MC_CLUSTER_SIZE - pages_per_cluster = cluster_size / page_size - clusters_per_erase_block = (pages_per_erase_block + pages_per_cluster = int(cluster_size / page_size) + clusters_per_erase_block = int(pages_per_erase_block / pages_per_cluster) - erase_blocks_per_card = pages_per_card / pages_per_erase_block - clusters_per_card = pages_per_card / pages_per_cluster - epc = cluster_size / 4 + erase_blocks_per_card = int(pages_per_card / pages_per_erase_block) + clusters_per_card = int(pages_per_card / pages_per_cluster) + epc = int(cluster_size / 4) if (page_size < PS2MC_DIRENT_LENGTH or pages_per_cluster < 1 @@ -937,8 +937,8 @@ def flush_alloc_cluster_cache(self): a[1] = False def read_fat_cluster(self, n): - indirect_offset = n % self.entries_per_cluster - dbl_offset = n / self.entries_per_cluster + indirect_offset = int(n % self.entries_per_cluster) + dbl_offset = int(n / self.entries_per_cluster) indirect_cluster = self.indirect_fat_cluster_list[dbl_offset] indirect_fat = self._read_fat_cluster(indirect_cluster) cluster = indirect_fat[indirect_offset] @@ -949,8 +949,8 @@ def read_fat(self, n): raise io_error(EIO, "FAT cluster index out of range" " (%d)" % n) - offset = n % self.entries_per_cluster - fat_cluster = n / self.entries_per_cluster + offset = int(n % self.entries_per_cluster) + fat_cluster = int(n / self.entries_per_cluster) (fat, cluster) = self.read_fat_cluster(fat_cluster) return (fat, offset, cluster) @@ -968,7 +968,7 @@ def allocate_cluster(self): allocatable_cluster_limit = self.allocatable_cluster_limit end = div_round_up(allocatable_cluster_limit, epc) - remainder = allocatable_cluster_limit % epc + remainder = int(allocatable_cluster_limit % epc) while self.fat_cursor < end: (fat, cluster) = self.read_fat_cluster(self.fat_cursor) @@ -1080,7 +1080,7 @@ def update_dirent_all(self, dirloc, thisf, new_ent): if is_dir and thisf != None and new_ent[2] != None: new_ent = list(new_ent) - new_ent[2] /= PS2MC_DIRENT_LENGTH + new_ent[2] = int(new_ent[2] / PS2MC_DIRENT_LENGTH) # print("len: ", ent[2], new_ent[2]) @@ -1251,7 +1251,7 @@ def delete_dirloc(self, dirloc, truncate, name): while cluster != PS2MC_FAT_CHAIN_END: if cluster / epc < self.fat_cursor: - self.fat_cursor = cluster / epc + self.fat_cursor = int(cluster / epc) next_cluster = self.lookup_fat(cluster) if next_cluster & PS2MC_FAT_ALLOCATED_BIT == 0: # corrupted diff --git a/round.py b/round.py index 1d81ef9..5f06723 100755 --- a/round.py +++ b/round.py @@ -10,12 +10,12 @@ _SCCS_ID = "@(#) mymc round.py 1.3 07/04/17 02:10:27\n" def div_round_up(a, b): - return (a + b - 1) / b + return int((a + b - 1) / b) def round_up(a, b): - return (a + b - 1) / b * b + return int((a + b - 1) / b * b) def round_down(a, b): - return a / b * b + return int(a / b * b) From 6a422f545edfd33dd648ec37d59b2fbf78159d2e Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Thu, 6 Jul 2023 19:48:59 +0200 Subject: [PATCH 10/15] Fixing unicode encoding/decoding Python3 has different handling of string encoding. Additionally, all functions dealing with data don't return strings, but bytearrays, so there were a lot of typing errors that needed to be fixed --- gui.py | 2 +- lzari.py | 38 +++++++++++++++++++------------------- mymc.py | 4 ++-- ps2mc.py | 41 +++++++++++++++++++++-------------------- ps2mc_dir.py | 6 ++++-- ps2mc_ecc.py | 10 +++++----- ps2save.py | 23 ++++++++++++----------- 7 files changed, 64 insertions(+), 60 deletions(-) diff --git a/gui.py b/gui.py index 88f39ad..14604cd 100755 --- a/gui.py +++ b/gui.py @@ -159,7 +159,7 @@ def __init__(self, parent, evt_focus, evt_select, config): def _update_dirtable(self, mc, dir): self.dirtable = table = [] - enc = "unicode" + enc = "utf-8" if self.config.get_ascii(): enc = "ascii" for ent in dir: diff --git a/lzari.py b/lzari.py index 9c12516..e84732b 100755 --- a/lzari.py +++ b/lzari.py @@ -59,15 +59,15 @@ # print("@@@ %s %04x" % (msg, value)) debug = lambda value, msg: None -_tr_16 = str.maketrans("0123456789abcdef", - "\x00\x01\x02\x03" - "\x10\x11\x12\x13" - "\x20\x21\x22\x23" - "\x30\x31\x32\x33") -_tr_4 = str.maketrans("0123", - "\x00\x01" - "\x10\x11") -_tr_2 = str.maketrans("01", "\x00\x01") +_tr_16 = bytes.maketrans(b"0123456789abcdef", + b"\x00\x01\x02\x03" + b"\x10\x11\x12\x13" + b"\x20\x21\x22\x23" + b"\x30\x31\x32\x33") +_tr_4 = bytes.maketrans(b"0123", + b"\x00\x01" + b"\x10\x11") +_tr_2 = bytes.maketrans(b"01", b"\x00\x01") def string_to_bit_array(s): """Convert a string to an array containing a sequence of bits.""" @@ -77,15 +77,15 @@ def string_to_bit_array(s): a = array.array('B', s) return a -_tr_rev_2 = str.maketrans("\x00\x01", "01") -_tr_rev_4 = str.maketrans("\x00\x01" - "\x10\x11", - "0123") -_tr_rev_16 = str.maketrans("\x00\x01\x02\x03" - "\x10\x11\x12\x13" - "\x20\x21\x22\x23" - "\x30\x31\x32\x33", - "0123456789abcdef") +_tr_rev_2 = bytes.maketrans(b"\x00\x01", b"01") +_tr_rev_4 = bytes.maketrans(b"\x00\x01" + b"\x10\x11", + b"0123") +_tr_rev_16 = bytes.maketrans(b"\x00\x01\x02\x03" + b"\x10\x11\x12\x13" + b"\x20\x21\x22\x23" + b"\x30\x31\x32\x33", + b"0123456789abcdef") def bit_array_to_string(a): """Convert an array containing a sequence of bits to a string.""" remainder = len(a) % 8 @@ -584,7 +584,7 @@ def decode(self, src, out_length, progress = None): a.fromlist([0] * 32) # add some extra bits self.in_iter = iter(a).next - out = array.array('B', "\0") * out_length + out = array.array('B', b"\0") * out_length outpos = 0 self.init(True) diff --git a/mymc.py b/mymc.py index 4d5c119..c780794 100755 --- a/mymc.py +++ b/mymc.py @@ -89,7 +89,7 @@ def _copy(fout, fin): while True: s = fin.read(1024) - if s == "": + if s == b"": break fout.write(s) @@ -472,7 +472,7 @@ def do_create_pad(cmd, mc, opts, args, opterr): length = mc.clusters_per_card if len(args) > 1: length = int(args[1]) - pad = "\0" * mc.cluster_size + pad = b"\0" * mc.cluster_size f = mc.open(args[0], "wb") try: for i in xrange(length): diff --git a/ps2mc.py b/ps2mc.py index 784ef98..2055bd0 100755 --- a/ps2mc.py +++ b/ps2mc.py @@ -97,7 +97,7 @@ def unpack_32bit_array(s): return array.array('I', s) def pack_32bit_array(a): - return a.tostring() + return a.tobytes() def unpack_superblock(s): sb = struct.unpack("<28s12sHHHHLLLLLL8x128s128sbbxx", s) @@ -349,7 +349,7 @@ def write_file_cluster(self, n, buf): True) return False mc.write_allocatable_cluster(cluster, - ["\0"] * cluster_size) + [b"\0"] * cluster_size) cluster = self._extend_file(n) if cluster == None: @@ -377,7 +377,7 @@ def read(self, size = None, eol = None): if size == None: size = self.length size = max(min(self.length - pos, size), 0) - ret = "" + ret = b"" while size > 0: off = int(pos % cluster_size) l = min(cluster_size - off, size) @@ -421,7 +421,7 @@ def write(self, out, _set_modified = True): else: buf = self.read_file_cluster(cluster) if buf == None: - buf = "\0" * cluster_size + buf = b"\0" * cluster_size buf = buf[:off] + s + buf[off + l:] if not self.write_file_cluster(cluster, buf): raise io_error(ENOSPC, @@ -515,7 +515,7 @@ def write_raw_ent(self, index, ent, set_modified): def next(self): # print("@@@ next", self.tell(), self.f.name) dirent = self.f.read(PS2MC_DIRENT_LENGTH) - if dirent == "": + if dirent == b"": if 0 == self._iter_end: raise StopIteration self.seek(0) @@ -615,14 +615,15 @@ def __init__(self, f, ignore_ecc = False, params = None): f.seek(0) s = f.read(0x154) - if len(s) != 0x154 or not s.startswith(PS2MC_MAGIC): + header = PS2MC_MAGIC.encode("utf-8") + if len(s) != 0x154 or not s[0:len(header)] == header: if (params == None): raise corrupt("Not a PS2 memory card image", f) self.f = f self.format(params) else: sb = unpack_superblock(s) - self.version = sb[1] + self.version = sb[1].decode("ascii") self.page_size = sb[2] self.pages_per_cluster = sb[3] self.pages_per_erase_block = sb[4] @@ -663,8 +664,8 @@ def __init__(self, f, ignore_ecc = False, params = None): self.curdir = (0, 0) def write_superblock(self): - s = pack_superblock((PS2MC_MAGIC, - self.version, + s = pack_superblock((PS2MC_MAGIC.encode("ascii"), + self.version.encode("ascii"), self.page_size, self.pages_per_cluster, self.pages_per_erase_block, @@ -679,10 +680,10 @@ def write_superblock(self): self.bad_erase_block_list, 2, 0x2B)) - s += "\x00" * (self.page_size - len(s)) + s += b"\x00" * (self.page_size - len(s)) self.write_page(0, s) - page = "\xFF" * self.raw_page_size + page = b"\xFF" * self.raw_page_size self.f.seek(self.good_block2 * self.pages_per_erase_block * self.raw_page_size) for i in range(self.pages_per_erase_block): @@ -739,7 +740,7 @@ def format(self, params): raise error("memory card image too small" " to be formatted") - ifc_list = unpack_fat("\0\0\0\0" + ifc_list = unpack_fat(b"\0\0\0\0" * PS2MC_MAX_INDIRECT_FAT_CLUSTERS) for i in range(indirect_fat_clusters): ifc_list[i] = first_ifc + i @@ -755,19 +756,19 @@ def format(self, params): self.good_block1 = good_block1 self.good_block2 = good_block2 self.indirect_fat_cluster_list = ifc_list - bebl = "\xFF\xFF\xFF\xFF" * 32 + bebl = b"\xFF\xFF\xFF\xFF" * 32 self.bad_erase_block_list = unpack_32bit_array(bebl) self._calculate_derived() self.ignore_ecc = not with_ecc - erased = "\0" * page_size + erased = b"\0" * page_size if not with_ecc: self.spare_size = 0 else: ecc = "".join(["".join(map(chr, s)) for s in ecc_calculate_page(erased)]) - erased += ecc + "\0" * (self.spare_size - len(ecc)) + erased += ecc.encode("utf-8") + b"\0" * (self.spare_size - len(ecc)) self.f.seek(0) for page in range(pages_per_card): @@ -801,7 +802,7 @@ def format(self, params): s = pack_dirent((DF_RWX | DF_DIR | DF_0400 | DF_EXISTS, 0, 2, now, 0, 0, now, 0, ".")) - s += "\0" * (cluster_size - len(s)) + s += b"\0" * (cluster_size - len(s)) self.write_allocatable_cluster(0, s) dir = self._directory((0, 0), 0, 2, "wb", "/") dir.write_raw_ent(1, (DF_WRITE | DF_EXECUTE | DF_DIR | DF_0400 @@ -845,7 +846,7 @@ def write_page(self, n, buf): for s in ecc_calculate_page(buf): a.fromlist(s) a.tofile(f) - f.write("\0" * (self.spare_size - len(a))) + f.write(b"\0" * (self.spare_size - len(a))) def read_cluster(self, n): pages_per_cluster = self.pages_per_cluster @@ -1208,7 +1209,7 @@ def create_dir_entry(self, parent_dirloc, name, mode): dirent = pack_dirent((DF_RWX | DF_0400 | DF_DIR | DF_EXISTS, 0, 0, now, dirloc[0], dirloc[1], now, 0, ".")) - dirent += "\0" * (self.cluster_size - PS2MC_DIRENT_LENGTH) + dirent += b"\0" * (self.cluster_size - PS2MC_DIRENT_LENGTH) self.write_allocatable_cluster(cluster, dirent) dir = self._directory(dirloc, cluster, 1, "wb", name = "") @@ -1908,8 +1909,8 @@ def get_icon_sys(self, dirname): f = self.open(icon_sys, "rb") s = f.read(964) f.close() - if len(s) == 964 and s[0:4] == "PS2D": - return s; + if len(s) == 964 and s[0:4] == b"PS2D": + return s return None def dir_size(self, dirname): diff --git a/ps2mc_dir.py b/ps2mc_dir.py index 2ab2c6e..3036caa 100755 --- a/ps2mc_dir.py +++ b/ps2mc_dir.py @@ -36,7 +36,7 @@ def zero_terminate(s): """Truncate a string at the first NUL ('\0') character, if any.""" - i = s.find('\0') + i = s.find(b'\0') if i == -1: return s return s[:i] @@ -67,13 +67,14 @@ def unpack_dirent(s): ent = list(ent) ent[3] = _tod_struct.unpack(ent[3]) ent[6] = _tod_struct.unpack(ent[6]) - ent[8] = zero_terminate(ent[8]) + ent[8] = zero_terminate(ent[8]).decode("ascii") return ent def pack_dirent(ent): ent = list(ent) ent[3] = _tod_struct.pack(*ent[3]) ent[6] = _tod_struct.pack(*ent[6]) + ent[8] = ent[8].encode("ascii") return _dirent_struct.pack(*ent) else: def unpack_tod(s): @@ -97,6 +98,7 @@ def pack_dirent(ent): ent = list(ent) ent[3] = struct.pack(_tod_fmt, *ent[3]) ent[6] = struct.pack(_tod_fmt, *ent[6]) + ent[8] = ent[8].encode("ascii") return struct.pack(_dirent_fmt, *ent) def time_to_tod(when): diff --git a/ps2mc_ecc.py b/ps2mc_ecc.py index 247dab7..16d5e21 100755 --- a/ps2mc_ecc.py +++ b/ps2mc_ecc.py @@ -64,7 +64,7 @@ def _ecc_calculate(s): if not isinstance(s, array.array): a = array.array('B') - a.fromstring(s) + a.frombytes(s) s = a column_parity = 0x77 line_parity_0 = 0x7F @@ -140,15 +140,15 @@ def ecc_check_page(page, spare): chunks = [] for i in range(div_round_up(len(page), 128)): a = array.array('B') - a.fromstring(page[i * 128 : i * 128 + 128]) - chunks.append((a, map(ord, spare[i * 3 : i * 3 + 3]))) + a.frombytes(page[i * 128 : i * 128 + 128]) + chunks.append((a, list(spare[i * 3 : i * 3 + 3]))) r = [ecc_check(s, ecc) for (s, ecc) in chunks] ret = ECC_CHECK_OK if ECC_CHECK_CORRECTED in r: # rebuild sector and spare from the corrected versions - page = "".join([a[0].tostring() + page = "".join([a[0].tobytes() for a in chunks]) spare = "".join([chr(a[1][i]) for a in chunks @@ -164,7 +164,7 @@ def ecc_check_page(page, spare): else: # _c_ubyte_p = ctypes.POINTER(ctypes.c_ubyte) def ecc_calculate(s): - aecc = array.array('B', "\0\0\0") + aecc = array.array('B', b"\0\0\0") cecc = ctypes.c_ubyte.from_address(aecc.buffer_info()[0]) mymcsup.ecc_calculate(s, len(s), cecc) return list(aecc) diff --git a/ps2save.py b/ps2save.py index 02af387..9d151aa 100755 --- a/ps2save.py +++ b/ps2save.py @@ -26,10 +26,10 @@ except ImportError: lzari = None -PS2SAVE_MAX_MAGIC = "Ps2PowerSave" -PS2SAVE_SPS_MAGIC = "\x0d\0\0\0SharkPortSave" -PS2SAVE_CBS_MAGIC = "CFU\0" -PS2SAVE_NPO_MAGIC = "nPort" +PS2SAVE_MAX_MAGIC = b"Ps2PowerSave" +PS2SAVE_SPS_MAGIC = b"\x0d\0\0\0SharkPortSave" +PS2SAVE_CBS_MAGIC = b"CFU\0" +PS2SAVE_NPO_MAGIC = b"nPort" # This is the initial permutation state ("S") for the RC4 stream cipher # algorithm used to encrpyt and decrypt Codebreaker saves. @@ -180,8 +180,8 @@ def shift_jis_conv(src, encoding = None): if encoding == "shift_jis": return src u = src.decode("shift_jis", "replace") - if encoding == "unicode": - return u + if encoding == "utf-8": + return u.encode("utf-8") a = [] for uc in u: try: @@ -242,8 +242,9 @@ def icon_sys_title(icon_sys, encoding = None): offset = icon_sys[1] title = icon_sys[14] - title2 = shift_jis_conv(title[offset:], encoding) - title1 = shift_jis_conv(title[:offset], encoding) + encoding = sys.getdefaultencoding() if not encoding else encoding + title2 = shift_jis_conv(title[offset:], encoding).decode(encoding) + title1 = shift_jis_conv(title[:offset], encoding).decode(encoding) return (title1, title2) def _read_fixed(f, n): @@ -350,7 +351,7 @@ def save_ems(self, f): # print(hex(ent[0])) raise error("Directory has a subdirectory.") f.write(data) - f.write("\0" * (round_up(len(data), cluster_size) + f.write(b"\0" * (round_up(len(data), cluster_size) - len(data))) f.flush() @@ -427,7 +428,7 @@ def save_max_drive(self, f): raise error("Non-file in save file.") s += struct.pack("= 12 and (dirname[0:2] in ("BA", "BJ", "BE", "BK")): From 3f6f5599781d8acf36209b33e557b0b22a8f0e1d Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Thu, 6 Jul 2023 19:51:35 +0200 Subject: [PATCH 11/15] Removing deprecated `xrange` In python3, `range` is equivalent to `xrange`, which is no longer valid --- mymc.py | 2 +- ps2mc.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mymc.py b/mymc.py index c780794..02fc388 100755 --- a/mymc.py +++ b/mymc.py @@ -475,7 +475,7 @@ def do_create_pad(cmd, mc, opts, args, opterr): pad = b"\0" * mc.cluster_size f = mc.open(args[0], "wb") try: - for i in xrange(length): + for i in range(length): f.write(pad) finally: f.close() diff --git a/ps2mc.py b/ps2mc.py index 2055bd0..8a01822 100755 --- a/ps2mc.py +++ b/ps2mc.py @@ -1148,7 +1148,7 @@ def search_directory(self, dir, name): start = dir.tell() - 1 if start == -1: start = 0 - for i in range(start, len(dir)) + range(0, start): + for i in list(range(start, len(dir))) + list(range(0, start)): try: ent = dir[i] except IndexError: @@ -1728,7 +1728,7 @@ def get_free_space(self): """Returns the amount of free space in bytes.""" free = 0 - for i in xrange(self.allocatable_cluster_end): + for i in range(self.allocatable_cluster_end): if (self.lookup_fat(i) & PS2MC_FAT_ALLOCATED_BIT) == 0: free += 1 return free * self.cluster_size @@ -1783,7 +1783,7 @@ def _check_dir(self, fat, dirloc, dirname, ent): print("bad directory:", (dirname + ': missing ".." entry')) ret = False - for i in xrange(2, length): + for i in range(2, length): ent = dir[i] mode = ent[0] if not (mode & DF_EXISTS): @@ -1821,7 +1821,7 @@ def check(self): ret = self._check_dir(fat, (0, 0), "/", ent) lost_clusters = 0 - for i in xrange(self.allocatable_cluster_end): + for i in range(self.allocatable_cluster_end): a = self.lookup_fat(i) if (a & PS2MC_FAT_ALLOCATED_BIT) and not fat[i]: print(i,) From 0585bea7040398f1cabf9add67b8daf230b72e58 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Thu, 6 Jul 2023 19:53:08 +0200 Subject: [PATCH 12/15] Fixing iterators Python3 requires the `next` function to be called `__next__`, which is implicitly called when generating the next value using the iterator --- ps2mc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ps2mc.py b/ps2mc.py index 8a01822..4b7bf49 100755 --- a/ps2mc.py +++ b/ps2mc.py @@ -512,7 +512,7 @@ def write_raw_ent(self, index, ent, set_modified): self.f.write(pack_dirent(ent), _set_modified = set_modified) - def next(self): + def __next__(self): # print("@@@ next", self.tell(), self.f.name) dirent = self.f.read(PS2MC_DIRENT_LENGTH) if dirent == b"": From 7d9ba4e893090f28cc8ca5069cc26eccb85e2976 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Thu, 6 Jul 2023 19:54:15 +0200 Subject: [PATCH 13/15] Random fixes Everything else that was wrong or was making the linter unhappy --- mymc.py | 2 +- ps2mc.py | 2 +- ps2mc_dir.py | 1 + ps2save.py | 4 ++-- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/mymc.py b/mymc.py index 02fc388..ff06b6c 100755 --- a/mymc.py +++ b/mymc.py @@ -337,7 +337,7 @@ def do_rename(cmd, mc, opts, args, opterr): mc.rename(args[0], args[1]) def _get_ps2_title(mc, enc): - s = mc.get_icon_sys("."); + s = mc.get_icon_sys(".") if s == None: return None a = ps2save.unpack_icon_sys(s) diff --git a/ps2mc.py b/ps2mc.py index 4b7bf49..0ce643e 100755 --- a/ps2mc.py +++ b/ps2mc.py @@ -1236,7 +1236,7 @@ def delete_dirloc(self, dirloc, truncate, name): if dirloc in self.open_files: raise io_error(EBUSY, - "cannot remove open file", filename) + "cannot remove open file", name) epc = self.entries_per_cluster diff --git a/ps2mc_dir.py b/ps2mc_dir.py index 3036caa..58f10a5 100755 --- a/ps2mc_dir.py +++ b/ps2mc_dir.py @@ -12,6 +12,7 @@ import struct import time import calendar +import os PS2MC_DIRENT_LENGTH = 512 diff --git a/ps2save.py b/ps2save.py index 9d151aa..b5f8bc7 100755 --- a/ps2save.py +++ b/ps2save.py @@ -369,14 +369,14 @@ def _load_max_drive_2(self): off = 0 for i in range(dirlen): if len(s) - off < 36: - raise eof(f) + raise eof(self.f) (l, name) = struct.unpack(" Date: Thu, 6 Jul 2023 20:14:35 +0200 Subject: [PATCH 14/15] Updating version numbers --- gui.py | 2 +- lzari.py | 2 +- mymc.py | 2 +- ps2mc.py | 2 +- ps2mc_dir.py | 2 +- ps2mc_ecc.py | 2 +- ps2save.py | 2 +- round.py | 2 +- verbuild.py | 4 ++-- 9 files changed, 10 insertions(+), 10 deletions(-) diff --git a/gui.py b/gui.py index 14604cd..8246aeb 100755 --- a/gui.py +++ b/gui.py @@ -7,7 +7,7 @@ """Graphical user-interface for mymc.""" -_SCCS_ID = "@(#) mymc gui.py 1.8 22/02/05 19:20:59\n" +_SCCS_ID = "@(#) mymc gui.py 1.9 23/07/06 19:35:21\n" import os import sys diff --git a/lzari.py b/lzari.py index e84732b..a8bc60b 100755 --- a/lzari.py +++ b/lzari.py @@ -11,7 +11,7 @@ LZARI.C's binary search tree. """ -_SCCS_ID = "@(#) mymc lzari.py 1.6 12/10/04 19:07:53\n" +_SCCS_ID = "@(#) mymc lzari.py 1.7 23/07/06 16:03:53\n" import sys import array diff --git a/mymc.py b/mymc.py index ff06b6c..e2a3c80 100755 --- a/mymc.py +++ b/mymc.py @@ -7,7 +7,7 @@ """A utility for manipulating PS2 memory card images.""" -_SCCS_ID = "@(#) mymc mymc.py 1.13 22/01/15 01:04:45\n"[:-1] +_SCCS_ID = "@(#) mymc mymc.py 1.14 23/07/06 15:51:52\n"[:-1] import sys import os diff --git a/ps2mc.py b/ps2mc.py index 0ce643e..7d1b4c1 100755 --- a/ps2mc.py +++ b/ps2mc.py @@ -7,7 +7,7 @@ """Manipulate PS2 memory card images.""" -_SCCS_ID = "@(#) mymc ps2mc.py 1.11 22/01/15 01:17:07\n" +_SCCS_ID = "@(#) mymc ps2mc.py 1.12 23/07/06 19:48:03\n" import sys import array diff --git a/ps2mc_dir.py b/ps2mc_dir.py index 58f10a5..fb8cc59 100755 --- a/ps2mc_dir.py +++ b/ps2mc_dir.py @@ -7,7 +7,7 @@ """Functions for working with PS2 memory card directory entries.""" -_SCCS_ID = "@(#) mymc ps2mc_dir.py 1.4 12/10/04 19:11:08\n" +_SCCS_ID = "@(#) mymc ps2mc_dir.py 1.5 23/07/06 06:30:13\n" import struct import time diff --git a/ps2mc_ecc.py b/ps2mc_ecc.py index 16d5e21..1c18a2e 100755 --- a/ps2mc_ecc.py +++ b/ps2mc_ecc.py @@ -10,7 +10,7 @@ correcting codes (ECC), as used on PS2 memory cards. """ -_SCCS_ID = "@(#) mymc ps2mc_ecc.py 1.4 07/12/17 02:34:04\n" +_SCCS_ID = "@(#) mymc ps2mc_ecc.py 1.5 23/07/06 16:03:58\n" import array diff --git a/ps2save.py b/ps2save.py index b5f8bc7..f7c3d9c 100755 --- a/ps2save.py +++ b/ps2save.py @@ -7,7 +7,7 @@ # A simple interface for working with various PS2 save file formats. # -_SCCS_ID = "@(#) mymc ps2save.py 1.8 22/01/15 01:25:25\n" +_SCCS_ID = "@(#) mymc ps2save.py 1.9 23/07/06 19:46:30\n" import sys import os diff --git a/round.py b/round.py index 5f06723..a768ee3 100755 --- a/round.py +++ b/round.py @@ -7,7 +7,7 @@ # Simple rounding functions. # -_SCCS_ID = "@(#) mymc round.py 1.3 07/04/17 02:10:27\n" +_SCCS_ID = "@(#) mymc round.py 1.4 23/07/06 02:44:14\n" def div_round_up(a, b): return int((a + b - 1) / b) diff --git a/verbuild.py b/verbuild.py index 71b2f66..7190414 100755 --- a/verbuild.py +++ b/verbuild.py @@ -1,2 +1,2 @@ -MYMC_VERSION_BUILD = r'''7''' -MYMC_VERSION_MAJOR = r'''2''' +MYMC_VERSION_BUILD = r'''0''' +MYMC_VERSION_MAJOR = r'''3''' From 779a1ddaa3fcbfebc05086db59d5b336065410ae Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Sat, 29 Jul 2023 17:11:53 +0200 Subject: [PATCH 15/15] Leaving utf-8 as default encoding instead of ASCII Less compatibility issues this way --- ps2mc_dir.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ps2mc_dir.py b/ps2mc_dir.py index fb8cc59..a0aca16 100755 --- a/ps2mc_dir.py +++ b/ps2mc_dir.py @@ -68,14 +68,14 @@ def unpack_dirent(s): ent = list(ent) ent[3] = _tod_struct.unpack(ent[3]) ent[6] = _tod_struct.unpack(ent[6]) - ent[8] = zero_terminate(ent[8]).decode("ascii") + ent[8] = zero_terminate(ent[8]).decode("utf-8") return ent def pack_dirent(ent): ent = list(ent) ent[3] = _tod_struct.pack(*ent[3]) ent[6] = _tod_struct.pack(*ent[6]) - ent[8] = ent[8].encode("ascii") + ent[8] = ent[8].encode("utf-8") return _dirent_struct.pack(*ent) else: def unpack_tod(s): @@ -99,7 +99,7 @@ def pack_dirent(ent): ent = list(ent) ent[3] = struct.pack(_tod_fmt, *ent[3]) ent[6] = struct.pack(_tod_fmt, *ent[6]) - ent[8] = ent[8].encode("ascii") + ent[8] = ent[8].encode("utf-8") return struct.pack(_dirent_fmt, *ent) def time_to_tod(when):