Skip to content

Commit

Permalink
Adjust logging lock handling for Python 3.13
Browse files Browse the repository at this point in the history
python/cpython#109462 removed
`logging._acquireLock`.  Fortunately,
python/cpython#3385 simplified lock creation so
that `with logging._lock:` is sufficient; that PR made it into Python
3.7, which is already pastescript's minimum Python requirement.
  • Loading branch information
cjwatson committed Dec 8, 2024
1 parent ed45e56 commit 4f98831
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 33 deletions.
27 changes: 10 additions & 17 deletions paste/script/util/logging_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,13 @@ def fileConfig(fname, defaults=None):
formatters = _create_formatters(cp)

# critical section
logging._acquireLock()
try:
with logging._lock:
logging._handlers.clear()
if hasattr(logging, '_handlerList'):
del logging._handlerList[:]
# Handlers add themselves to logging._handlers
handlers = _install_handlers(cp, formatters)
_install_loggers(cp, handlers)
finally:
logging._releaseLock()


def _resolve(name):
Expand Down Expand Up @@ -327,9 +324,8 @@ class ConfigSocketReceiver(ThreadingTCPServer):
def __init__(self, host='localhost', port=DEFAULT_LOGGING_CONFIG_PORT,
handler=None):
ThreadingTCPServer.__init__(self, (host, port), handler)
logging._acquireLock()
self.abort = 0
logging._releaseLock()
with logging._lock:
self.abort = 0
self.timeout = 1

def serve_until_stopped(self):
Expand All @@ -341,16 +337,14 @@ def serve_until_stopped(self):
self.timeout)
if rd:
self.handle_request()
logging._acquireLock()
abort = self.abort
logging._releaseLock()
with logging._lock:
abort = self.abort

def serve(rcvr, hdlr, port):
server = rcvr(port=port, handler=hdlr)
global _listener
logging._acquireLock()
_listener = server
logging._releaseLock()
with logging._lock:
_listener = server
server.serve_until_stopped()

return threading.Thread(target=serve,
Expand All @@ -363,7 +357,6 @@ def stopListening():
"""
global _listener
if _listener:
logging._acquireLock()
_listener.abort = 1
_listener = None
logging._releaseLock()
with logging._lock:
_listener.abort = 1
_listener = None
20 changes: 4 additions & 16 deletions tests/test_logging_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,11 @@ def test4():
conf = globals()['config%d' % i]
sys.stdout.write('config%d: ' % i)
loggerDict = logging.getLogger().manager.loggerDict
logging._acquireLock()
try:
with logging._lock:
saved_handlers = logging._handlers.copy()
if hasattr(logging, '_handlerList'):
saved_handler_list = logging._handlerList[:]
saved_loggers = loggerDict.copy()
finally:
logging._releaseLock()
try:
fn = tempfile.mktemp(".ini")
f = open(fn, "w")
Expand All @@ -146,17 +143,14 @@ def test4():
message('ok.')
os.remove(fn)
finally:
logging._acquireLock()
try:
with logging._lock:
logging._handlers.clear()
logging._handlers.update(saved_handlers)
if hasattr(logging, '_handlerList'):
logging._handlerList[:] = saved_handler_list
loggerDict = logging.getLogger().manager.loggerDict
loggerDict.clear()
loggerDict.update(saved_loggers)
finally:
logging._releaseLock()

#----------------------------------------------------------------------------
# Test 5
Expand Down Expand Up @@ -196,14 +190,11 @@ def formatException(self, ei):

def test5():
loggerDict = logging.getLogger().manager.loggerDict
logging._acquireLock()
try:
with logging._lock:
saved_handlers = logging._handlers.copy()
if hasattr(logging, '_handlerList'):
saved_handler_list = logging._handlerList[:]
saved_loggers = loggerDict.copy()
finally:
logging._releaseLock()
try:
fn = tempfile.mktemp(".ini")
f = open(fn, "w")
Expand All @@ -218,14 +209,11 @@ def test5():
hdlr = logging.getLogger().handlers[0]
logging.getLogger().handlers.remove(hdlr)
finally:
logging._acquireLock()
try:
with logging._lock:
logging._handlers.clear()
logging._handlers.update(saved_handlers)
if hasattr(logging, '_handlerList'):
logging._handlerList[:] = saved_handler_list
loggerDict = logging.getLogger().manager.loggerDict
loggerDict.clear()
loggerDict.update(saved_loggers)
finally:
logging._releaseLock()

0 comments on commit 4f98831

Please sign in to comment.