Commit 863b9952 authored by Kipp Cannon's avatar Kipp Cannon

glue.ligolw: don't close stdin

- I did tests in the command line interface that made it seem like using
  stdin as a context manager would not close stdin, but when you do it in a
  program stdin gets closed, which we can't have.
parent bc04413d
Pipeline #47353 failed with stages
in 3 minutes and 2 seconds
......@@ -393,10 +393,13 @@ def load_filename(filename, verbose = False, **kwargs):
"""
if verbose:
sys.stderr.write("reading %s ...\n" % (("'%s'" % filename) if filename is not None else "stdin"))
# In Python 3, ``sys.stdin`` has an attribute called ``buffer``
# that is the underyling byte-oriented stream.
with open(filename, "rb") if filename is not None else sys.stdin.buffer if hasattr(sys.stdin, "buffer") else sys.stdin as fileobj:
xmldoc, hexdigest = load_fileobj(fileobj, **kwargs)
if filename is None:
# In Python 3, ``sys.stdin`` has an attribute called
# ``buffer`` that is the underyling byte-oriented stream.
xmldoc, hexdigest = load_fileobj(sys.stdin.buffer if hasattr(sys.stdin, "buffer") else sys.stdin, **kwargs)
else:
with open(filename, "rb") as fileobj:
xmldoc, hexdigest = load_fileobj(fileobj, **kwargs)
if verbose:
sys.stderr.write("md5sum: %s %s\n" % (hexdigest, (filename if filename is not None else "")))
return xmldoc
......@@ -420,18 +423,18 @@ def load_url(url, verbose = False, **kwargs):
"""
if verbose:
sys.stderr.write("reading %s ...\n" % (("'%s'" % url) if url is not None else "stdin"))
if url is not None:
if url is None:
# In Python 3, ``sys.stdin`` has an attribute called
# ``buffer`` that is the underyling byte-oriented stream.
xmldoc, hexdigest = load_fileobj(sys.stdin.buffer if hasattr(sys.stdin, "buffer") else sys.stdin, **kwargs)
else:
scheme, host, path = urllib.parse.urlparse(url)[:3]
if scheme.lower() in ("", "file") and host.lower() in ("", "localhost"):
fileobj = lambda: open(path, "rb")
else:
fileobj = lambda: urllib.request.urlopen(url)
else:
# In Python 3, ``sys.stdin`` has an attribute called
# ``buffer`` that is the underyling byte-oriented stream.
fileobj = lambda: sys.stdin.buffer if hasattr(sys.stdin, "buffer") else sys.stdin
with fileobj() as fileobj:
xmldoc, hexdigest = load_fileobj(fileobj, **kwargs)
with fileobj() as fileobj:
xmldoc, hexdigest = load_fileobj(fileobj, **kwargs)
if verbose:
sys.stderr.write("md5sum: %s %s\n" % (hexdigest, (url if url is not None else "")))
return xmldoc
......@@ -544,8 +547,9 @@ def write_filename(xmldoc, filename, verbose = False, gz = False, with_mv = True
sys.stderr.write("writing %s ...\n" % (("'%s'" % filename) if filename is not None else "stdout"))
with SignalsTrap(trap_signals):
if filename is None:
# In Python 3, ``sys.stdout`` has an attribute called ``buffer`` that is
# the underyling byte-oriented stream.
# In Python 3, ``sys.stdout`` has an attribute
# called ``buffer`` that is the underyling
# byte-oriented stream.
fileobj = sys.stdout
try:
fileobj = fileobj.buffer
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment