diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2017-10-06 08:02:05 -0400 |
---|---|---|
committer | Jon Dufresne <jon.dufresne@gmail.com> | 2017-10-11 18:48:03 -0700 |
commit | aea4d6a0dd052b3fa28e1b00c9922bf31326673c (patch) | |
tree | ea256e7191cd9b5acb287d737bd701e43982e3d5 /src/saml2/sigver.py | |
parent | 847e970c5af6a09de61307be3a3d0305eee131f7 (diff) | |
download | pysaml2-aea4d6a0dd052b3fa28e1b00c9922bf31326673c.tar.gz |
Clean up unclosed files causing ResourceWarnings in tests
* Use better subprocess functions to correctly close pipes.
* When opening files, use a context manager to ensure file is closed
in a deterministic way.
* Close logging handlers
Diffstat (limited to 'src/saml2/sigver.py')
-rw-r--r-- | src/saml2/sigver.py | 44 |
1 files changed, 21 insertions, 23 deletions
diff --git a/src/saml2/sigver.py b/src/saml2/sigver.py index 3d80fb74..b89ae69c 100644 --- a/src/saml2/sigver.py +++ b/src/saml2/sigver.py @@ -787,8 +787,8 @@ class CryptoBackendXmlSec1(CryptoBackend): def version(self): com_list = [self.xmlsec, "--version"] pof = Popen(com_list, stderr=PIPE, stdout=PIPE) - content = pof.stdout.read().decode('ascii') - pof.wait() + content, _ = pof.communicate() + content = content.decode('ascii') try: return content.split(" ")[1] except IndexError: @@ -971,32 +971,30 @@ class CryptoBackendXmlSec1(CryptoBackend): :param exception: The exception class to raise on errors :result: Whatever xmlsec wrote to an --output temporary file """ - ntf = NamedTemporaryFile(suffix=".xml", - delete=self._xmlsec_delete_tmpfiles) - com_list.extend(["--output", ntf.name]) - com_list += extra_args + with NamedTemporaryFile(suffix=".xml", delete=self._xmlsec_delete_tmpfiles) as ntf: + com_list.extend(["--output", ntf.name]) + com_list += extra_args - logger.debug("xmlsec command: %s", " ".join(com_list)) + logger.debug("xmlsec command: %s", " ".join(com_list)) - pof = Popen(com_list, stderr=PIPE, stdout=PIPE) + pof = Popen(com_list, stderr=PIPE, stdout=PIPE) + p_out, p_err = pof.communicate() + p_out = p_out.decode('utf-8') + p_err = p_err.decode('utf-8') - p_out = pof.stdout.read().decode('utf-8') - p_err = pof.stderr.read().decode('utf-8') - pof.wait() + if pof.returncode is not None and pof.returncode < 0: + logger.error(LOG_LINE, p_out, p_err) + raise XmlsecError("%d:%s" % (pof.returncode, p_err)) - if pof.returncode is not None and pof.returncode < 0: - logger.error(LOG_LINE, p_out, p_err) - raise XmlsecError("%d:%s" % (pof.returncode, p_err)) + try: + if validate_output: + parse_xmlsec_output(p_err) + except XmlsecError as exc: + logger.error(LOG_LINE_2, p_out, p_err, exc) + raise - try: - if validate_output: - parse_xmlsec_output(p_err) - except XmlsecError as exc: - logger.error(LOG_LINE_2, p_out, p_err, exc) - raise - - ntf.seek(0) - return p_out, p_err, ntf.read() + ntf.seek(0) + return p_out, p_err, ntf.read() class CryptoBackendXMLSecurity(CryptoBackend): |