Page MenuHomePhabricator

behaviour of pywikibot.exception() should follow Python logging.exception()
Closed, ResolvedPublicFeature

Description

Feature summary:
Python logging.exception() uses exc_info=True by default [1] which means the traceback is shown whereas Pywikibot pywikibot.logging.exception() (also available as pywikibot.exception()) uses tb=False by default. There is not significant difference between pywikibot.exception() and pywkibot.error() except that the pywikibot variant of exception is able to get the exception from sys.exc_info() and the msg parameter may be omitted.

The behaviour of pywikibot.exception() should be in sync with the underlying Python implementation and the tb parameter should be True by default.

Use case(s):
Currently there is no difference between pywikibot.error() and pywikibot.exception() if the exception is given as message:

import pywikibot
try:
    1/0
except Exception as e:
    pywikibot.error(e)

ERROR: division by zero

try:
    1/0
except Exception as e:
    pywikibot.error(e, exc_info=True)

ERROR: division by zero
Traceback (most recent call last):
  File "<pyshell#7>", line 2, in <module>
ZeroDivisionError: division by zero
try:
    1/0
except Exception as e:
    pywikibot.exception(e)

ERROR: division by zero

try:
    1/0
except Exception as e:
    pywikibot.exception(e, tb=True)

    
ERROR: division by zero
Traceback (most recent call last):
  File "<pyshell#11>", line 2, in <module>
ZeroDivisionError: division by zero

try:
    1/0
except Exception as e:
    pywikibot.exception(e, exc_info=True)

ERROR: division by zero
Traceback (most recent call last):
  File "<pyshell#13>", line 2, in <module>
ZeroDivisionError: division by zero

Benefits:
The behaviour for pywikibot.error() should be differ from pywikibot.exception() and follow the underlying Python implementation..

I propose to change this behaviour with Pywikibot 8 because ist is a breaking change but I don't see any important impact to have a longer deprecation period here.

[1] https://docs.python.org/3/library/logging.html#logging.exception

Event Timeline

Xqt renamed this task from behaviour of pywikibot.exception() shoul follow Python logging.exception() to behaviour of pywikibot.exception() should follow Python logging.exception().Apr 24 2022, 3:17 PM

Change 787065 had a related patch set uploaded (by Xqt; author: Xqt):

[pywikibot/core@master] [IMPR] use exc_info=True with pywikibot.exception() by default

https://gerrit.wikimedia.org/r/787065

Change 787065 merged by jenkins-bot:

[pywikibot/core@master] [IMPR] use exc_info=True with pywikibot.exception() by default

https://gerrit.wikimedia.org/r/787065