Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/chgserver.py @ 49042:cf99c4af1079
chgserver: remove Python 2 file descriptor logic
Follows up 0bb28b7736bc "chgserver: remove Python 2 support code."
On Python 2, we had to close newfp prior to restoring the original file
description since "delete newfp" would otherwise close the file descriptor
shared with the long-lived fp:
in attachio():
newfp = os.fdopen(fp.fileno(), mode, bufsize)
in _restoreio():
newfp.close() # temporarily close newfp.fileno() (= fp.fileno())
os.dup2(fd, fp.fileno()) # reopen fp.fileno() with original fd
On the other hand, we shouldn't call newfp.close() on Python 3 since
any function calls are proxied to the underlying file object by
procutil.LineBufferedWrapper.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Fri, 04 Mar 2022 10:28:46 +0900 |
parents | 642e31cb55f0 |
children | cd51d4957b28 |
comparison
equal
deleted
inserted
replaced
49041:11c0411bf4e2 | 49042:cf99c4af1079 |
---|---|
436 if not self._oldios: | 436 if not self._oldios: |
437 return | 437 return |
438 nullfd = os.open(os.devnull, os.O_WRONLY) | 438 nullfd = os.open(os.devnull, os.O_WRONLY) |
439 ui = self.ui | 439 ui = self.ui |
440 for (ch, fp, fd), (cn, fn, mode) in zip(self._oldios, _iochannels): | 440 for (ch, fp, fd), (cn, fn, mode) in zip(self._oldios, _iochannels): |
441 newfp = getattr(ui, fn) | |
442 # On Python 3, newfp is just a wrapper around fp even if newfp is | |
443 # not fp, so deleting newfp is safe. | |
444 if newfp is not fp: | |
445 newfp.close() | |
446 # restore original fd: fp is open again | |
447 try: | 441 try: |
448 if newfp is fp and 'w' in mode: | 442 if 'w' in mode: |
449 # Discard buffered data which couldn't be flushed because | 443 # Discard buffered data which couldn't be flushed because |
450 # of EPIPE. The data should belong to the current session | 444 # of EPIPE. The data should belong to the current session |
451 # and should never persist. | 445 # and should never persist. |
452 os.dup2(nullfd, fp.fileno()) | 446 os.dup2(nullfd, fp.fileno()) |
453 fp.flush() | 447 fp.flush() |