mirror of
https://github.com/python/cpython.git
synced 2025-12-31 04:23:37 +00:00
gh-143237: Fix support of named pipes in the rotating logging handlers (GH-143259)
This fixes regression introduced in GH-105887.
This commit is contained in:
parent
7e3a5a7e79
commit
aa8a43d179
3 changed files with 33 additions and 1 deletions
|
|
@ -196,7 +196,11 @@ def shouldRollover(self, record):
|
|||
if self.stream is None: # delay was set...
|
||||
self.stream = self._open()
|
||||
if self.maxBytes > 0: # are we rolling over?
|
||||
try:
|
||||
pos = self.stream.tell()
|
||||
except io.UnsupportedOperation:
|
||||
# gh-143237: Never rollover a named pipe.
|
||||
return False
|
||||
if not pos:
|
||||
# gh-116263: Never rollover an empty file
|
||||
return False
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@
|
|||
|
||||
import codecs
|
||||
import configparser
|
||||
import contextlib
|
||||
import copy
|
||||
import datetime
|
||||
import pathlib
|
||||
|
|
@ -6369,6 +6370,32 @@ def test_should_not_rollover_non_file(self):
|
|||
self.assertFalse(rh.shouldRollover(self.next_rec()))
|
||||
rh.close()
|
||||
|
||||
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
|
||||
def test_should_not_rollover_named_pipe(self):
|
||||
# gh-143237 - test with non-seekable special file (named pipe)
|
||||
filename = os_helper.TESTFN
|
||||
self.addCleanup(os_helper.unlink, filename)
|
||||
try:
|
||||
os.mkfifo(filename)
|
||||
except PermissionError as e:
|
||||
self.skipTest('os.mkfifo(): %s' % e)
|
||||
|
||||
data = 'not read'
|
||||
def other_side():
|
||||
nonlocal data
|
||||
with open(filename, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
thread = threading.Thread(target=other_side)
|
||||
with threading_helper.start_threads([thread]):
|
||||
rh = logging.handlers.RotatingFileHandler(
|
||||
filename, encoding="utf-8", maxBytes=1)
|
||||
with contextlib.closing(rh):
|
||||
m = self.next_rec()
|
||||
self.assertFalse(rh.shouldRollover(m))
|
||||
rh.emit(m)
|
||||
self.assertEqual(data.decode(), m.msg + os.linesep)
|
||||
|
||||
def test_should_rollover(self):
|
||||
with open(self.fn, 'wb') as f:
|
||||
f.write(b'\n')
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Fix support of named pipes in the rotating :mod:`logging` handlers.
|
||||
Loading…
Add table
Add a link
Reference in a new issue