Skip to content

Commit

Permalink
Merge pull request #1821 from BoboTiG/fix-deprecation-warning-unclose…
Browse files Browse the repository at this point in the history
…d-file

Fix more DeprecationWarning: unclosed file
  • Loading branch information
gbtami committed Apr 13, 2020
2 parents 8510a85 + 435eac6 commit 3ede81c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 28 deletions.
12 changes: 5 additions & 7 deletions lib/pychess/Database/PgnImport.py
Expand Up @@ -396,13 +396,11 @@ def do_import(self, filename, info=None, progressbar=None):
if self.append_pgn:
# reopen database to write
self.db_handle.close()
self.db_handle = protosave(self.chessfile.path, self.append_pgn)

log.info("Append from %s to %s" % (pgnfile, self.chessfile.path))
handle.seek(0)
self.db_handle.writelines(handle)
self.db_handle.close()
handle.close()
with protosave(self.chessfile.path, self.append_pgn) as self.db_handle:
log.info("Append from %s to %s" % (pgnfile, self.chessfile.path))
handle.seek(0)
self.db_handle.writelines(handle)
handle.close()

if self.chessfile.scoutfish is not None:
# create new .scout from pgnfile we are importing
Expand Down
3 changes: 2 additions & 1 deletion lib/pychess/Players/engineNest.py
Expand Up @@ -56,7 +56,8 @@ def __init__(self):
self.jsonpath = addUserConfigPrefix("engines.json")
self.engines = []
try:
self.engines = json.load(open(self.jsonpath))
with open(self.jsonpath) as fh:
self.engines = json.load(fh)
except ValueError as err:
log.warning(
"engineNest: Couldn\'t read engines.json, renamed it to .bak\n%s\n%s"
Expand Down
3 changes: 2 additions & 1 deletion lib/pychess/Utils/GameModel.py
Expand Up @@ -676,7 +676,8 @@ def save(self, uri, saver, append, position=None, flip=False):
else:
fileobj = uri
self.uri = None
saver.save(fileobj, self, position, flip)
with fileobj:
saver.save(fileobj, self, position, flip)
self.needsSave = False
self.emit("game_saved", uri)

Expand Down
16 changes: 8 additions & 8 deletions lib/pychess/widgets/Background.py
Expand Up @@ -202,11 +202,11 @@ def get_col(col, mult):
# Check if a cache has been saved
temppng = addUserCachePrefix("temp.png")
if path.isfile(temppng):
fyle = open(temppng, "rb")
# Check if the cache was made while using the same theme
if list(fyle.read(6)) == colors:
surface = cairo.ImageSurface.create_from_png(fyle)
return
with open(temppng, "rb") as fyle:
# Check if the cache was made while using the same theme
if list(fyle.read(6)) == colors:
surface = cairo.ImageSurface.create_from_png(fyle)
return

# Get mostly transparant shadowy image
imgsurface = cairo.ImageSurface.create_from_png(background)
Expand All @@ -227,9 +227,9 @@ def get_col(col, mult):

# Save a cache for later use. Save 'newcolor' in the frist three pixels
# to check for theme changes between two instances
fyle = open(temppng, "wb")
fyle.write(bytes(colors))
surface.write_to_png(fyle)
with open(temppng, "wb") as fyle:
fyle.write(bytes(colors))
surface.write_to_png(fyle)


def isDarkTheme(widget):
Expand Down
8 changes: 4 additions & 4 deletions lib/pychess/widgets/pydock/OverlayWindow.py
Expand Up @@ -127,14 +127,14 @@ def getcol(col):
"#ffffff": getcol("p_base_color"),
"#000000": getcol("p_fg_color")}

data = open(svgPath).read()
with open(svgPath) as fh:
data = fh.read()
data = re.sub(
"|".join(colorDic.keys()),
lambda m: m.group() in colorDic and colorDic[m.group()] or m.group(),
data)
f_handle = open(TEMP_PATH, "w")
f_handle.write(data)
f_handle.close()
with open(TEMP_PATH, "w") as f_handle:
f_handle.write(data)
svg = Rsvg.Handle.new_from_file(TEMP_PATH)
os.remove(TEMP_PATH)
return svg
Expand Down
5 changes: 2 additions & 3 deletions lib/pychess/widgets/pydock/PyDockTop.py
Expand Up @@ -167,9 +167,8 @@ def saveToXML(self, xmlpath):

if self.get_child():
self.__addToXML(self.get_child(), dockElem, doc)
f_handle = open(xmlpath, "w")
doc.writexml(f_handle)
f_handle.close()
with open(xmlpath, "w") as f_handle:
doc.writexml(f_handle)
doc.unlink()

def __addToXML(self, component, parentElement, document):
Expand Down
6 changes: 4 additions & 2 deletions setup.py
Expand Up @@ -141,10 +141,12 @@ def run(self):
stdout = sys.stdout

if not isfile("eco.db"):
exec(open("pgn2ecodb.py").read())
with open("pgn2ecodb.py") as fh:
exec(fh.read())

if not isfile(os.path.abspath("pieces/Pychess.png")):
exec(open("create_theme_preview.py").read())
with open("create_theme_preview.py") as fh:
exec(fh.read())

# restore
sys.stderr = stderr
Expand Down
4 changes: 2 additions & 2 deletions utilities/arena.py
Expand Up @@ -115,8 +115,8 @@ def cb_gameended(game, reason):
printScoreboard()
print()

f = open("arena.pgn", "a+")
save(f, game)
with open("arena.pgn", "a+") as fh:
save(fh, game)

runGame()

Expand Down

0 comments on commit 3ede81c

Please sign in to comment.