Use open context-manager instead of file.open()

Static code checker identified multiple places where a file is opened
but is not necessarily closed.  This commit addressed that with the
exception of RemoteGraphicsView.py
This commit is contained in:
Ogi Moore 2021-04-25 17:05:05 -07:00
parent cf95964678
commit 4d388ee633
5 changed files with 44 additions and 63 deletions

View File

@ -369,7 +369,8 @@ class ExampleLoader(QtGui.QMainWindow):
return
if os.path.isdir(fn):
fn = os.path.join(fn, '__main__.py')
text = open(fn).read()
with open(fn, "r") as currentFile:
text = currentFile.read()
self.ui.codeView.setPlainText(text)
self.ui.loadedFileLabel.setText(fn)
self.codeBtn.hide()

View File

@ -56,7 +56,8 @@ def test_reload():
# write a module
mod = os.path.join(path, 'reload_test_mod.py')
print("\nRELOAD FILE:", mod)
open(mod, 'w').write(code.format(path_repr=pgpath_repr, msg="C.fn() Version1"))
with open(mod, "w") as file_:
file_.write(code.format(path_repr=pgpath_repr, msg="C.fn() Version1"))
# import the new module
import reload_test_mod
@ -64,61 +65,37 @@ def test_reload():
c = reload_test_mod.C()
c.sig.connect(c.fn)
if py3:
v1 = (reload_test_mod.C, reload_test_mod.C.sig, reload_test_mod.C.fn, c.sig, c.fn, c.fn.__func__)
else:
v1 = (reload_test_mod.C, reload_test_mod.C.sig, reload_test_mod.C.fn, reload_test_mod.C.fn.__func__, c.sig, c.fn, c.fn.__func__)
v1 = (reload_test_mod.C, reload_test_mod.C.sig, reload_test_mod.C.fn, c.sig, c.fn, c.fn.__func__)
# write again and reload
open(mod, 'w').write(code.format(path_repr=pgpath_repr, msg="C.fn() Version2"))
with open(mod, "w") as file_:
file_.write(code.format(path_repr=pgpath_repr, msg="C.fn() Version 2"))
time.sleep(1.1)
#remove_cache(mod)
result1 = pg.reload.reloadAll(path, debug=True)
if py3:
v2 = (reload_test_mod.C, reload_test_mod.C.sig, reload_test_mod.C.fn, c.sig, c.fn, c.fn.__func__)
else:
v2 = (reload_test_mod.C, reload_test_mod.C.sig, reload_test_mod.C.fn, reload_test_mod.C.fn.__func__, c.sig, c.fn, c.fn.__func__)
v2 = (reload_test_mod.C, reload_test_mod.C.sig, reload_test_mod.C.fn, c.sig, c.fn, c.fn.__func__)
if not py3:
assert c.fn.im_class is v2[0]
oldcfn = pg.reload.getPreviousVersion(c.fn)
if oldcfn is None:
# Function did not reload; are we using pytest's assertion rewriting?
raise Exception("Function did not reload. (This can happen when using py.test"
" with assertion rewriting; use --assert=plain for this test.)")
if py3:
assert oldcfn.__func__ is v1[2]
else:
assert oldcfn.im_class is v1[0]
assert oldcfn.__func__ is v1[2].__func__
assert oldcfn.__func__ is v1[2]
assert oldcfn.__self__ is c
# write again and reload
open(mod, 'w').write(code.format(path_repr=pgpath_repr, msg="C.fn() Version2"))
with open(mod, "w") as file_:
file_.write(code.format(path_repr=pgpath_repr, msg="C.fn() Version2"))
time.sleep(1.1)
# remove_cache(mod)
result2 = pg.reload.reloadAll(path, debug=True)
if py3:
v3 = (reload_test_mod.C, reload_test_mod.C.sig, reload_test_mod.C.fn, c.sig, c.fn, c.fn.__func__)
else:
v3 = (reload_test_mod.C, reload_test_mod.C.sig, reload_test_mod.C.fn, reload_test_mod.C.fn.__func__, c.sig, c.fn, c.fn.__func__)
#for i in range(len(old)):
#print id(old[i]), id(new1[i]), id(new2[i]), old[i], new1[i]
v3 = (reload_test_mod.C, reload_test_mod.C.sig, reload_test_mod.C.fn, c.sig, c.fn, c.fn.__func__)
cfn1 = pg.reload.getPreviousVersion(c.fn)
cfn2 = pg.reload.getPreviousVersion(cfn1)
if py3:
assert cfn1.__func__ is v2[2]
assert cfn2.__func__ is v1[2]
else:
assert cfn1.__func__ is v2[2].__func__
assert cfn2.__func__ is v1[2].__func__
assert cfn1.im_class is v2[0]
assert cfn2.im_class is v1[0]
assert cfn1.__func__ is v2[2]
assert cfn2.__func__ is v1[2]
assert cfn1.__self__ is c
assert cfn2.__self__ is c

View File

@ -110,8 +110,10 @@ class Install(install.install):
try:
initfile = os.path.join(path, '__init__.py')
data = open(initfile, 'r').read()
open(initfile, 'w').write(re.sub(r"__version__ = .*", "__version__ = '%s'" % version, data))
with open(initfile, "r") as file_:
data = file_.read()
with open(initfile, "w") as file_:
file_.write(re.sub(r"__version__ = .*", "__version__ = '%s'" % version, data))
installVersion = version
except:
sys.stderr.write("Warning: Error occurred while setting version string in build path. "

View File

@ -28,17 +28,18 @@ def generateDebianChangelog(package, logFile, version, maintainer):
current_version = None
current_log = None
current_date = None
for line in open(logFile).readlines():
match = re.match(package+r'-(\d+\.\d+\.\d+(\.\d+)?)\s*(\d+-\d+-\d+)\s*$', line)
if match is None:
if current_log is not None:
current_log.append(line)
else:
if current_log is not None:
releases.append((current_version, current_log, current_date))
current_version, current_date = match.groups()[0], match.groups()[2]
#sys.stderr.write("Found release %s\n" % current_version)
current_log = []
with open(logFile) as file_:
for line in file_.readlines():
match = re.match(package+r'-(\d+\.\d+\.\d+(\.\d+)?)\s*(\d+-\d+-\d+)\s*$', line)
if match is None:
if current_log is not None:
current_log.append(line)
else:
if current_log is not None:
releases.append((current_version, current_log, current_date))
current_version, current_date = match.groups()[0], match.groups()[2]
#sys.stderr.write("Found release %s\n" % current_version)
current_log = []
if releases[0][0] != version:
raise Exception("Latest release in changelog (%s) does not match current release (%s)\n" % (releases[0][0], version))

View File

@ -149,20 +149,20 @@ def checkStyle():
if os.path.splitext(f)[1] not in ('.py', '.rst'):
continue
filename = os.path.join(path, f)
fh = open(filename, 'U')
_ = fh.readlines()
endings = set(
fh.newlines
if isinstance(fh.newlines, tuple)
else (fh.newlines,)
)
endings -= allowedEndings
if len(endings) > 0:
print("\033[0;31m"
+ "File has invalid line endings: "
+ "%s" % filename + "\033[0m")
ret = ret | 2
count += 1
with open(filename, 'U') as fh:
_ = fh.readlines()
endings = set(
fh.newlines
if isinstance(fh.newlines, tuple)
else (fh.newlines,)
)
endings -= allowedEndings
if len(endings) > 0:
print("\033[0;31m"
+ "File has invalid line endings: "
+ "%s" % filename + "\033[0m")
ret = ret | 2
count += 1
print('checked line endings in %d files' % count)