bpo-27165: Skip callables when printing an exception details by BookLaugh · Pull Request #9695 · python/cpython (original) (raw)

Thanks for the PR. I think a test and a NEWS entry can help here. You can add a test to https://github.com/python/cpython/blob/master/Lib/test/test_cgitb.py. A sample test I tried with the PR as below but feel free to add in relevant assertions.

You can find more about NEWS entry here : https://devguide.python.org/committing/#what-s-new-and-news-entries

Sample test

def test_text_text(self): try: raise Exception("foo") except Exception as e: text = cgitb.text(sys.exc_info()) print(text) self.assertNotIn('init', text) self.assertNotIn('reduce', text)

On master this fails

➜  cpython git:(master) ✗ ./python.exe -m unittest -v test.test_cgitb.TestCgitb.test_text_text
test_text_text (test.test_cgitb.TestCgitb) ... Exception
Python 3.8.0a0: /Users/karthikeyansingaravelan/stuff/python/cpython/python.exe
Thu Oct  4 17:06:16 2018

A problem occurred in a Python script.  Here is the sequence of
function calls leading up to the error, in the order they occurred.

 /Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_cgitb.py in test_text_text(self=<test.test_cgitb.TestCgitb testMethod=test_text_text>)
   66     def test_text_text(self):
   67         try:
   68             raise Exception("foo")
   69         except Exception as e:
   70             text = cgitb.text(sys.exc_info())
builtinException = <class 'Exception'>
Exception: foo
    __cause__ = None
    __class__ = <class 'Exception'>
    __context__ = None
    __delattr__ = <method-wrapper '__delattr__' of Exception object>
    __dict__ = {}
    __dir__ = <built-in method __dir__ of Exception object>
    __doc__ = 'Common base class for all non-exit exceptions.'
    __eq__ = <method-wrapper '__eq__' of Exception object>
    __format__ = <built-in method __format__ of Exception object>
    __ge__ = <method-wrapper '__ge__' of Exception object>
    __getattribute__ = <method-wrapper '__getattribute__' of Exception object>
    __gt__ = <method-wrapper '__gt__' of Exception object>
    __hash__ = <method-wrapper '__hash__' of Exception object>
    __init__ = <method-wrapper '__init__' of Exception object>
    __init_subclass__ = <built-in method __init_subclass__ of type object>
    __le__ = <method-wrapper '__le__' of Exception object>
    __lt__ = <method-wrapper '__lt__' of Exception object>
    __ne__ = <method-wrapper '__ne__' of Exception object>
    __new__ = <built-in method __new__ of type object>
    __reduce__ = <built-in method __reduce__ of Exception object>
    __reduce_ex__ = <built-in method __reduce_ex__ of Exception object>
    __repr__ = <method-wrapper '__repr__' of Exception object>
    __setattr__ = <method-wrapper '__setattr__' of Exception object>
    __setstate__ = <built-in method __setstate__ of Exception object>
    __sizeof__ = <built-in method __sizeof__ of Exception object>
    __str__ = <method-wrapper '__str__' of Exception object>
    __subclasshook__ = <built-in method __subclasshook__ of type object>
    __suppress_context__ = False
    __traceback__ = <traceback object>
    args = ('foo',)
    with_traceback = <built-in method with_traceback of Exception object>

The above is a description of an error in a Python program.  Here is
the original traceback:

Traceback (most recent call last):
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_cgitb.py", line 68, in test_text_text
    raise Exception("foo")
Exception: foo


FAIL

======================================================================
FAIL: test_text_text (test.test_cgitb.TestCgitb)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_cgitb.py", line 68, in test_text_text
    raise Exception("foo")
Exception: foo

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_cgitb.py", line 72, in test_text_text
    self.assertNotIn('__init__', text)
