This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: cgitb file to print OSError exceptions
Type: Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: hardkrash, iritkatriel, r.david.murray
Priority: normal Keywords:

Created on 2018-01-25 19:03 by hardkrash, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg310702 - (view) Author: steven Michalske (hardkrash) Date: 2018-01-25 19:03
With the source

```
import cgitb
import sys

try:
    f = open('non_exitant_file_path.foo')
except Exception as e:
    cgitb.text(sys.exc_info())
```

we get the output

```
Traceback (most recent call last):
  File "foo.py", line 22, in <module>
    f = open('non_exitant_file_path.foo')
FileNotFoundError: [Errno 2] No such file or directory: 'non_exitant_file_path.foo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "foo.py", line 24, in <module>
    cgitb.text(sys.exc_info())
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/cgitb.py", line 245, in text
    value = pydoc.text.repr(getattr(evalue, name))
AttributeError: characters_written
```

In bug #30554 it was brought up that there are attributes designed as invariant os some other as designed behavior.  Unfortunately it was closed "as designed" 

So this means that cgitb has a bug when text formatting a sys_exc traceback of an OSError.

This is hidden in the by the hook with this code form cgitb.py
by falling back to the standard formatter...

line 277
```
        try:
            doc = formatter(info, self.context)
        except:                         # just in case something goes wrong
            doc = ''.join(traceback.format_exception(*info))
            plain = True
```
msg310707 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-01-25 21:05
Yep, this one might get closed as "as designed", too ;)  cgitb has to cope with something going wrong with trying to print out values, because there are a number of ways to break that in Python, not just the one you are pointing to.

If you have a thought about how to move the exception handling deeper into the module in a generalized way, we could consider that option.
msg415056 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-03-13 17:33
cgi/cgitb are deprecated as per PEP 594, so there won't be further enhancements to them.
History
Date User Action Args
2022-04-11 14:58:57adminsetgithub: 76850
2022-03-13 17:33:40iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg415056

resolution: wont fix
stage: resolved
2018-01-25 21:05:40r.david.murraysetnosy: + r.david.murray
messages: + msg310707
2018-01-25 19:03:08hardkrashcreate