From 013e659e495796df77aae4d33b67995f9793f454 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Sat, 27 Aug 2022 12:13:19 +0100 Subject: =?UTF-8?q?gh-92007:=20Handle=20elevation=20errors=20in=20NTEventL?= =?UTF-8?q?ogHandler=20more=20grace=E2=80=A6=20(GH-96322)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lib/logging/handlers.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py index 3f97862..b4c5c13 100644 --- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -1111,7 +1111,16 @@ class NTEventLogHandler(logging.Handler): dllname = os.path.join(dllname[0], r'win32service.pyd') self.dllname = dllname self.logtype = logtype - self._welu.AddSourceToRegistry(appname, dllname, logtype) + # Administrative privileges are required to add a source to the registry. + # This may not be available for a user that just wants to add to an + # existing source - handle this specific case. + try: + self._welu.AddSourceToRegistry(appname, dllname, logtype) + except Exception as e: + # This will probably be a pywintypes.error. Only raise if it's not + # an "access denied" error, else let it pass + if getattr(e, 'winerror', None) != 5: # not access denied + raise self.deftype = win32evtlog.EVENTLOG_ERROR_TYPE self.typemap = { logging.DEBUG : win32evtlog.EVENTLOG_INFORMATION_TYPE, -- cgit v0.12