AssertionError: '__init__' unexpectedly found in 'Exception\nPython 3.8.0a0: /Users/karthikeyansingaravelan/stuff/python/cpython/python.exe\nThu Oct  4 17:06:16 2018\n\nA problem occurred in a Python script.  Here is the sequence of\nfunction calls leading up to the error, in the order they occurred.\n\n /Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_cgitb.py in test_text_text(self=<test.test_cgitb.TestCgitb testMethod=test_text_text>)\n   66     def test_text_text(self):\n   67         try:\n   68             raise Exception("foo")\n   69         except Exception as e:\n   70             text = cgitb.text(sys.exc_info())\nbuiltinException = <class \'Exception\'>\nException: foo\n    __cause__ = None\n    __class__ = <class \'Exception\'>\n    __context__ = None\n    __delattr__ = <method-wrapper \'__delattr__\' of Exception object>\n    __dict__ = {}\n    __dir__ = <built-in method __dir__ of Exception object>\n    __doc__ = \'Common base class for all non-exit exceptions.\'\n    __eq__ = <method-wrapper \'__eq__\' of Exception object>\n    __format__ = <built-in method __format__ of Exception object>\n    __ge__ = <method-wrapper \'__ge__\' of Exception object>\n    __getattribute__ = <method-wrapper \'__getattribute__\' of Exception object>\n    __gt__ = <method-wrapper \'__gt__\' of Exception object>\n    __hash__ = <method-wrapper \'__hash__\' of Exception object>\n    __init__ = <method-wrapper \'__init__\' of Exception object>\n    __init_subclass__ = <built-in method __init_subclass__ of type object>\n    __le__ = <method-wrapper \'__le__\' of Exception object>\n    __lt__ = <method-wrapper \'__lt__\' of Exception object>\n    __ne__ = <method-wrapper \'__ne__\' of Exception object>\n    __new__ = <built-in method __new__ of type object>\n    __reduce__ = <built-in method __reduce__ of Exception object>\n    __reduce_ex__ = <built-in method __reduce_ex__ of Exception object>\n    __repr__ = <method-wrapper \'__repr__\' of Exception object>\n    __setattr__ = <method-wrapper \'__setattr__\' of Exception object>\n    __setstate__ = <built-in method __setstate__ of Exception object>\n    __sizeof__ = <built-in method __sizeof__ of Exception object>\n    __str__ = <method-wrapper \'__str__\' of Exception object>\n    __subclasshook__ = <built-in method __subclasshook__ of type object>\n    __suppress_context__ = False\n    __traceback__ = <traceback object>\n    args = (\'foo\',)\n    with_traceback = <built-in method with_traceback of Exception object>\n\nThe above is a description of an error in a Python program.  Here is\nthe original traceback:\n\nTraceback (most recent call last):\n  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_cgitb.py", line 68, in test_text_text\n    raise Exception("foo")\nException: foo\n\n'

With PR this passes

➜  cpython git:(master) ✗ git pr 9695
➜  cpython git:(master) ✗ git checkout pr_9695 Lib/cgitb.py
➜  cpython git:(master) ✗ ./python.exe -m unittest -v test.test_cgitb.TestCgitb.test_text_text
test_text_text (test.test_cgitb.TestCgitb) ... Exception
Python 3.8.0a0: /Users/karthikeyansingaravelan/stuff/python/cpython/python.exe
Thu Oct  4 17:07:38 2018

A problem occurred in a Python script.  Here is the sequence of
function calls leading up to the error, in the order they occurred.

 /Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_cgitb.py in test_text_text(self=<test.test_cgitb.TestCgitb testMethod=test_text_text>)
   66     def test_text_text(self):
   67         try:
   68             raise Exception("foo")
   69         except Exception as e:
   70             text = cgitb.text(sys.exc_info())
builtinException = <class 'Exception'>
Exception: foo
    __cause__ = None
    __context__ = None
    __dict__ = {}
    __doc__ = 'Common base class for all non-exit exceptions.'
    __suppress_context__ = False
    __traceback__ = <traceback object>
    args = ('foo',)

The above is a description of an error in a Python program.  Here is
the original traceback:

Traceback (most recent call last):
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_cgitb.py", line 68, in test_text_text
    raise Exception("foo")
Exception: foo


ok

----------------------------------------------------------------------
Ran 1 test in 0.093s

OK

Hope this helps