From b66ee7e61946c7dc03fb57b3ad6262f488a2c0ef Mon Sep 17 00:00:00 2001 From: cidoku Date: Sun, 23 Feb 2025 14:09:24 -0300 Subject: [PATCH] handle some file open()s better --- gameview.py | 25 ++++++++++++++----------- options.py | 3 ++- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/gameview.py b/gameview.py index 224b36f..30c156a 100644 --- a/gameview.py +++ b/gameview.py @@ -1197,7 +1197,8 @@ class gui(QtGui.QWidget): self.height = 730 theme = get_option("General", "theme", "default") try: - exec open(AO2XPpath+"ao2xp_themes/"+theme+"/theme.py") + with open(AO2XPpath+"ao2xp_themes/"+theme+"/theme.py") as t: + exec t except Exception as e: QtGui.QMessageBox.critical(None, "Unable to load theme", "There was a problem loading the current theme \"%s\":\n\n%s." % (theme, e)) os._exit(-2) @@ -1601,8 +1602,9 @@ class gui(QtGui.QWidget): inifile.set(id, "name", evi[0].replace('\n', '\\n').encode('utf-8')) inifile.set(id, "description", evi[1].replace('\n', '\\n').encode('utf-8')) inifile.set(id, "image", evi[2].encode('utf-8')) - - inifile.write(open(path, "wb")) + + with open(path, "wb") as f: + inifile.write(f) def onImportEvidence(self, is_autoload=False): if not is_autoload: @@ -2545,14 +2547,15 @@ class gui(QtGui.QWidget): self.anim_state = 3 if exists(AO2XPpath+"callwords.ini"): - callwords = [line.rstrip() for line in open(AO2XPpath+"callwords.ini")] - for callword in callwords: - if callword.decode('utf-8').lower() in self.m_chatmessage[CHATMSG].lower().split(" "): - self.ooclog.append("%s called you." % f_char) - snd = audio.loadhandle(False, "word_call.wav", 0, 0, BASS_STREAM_AUTOFREE) - if snd: - audio.playhandle(snd, True) - break + with open(AO2XPpath+"callwords.ini") as f: + callwords = [line.rstrip() for line in f] + for callword in callwords: + if callword.decode('utf-8').lower() in self.m_chatmessage[CHATMSG].lower().split(" "): + self.ooclog.append("%s called you." % f_char) + snd = audio.loadhandle(False, "word_call.wav", 0, 0, BASS_STREAM_AUTOFREE) + if snd: + audio.playhandle(snd, True) + break def do_effect(self, fx_name, fx_sound, p_char, p_folder): effect = ini.get_effect(fx_name, p_char, p_folder) diff --git a/options.py b/options.py index 59f1dc7..0aa64dd 100644 --- a/options.py +++ b/options.py @@ -296,7 +296,8 @@ class Settings(QtGui.QDialog): self.inifile.set("Audio", "Sound volume", self.soundslider.value()) self.inifile.set("Audio", "Blip volume", self.blipslider.value()) - self.inifile.write(open("AO2XP.ini", "wb")) + with open("AO2XP.ini", "wb") as f: + self.inifile.write(f) with open(AO2XPpath+"callwords.ini", "w") as f: f.write(self.callwords_edit.toPlainText().toUtf8())