Skip to content

Commit

Permalink
Wrap click.open_file in with clause to safely load and close
Browse files Browse the repository at this point in the history
Resolves issue of ResourceWarning: unclosed file
  • Loading branch information
matthewfeickert committed Sep 18, 2018
1 parent 5afc2a7 commit dac3303
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
36 changes: 19 additions & 17 deletions pyhf/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@ def xml2json(entrypoint_xml, basedir, output_file, track_progress):
if output_file is None:
print(json.dumps(spec, indent=4, sort_keys=True))
else:
json.dump(spec, open(output_file, 'w+'), indent=4, sort_keys=True)
with open(output_file, 'w+') as out_file:
json.dump(spec, out_file, indent=4, sort_keys=True)
log.debug("Written to {0:s}".format(output_file))

@pyhf.command()
@click.argument('workspace', default = '-')
@click.argument('xmlfile', default = '-')
@click.option('--specroot', default = click.Path(exists = True))
@click.option('--dataroot', default = click.Path(exists = True))
def json2xml(workspace,xmlfile,specroot,dataroot):
specstream = click.open_file(workspace)
outstream = click.open_file(xmlfile,'w')
d = json.load(specstream)

outstream.write(writexml.writexml(d,specroot,dataroot,'').decode('utf-8'))
@click.argument('workspace', default='-')
@click.argument('xmlfile', default='-')
@click.option('--specroot', default=click.Path(exists=True))
@click.option('--dataroot', default=click.Path(exists=True))
def json2xml(workspace, xmlfile, specroot, dataroot):
with click.open_file(workspace, 'r') as specstream:
d = json.load(specstream)
with click.open_file(xmlfile, 'w') as outstream:
outstream.write(writexml.writexml(d, specroot, dataroot,'').decode('utf-8'))

@pyhf.command()
@click.argument('workspace', default = '-')
@click.argument('workspace', default='-')
@click.option('--output-file', help='The location of the output json file. If not specified, prints to screen.', default=None)
@click.option('--measurement', default=None)
@click.option('-p','--patch', multiple = True)
@click.option('-p', '--patch', multiple=True)
@click.option('--qualify-names/--no-qualify-names', default=False)
def cls(workspace, output_file, measurement, qualify_names, patch):
specstream = click.open_file(workspace)
d = json.load(specstream)
with click.open_file(workspace, 'r') as specstream:
d = json.load(specstream)
measurements = d['toplvl']['measurements']
measurement_names = [m['name'] for m in measurements]
measurement_index = 0
Expand All @@ -68,13 +68,15 @@ def cls(workspace, output_file, measurement, qualify_names, patch):
log.debug('calculating CLs for measurement {0:s}'.format(measurements[measurement_index]['name']))
spec = {'channels':d['channels']}
for p in patch:
p = jsonpatch.JsonPatch(json.loads(click.open_file(p).read()))
with click.open_file(p, 'r') as read_file:
p = jsonpatch.JsonPatch(json.loads(read_file.read()))
spec = p.apply(spec)
p = Model(spec, poiname=measurements[measurement_index]['config']['poi'], qualify_names=qualify_names)
result = runOnePoint(1.0, sum((d['data'][c['name']] for c in d['channels']),[]) + p.config.auxdata, p)
result = {'CLs_obs': result[-2].tolist()[0], 'CLs_exp': result[-1].ravel().tolist()}
if output_file is None:
print(json.dumps(result, indent=4, sort_keys=True))
else:
json.dump(result, open(output_file, 'w+'), indent=4, sort_keys=True)
with open(output_file, 'w+') as out_file:
json.dump(result, out_file, indent=4, sort_keys=True)
log.debug("Written to {0:s}".format(output_file))
3 changes: 1 addition & 2 deletions tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_patch(tmpdir, script_runner):
import io
command = 'pyhf cls {0:s} --patch -'.format(temp.strpath,patch.strpath)

pipefile = io.StringIO(patchcontent) #python 2.7 pytest-files are not file-like enough
pipefile = io.StringIO(patchcontent) # python 2.7 pytest-files are not file-like enough
ret = script_runner.run(*shlex.split(command), stdin = pipefile)
print(ret.stderr)
assert ret.success
Expand All @@ -106,4 +106,3 @@ def test_patch_fail(tmpdir, script_runner):
command = 'pyhf cls {0:s} --patch {1:s}'.format(temp.strpath,patch.strpath)
ret = script_runner.run(*shlex.split(command))
assert not ret.success

0 comments on commit dac3303

Please sign in to comment.