Mercurial > public > mercurial-scm > hg
comparison mercurial/httppeer.py @ 51734:d17578f96e60
httppeer: simplify two-way stream cleanup
No need to conditionalize the cleanup if the filename is assigned outside the
exception handler. I suppose `fd` leaks if `os.fdopen()` fails, but that was
the case before too (and may trigger another exception in the `finally` block on
Windows, when the file is still open).
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Fri, 26 Jul 2024 21:54:07 -0400 |
parents | 12c308c55e53 |
children | 78f789a4c8a2 |
comparison
equal
deleted
inserted
replaced
51733:48eb51494a7a | 51734:d17578f96e60 |
---|---|
518 finally: | 518 finally: |
519 fp.close() | 519 fp.close() |
520 os.unlink(tempname) | 520 os.unlink(tempname) |
521 | 521 |
522 def _calltwowaystream(self, cmd, fp, **args): | 522 def _calltwowaystream(self, cmd, fp, **args): |
523 filename = None | 523 # dump bundle to disk |
524 fd, filename = pycompat.mkstemp(prefix=b"hg-bundle-", suffix=b".hg") | |
524 try: | 525 try: |
525 # dump bundle to disk | |
526 fd, filename = pycompat.mkstemp(prefix=b"hg-bundle-", suffix=b".hg") | |
527 with os.fdopen(fd, "wb") as fh: | 526 with os.fdopen(fd, "wb") as fh: |
528 d = fp.read(4096) | 527 d = fp.read(4096) |
529 while d: | 528 while d: |
530 fh.write(d) | 529 fh.write(d) |
531 d = fp.read(4096) | 530 d = fp.read(4096) |
532 # start http push | 531 # start http push |
533 with httpconnection.httpsendfile(self.ui, filename, b"rb") as fp_: | 532 with httpconnection.httpsendfile(self.ui, filename, b"rb") as fp_: |
534 headers = {'Content-Type': 'application/mercurial-0.1'} | 533 headers = {'Content-Type': 'application/mercurial-0.1'} |
535 return self._callstream(cmd, data=fp_, headers=headers, **args) | 534 return self._callstream(cmd, data=fp_, headers=headers, **args) |
536 finally: | 535 finally: |
537 if filename is not None: | 536 os.unlink(filename) |
538 os.unlink(filename) | |
539 | 537 |
540 def _callcompressable(self, cmd, **args): | 538 def _callcompressable(self, cmd, **args): |
541 return self._callstream(cmd, _compressible=True, **args) | 539 return self._callstream(cmd, _compressible=True, **args) |
542 | 540 |
543 def _abort(self, exception): | 541 def _abort(self, exception): |