From f923828a206f56b527f43d836c825bf300c191a7 Mon Sep 17 00:00:00 2001
From: Pierre Raybaut
Date: Thu, 7 Nov 2024 11:44:00 +0100
Subject: [PATCH] File handling: use context managers for safer resource
management
---
plotpy/builder/shape.py | 3 ++-
plotpy/tests/features/test_loadsaveitems_pickle.py | 8 ++++----
plotpy/tests/tools/test_get_rectangle_with_svg.py | 3 ++-
plotpy/tools/item.py | 8 ++++----
plotpy/widgets/qtdesigner.py | 13 ++++++-------
5 files changed, 18 insertions(+), 17 deletions(-)
diff --git a/plotpy/builder/shape.py b/plotpy/builder/shape.py
index a7be7957..fb98fa29 100644
--- a/plotpy/builder/shape.py
+++ b/plotpy/builder/shape.py
@@ -179,7 +179,8 @@ def svg(
assert shape in ("circle", "rectangle", "square")
assert isinstance(fname_or_data, (str, bytes))
if isinstance(fname_or_data, str):
- data = open(fname_or_data, "rb").read()
+ with open(fname_or_data, "rb") as file:
+ data = file.read()
else:
data = fname_or_data
shapeklass = {
diff --git a/plotpy/tests/features/test_loadsaveitems_pickle.py b/plotpy/tests/features/test_loadsaveitems_pickle.py
index 4e443bd0..3b9c4855 100644
--- a/plotpy/tests/features/test_loadsaveitems_pickle.py
+++ b/plotpy/tests/features/test_loadsaveitems_pickle.py
@@ -192,14 +192,14 @@ class PickleTest(IOTest):
def restore_items(self) -> None:
"""Restore items"""
- f = open(self.FNAME, "rb")
- self.plot.restore_items(f)
+ with open(self.FNAME, "rb") as f:
+ self.plot.restore_items(f)
def save_items(self) -> None:
"""Save items"""
self.plot.select_all()
- f = open(self.FNAME, "wb")
- self.plot.save_items(f, selected=True)
+ with open(self.FNAME, "wb") as f:
+ self.plot.save_items(f, selected=True)
def test_pickle() -> None:
diff --git a/plotpy/tests/tools/test_get_rectangle_with_svg.py b/plotpy/tests/tools/test_get_rectangle_with_svg.py
index 8c96665f..a62b0f77 100644
--- a/plotpy/tests/tools/test_get_rectangle_with_svg.py
+++ b/plotpy/tests/tools/test_get_rectangle_with_svg.py
@@ -29,7 +29,8 @@ class SVGToolExample(RectangularShapeTool):
def create_shape(self):
"""Create shape to be drawn"""
- svg_data = open(self.SVG_FNAME, "rb").read()
+ with open(self.SVG_FNAME, "rb") as svg_file:
+ svg_data = svg_file.read()
shape = make.svg("rectangle", svg_data, 0, 0, 1, 1, "SVG")
self.set_shape_style(shape)
return shape, 0, 2
diff --git a/plotpy/tools/item.py b/plotpy/tools/item.py
index 342d7639..e6967465 100644
--- a/plotpy/tools/item.py
+++ b/plotpy/tools/item.py
@@ -311,8 +311,8 @@ def activate_command(self, plot: BasePlot, checked: bool) -> None:
)
if not fname:
return
- itemfile = open(fname, "wb")
- plot.save_items(itemfile, selected=True)
+ with open(fname, "wb") as itemfile:
+ plot.save_items(itemfile, selected=True)
class LoadItemsTool(OpenFileTool):
@@ -343,8 +343,8 @@ def activate_command(self, plot: BasePlot, checked: bool) -> None:
filename = self.get_filename(plot)
if not filename:
return
- itemfile = open(filename, "rb")
- plot.restore_items(itemfile)
+ with open(filename, "rb") as itemfile:
+ plot.restore_items(itemfile)
plot.replot()
diff --git a/plotpy/widgets/qtdesigner.py b/plotpy/widgets/qtdesigner.py
index 9146701b..c5697e07 100644
--- a/plotpy/widgets/qtdesigner.py
+++ b/plotpy/widgets/qtdesigner.py
@@ -36,7 +36,8 @@ def loadui(fname, replace_class="QwtPlot"):
QtDesigner plugins because they don't inheritate from a PyQt5.QtGui
object.
"""
- uifile_text = open(fname).read().replace(replace_class, "QFrame")
+ with open(fname) as f:
+ uifile_text = f.read().replace(replace_class, "QFrame")
ui, base_class = uic.loadUiType(io.StringIO(uifile_text))
class Form(base_class, ui):
@@ -55,12 +56,10 @@ def compileui(fname, replace_class="QwtPlot"):
:param fname:
:param replace_class:
"""
- uifile_text = open(fname).read().replace("QwtPlot", "QFrame")
- uic.compileUi(
- io.StringIO(uifile_text),
- open(fname.replace(".ui", "_ui.py"), "w"),
- pyqt3_wrapper=True,
- )
+ with open(fname) as f:
+ uifile_text = f.read().replace("QwtPlot", "QFrame")
+ with open(fname.replace(".ui", "_ui.py"), "w") as pyfile:
+ uic.compileUi(io.StringIO(uifile_text), pyfile, pyqt3_wrapper=True)
def create_qtdesigner_plugin(