Skip to content

Commit cc46702

Browse files
gh-143237: Fix support of named pipes in the rotating logging handlers
This fixes regression introduced in GH-105887.
1 parent fa9a425 commit cc46702

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Lib/logging/handlers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ def shouldRollover(self, record):
196196
if self.stream is None: # delay was set...
197197
self.stream = self._open()
198198
if self.maxBytes > 0: # are we rolling over?
199-
pos = self.stream.tell()
199+
try:
200+
pos = self.stream.tell()
201+
except io.UnsupportedOperation:
202+
# gh-143237: Never rollover a named pipe.
203+
return False
200204
if not pos:
201205
# gh-116263: Never rollover an empty file
202206
return False

Lib/test/test_logging.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6369,6 +6369,29 @@ def test_should_not_rollover_non_file(self):
63696369
self.assertFalse(rh.shouldRollover(self.next_rec()))
63706370
rh.close()
63716371

6372+
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
6373+
def test_should_not_rollover_named_pipe(self):
6374+
# gh-143237 - test with non-seekable special file (named pipe)
6375+
filename = os_helper.TESTFN
6376+
self.addCleanup(os_helper.unlink, filename)
6377+
try:
6378+
os.mkfifo(filename)
6379+
except PermissionError as e:
6380+
self.skipTest('os.mkfifo(): %s' % e)
6381+
6382+
def other_side():
6383+
with open(filename, 'rb') as f:
6384+
f.read(1)
6385+
6386+
thread = threading.Thread(target=other_side)
6387+
with threading_helper.start_threads([thread]):
6388+
rh = logging.handlers.RotatingFileHandler(
6389+
filename, encoding="utf-8", maxBytes=1)
6390+
try:
6391+
self.assertFalse(rh.shouldRollover(self.next_rec()))
6392+
finally:
6393+
rh.close()
6394+
63726395
def test_should_rollover(self):
63736396
with open(self.fn, 'wb') as f:
63746397
f.write(b'\n')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix support of named pipes in the rotating :mod:`logging` handlers.

0 commit comments

Comments
 (0)