Skip to content

Commit f923828

Browse files
committed
File handling: use context managers for safer resource management
1 parent 3e509ae commit f923828

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

plotpy/builder/shape.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ def svg(
179179
assert shape in ("circle", "rectangle", "square")
180180
assert isinstance(fname_or_data, (str, bytes))
181181
if isinstance(fname_or_data, str):
182-
data = open(fname_or_data, "rb").read()
182+
with open(fname_or_data, "rb") as file:
183+
data = file.read()
183184
else:
184185
data = fname_or_data
185186
shapeklass = {

plotpy/tests/features/test_loadsaveitems_pickle.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,14 @@ class PickleTest(IOTest):
192192

193193
def restore_items(self) -> None:
194194
"""Restore items"""
195-
f = open(self.FNAME, "rb")
196-
self.plot.restore_items(f)
195+
with open(self.FNAME, "rb") as f:
196+
self.plot.restore_items(f)
197197

198198
def save_items(self) -> None:
199199
"""Save items"""
200200
self.plot.select_all()
201-
f = open(self.FNAME, "wb")
202-
self.plot.save_items(f, selected=True)
201+
with open(self.FNAME, "wb") as f:
202+
self.plot.save_items(f, selected=True)
203203

204204

205205
def test_pickle() -> None:

plotpy/tests/tools/test_get_rectangle_with_svg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class SVGToolExample(RectangularShapeTool):
2929

3030
def create_shape(self):
3131
"""Create shape to be drawn"""
32-
svg_data = open(self.SVG_FNAME, "rb").read()
32+
with open(self.SVG_FNAME, "rb") as svg_file:
33+
svg_data = svg_file.read()
3334
shape = make.svg("rectangle", svg_data, 0, 0, 1, 1, "SVG")
3435
self.set_shape_style(shape)
3536
return shape, 0, 2

plotpy/tools/item.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ def activate_command(self, plot: BasePlot, checked: bool) -> None:
311311
)
312312
if not fname:
313313
return
314-
itemfile = open(fname, "wb")
315-
plot.save_items(itemfile, selected=True)
314+
with open(fname, "wb") as itemfile:
315+
plot.save_items(itemfile, selected=True)
316316

317317

318318
class LoadItemsTool(OpenFileTool):
@@ -343,8 +343,8 @@ def activate_command(self, plot: BasePlot, checked: bool) -> None:
343343
filename = self.get_filename(plot)
344344
if not filename:
345345
return
346-
itemfile = open(filename, "rb")
347-
plot.restore_items(itemfile)
346+
with open(filename, "rb") as itemfile:
347+
plot.restore_items(itemfile)
348348
plot.replot()
349349

350350

plotpy/widgets/qtdesigner.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def loadui(fname, replace_class="QwtPlot"):
3636
QtDesigner plugins because they don't inheritate from a PyQt5.QtGui
3737
object.
3838
"""
39-
uifile_text = open(fname).read().replace(replace_class, "QFrame")
39+
with open(fname) as f:
40+
uifile_text = f.read().replace(replace_class, "QFrame")
4041
ui, base_class = uic.loadUiType(io.StringIO(uifile_text))
4142

4243
class Form(base_class, ui):
@@ -55,12 +56,10 @@ def compileui(fname, replace_class="QwtPlot"):
5556
:param fname:
5657
:param replace_class:
5758
"""
58-
uifile_text = open(fname).read().replace("QwtPlot", "QFrame")
59-
uic.compileUi(
60-
io.StringIO(uifile_text),
61-
open(fname.replace(".ui", "_ui.py"), "w"),
62-
pyqt3_wrapper=True,
63-
)
59+
with open(fname) as f:
60+
uifile_text = f.read().replace("QwtPlot", "QFrame")
61+
with open(fname.replace(".ui", "_ui.py"), "w") as pyfile:
62+
uic.compileUi(io.StringIO(uifile_text), pyfile, pyqt3_wrapper=True)
6463

6564

6665
def create_qtdesigner_plugin(

0 commit comments

Comments
 (0)