Skip to content

Commit 2953004

Browse files
authored
Update logging to 3.14.4 (RustPython#7820)
* Update `test_logging.py` to 3.14.4 * Update `logging` to 3.14.4 /
1 parent 5b0177d commit 2953004

4 files changed

Lines changed: 210 additions & 70 deletions

File tree

Lib/logging/__init__.py

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,8 +1475,6 @@ class Logger(Filterer):
14751475
level, and "input.csv", "input.xls" and "input.gnu" for the sub-levels.
14761476
There is no arbitrary limit to the depth of nesting.
14771477
"""
1478-
_tls = threading.local()
1479-
14801478
def __init__(self, name, level=NOTSET):
14811479
"""
14821480
Initialize the logger with a name and an optional level.
@@ -1673,19 +1671,14 @@ def handle(self, record):
16731671
This method is used for unpickled records received from a socket, as
16741672
well as those created locally. Logger-level filtering is applied.
16751673
"""
1676-
if self._is_disabled():
1674+
if self.disabled:
16771675
return
1678-
1679-
self._tls.in_progress = True
1680-
try:
1681-
maybe_record = self.filter(record)
1682-
if not maybe_record:
1683-
return
1684-
if isinstance(maybe_record, LogRecord):
1685-
record = maybe_record
1686-
self.callHandlers(record)
1687-
finally:
1688-
self._tls.in_progress = False
1676+
maybe_record = self.filter(record)
1677+
if not maybe_record:
1678+
return
1679+
if isinstance(maybe_record, LogRecord):
1680+
record = maybe_record
1681+
self.callHandlers(record)
16891682

16901683
def addHandler(self, hdlr):
16911684
"""
@@ -1773,7 +1766,7 @@ def isEnabledFor(self, level):
17731766
"""
17741767
Is this logger enabled for level 'level'?
17751768
"""
1776-
if self._is_disabled():
1769+
if self.disabled:
17771770
return False
17781771

17791772
try:
@@ -1823,11 +1816,6 @@ def _hierlevel(logger):
18231816
if isinstance(item, Logger) and item.parent is self and
18241817
_hierlevel(item) == 1 + _hierlevel(item.parent))
18251818

1826-
def _is_disabled(self):
1827-
# We need to use getattr as it will only be set the first time a log
1828-
# message is recorded on any given thread
1829-
return self.disabled or getattr(self._tls, 'in_progress', False)
1830-
18311819
def __repr__(self):
18321820
level = getLevelName(self.getEffectiveLevel())
18331821
return '<%s %s (%s)>' % (self.__class__.__name__, self.name, level)
@@ -1864,9 +1852,9 @@ class LoggerAdapter(object):
18641852

18651853
def __init__(self, logger, extra=None, merge_extra=False):
18661854
"""
1867-
Initialize the adapter with a logger and a dict-like object which
1868-
provides contextual information. This constructor signature allows
1869-
easy stacking of LoggerAdapters, if so desired.
1855+
Initialize the adapter with a logger and an optional dict-like object
1856+
which provides contextual information. This constructor signature
1857+
allows easy stacking of LoggerAdapters, if so desired.
18701858
18711859
You can effectively pass keyword arguments as shown in the
18721860
following example:
@@ -1897,8 +1885,9 @@ def process(self, msg, kwargs):
18971885
Normally, you'll only need to override this one method in a
18981886
LoggerAdapter subclass for your specific needs.
18991887
"""
1900-
if self.merge_extra and "extra" in kwargs:
1901-
kwargs["extra"] = {**self.extra, **kwargs["extra"]}
1888+
if self.merge_extra and kwargs.get("extra") is not None:
1889+
if self.extra is not None:
1890+
kwargs["extra"] = {**self.extra, **kwargs["extra"]}
19021891
else:
19031892
kwargs["extra"] = self.extra
19041893
return msg, kwargs

Lib/logging/config.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,8 @@ def configure_handler(self, config):
865865
else:
866866
factory = klass
867867
kwargs = {k: config[k] for k in config if (k != '.' and valid_ident(k))}
868+
# When deprecation ends for using the 'strm' parameter, remove the
869+
# "except TypeError ..."
868870
try:
869871
result = factory(**kwargs)
870872
except TypeError as te:
@@ -876,6 +878,15 @@ def configure_handler(self, config):
876878
#(e.g. by Django)
877879
kwargs['strm'] = kwargs.pop('stream')
878880
result = factory(**kwargs)
881+
882+
import warnings
883+
warnings.warn(
884+
"Support for custom logging handlers with the 'strm' argument "
885+
"is deprecated and scheduled for removal in Python 3.16. "
886+
"Define handlers with the 'stream' argument instead.",
887+
DeprecationWarning,
888+
stacklevel=2,
889+
)
879890
if formatter:
880891
result.setFormatter(formatter)
881892
if level is not None:
@@ -1006,7 +1017,8 @@ class ConfigSocketReceiver(ThreadingTCPServer):
10061017
A simple TCP socket-based logging config receiver.
10071018
"""
10081019

1009-
allow_reuse_address = 1
1020+
allow_reuse_address = True
1021+
allow_reuse_port = False
10101022

10111023
def __init__(self, host='localhost', port=DEFAULT_LOGGING_CONFIG_PORT,
10121024
handler=None, ready=None, verify=None):

Lib/logging/handlers.py

Lines changed: 22 additions & 2 deletions
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
@@ -855,7 +859,7 @@ class SysLogHandler(logging.Handler):
855859
}
856860

857861
def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
858-
facility=LOG_USER, socktype=None):
862+
facility=LOG_USER, socktype=None, timeout=None):
859863
"""
860864
Initialize a handler.
861865
@@ -872,6 +876,7 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
872876
self.address = address
873877
self.facility = facility
874878
self.socktype = socktype
879+
self.timeout = timeout
875880
self.socket = None
876881
self.createSocket()
877882

@@ -933,6 +938,8 @@ def createSocket(self):
933938
err = sock = None
934939
try:
935940
sock = socket.socket(af, socktype, proto)
941+
if self.timeout:
942+
sock.settimeout(self.timeout)
936943
if socktype == socket.SOCK_STREAM:
937944
sock.connect(sa)
938945
break
@@ -1529,6 +1536,19 @@ def __init__(self, queue, *handlers, respect_handler_level=False):
15291536
self._thread = None
15301537
self.respect_handler_level = respect_handler_level
15311538

1539+
def __enter__(self):
1540+
"""
1541+
For use as a context manager. Starts the listener.
1542+
"""
1543+
self.start()
1544+
return self
1545+
1546+
def __exit__(self, *args):
1547+
"""
1548+
For use as a context manager. Stops the listener.
1549+
"""
1550+
self.stop()
1551+
15321552
def dequeue(self, block):
15331553
"""
15341554
Dequeue a record and return it, optionally blocking.

0 commit comments

Comments
 (0)