classification
Title: doctest terminates when accessing __wrapped__ raises an error
Type: behavior Stage: patch review
Components: Tests Versions: Python 3.6, Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Lasse Schuirmann, Tiger-222, jstasiak, r.david.murray, steven.daprano
Priority: normal Keywords: patch

Created on 2016-01-02 22:30 by Lasse Schuirmann, last changed 2019-07-13 16:45 by Tiger-222.

Files
File name Uploaded Description Edit
shell Lasse Schuirmann, 2016-01-02 22:30 The sample shell session to reproduce
unwrapable_ob.py Tiger-222, 2019-07-13 16:45
Pull Requests
URL Status Linked Edit
PR 14756 open Tiger-222, 2019-07-13 16:43
Messages (8)
msg257378 - (view) Author: Lasse Schuirmann (Lasse Schuirmann) * Date: 2016-01-02 22:30
You can see this when importing the Flask `request` object in a file that is doctested. The `request` object will throw a RuntimeError when one tries to access any attribute. Doctest tries to `inspect.unwrap` all objects in the file in order to find out if they're doctestable functions - and apparently only catches the `AttributeError` exception. Thus it crashes :)
msg257379 - (view) Author: Lasse Schuirmann (Lasse Schuirmann) * Date: 2016-01-02 22:35
(Closed) issue at the flask repository of this: https://github.com/mitsuhiko/flask/issues/1680

Also IIRC this worked back with Python 3.4 but do not consider this a fact yet. (At least the tests ran on Fedora...)
msg257387 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-01-02 23:43
doctest doesn't crash -- it is a regular exception, not a crash. "Crash" means "Hard crashes of the Python interpreter – possibly with a core dump or a Windows error box." In other words, a segmentation fault or other low-level crash, not an exception.

I'm not convinced that this is a problem with doctest, it looks to me like a broken-by-design flaw in requests. If you try to access an attribute that doesn't exist, you should get AttributeError, not RuntimeError. I don't understand the justification given on the requests tracker for why the attribute access fails, but I would expect that any exception other than AttributeError, or a subclass of such, is a clear bug in requests.

I don't think it is doctest's responsibility to have special handling for an ill-designed proxy class, but I'll hold off closing the ticket for a few days in case anyone makes a good argument for why this is a doctest bug.
msg257467 - (view) Author: Lasse Schuirmann (Lasse Schuirmann) * Date: 2016-01-04 16:51
Sorry for giving it the wrong category.

I personally think both projects should solve this. IMO it would be nice if doctest is robust against modules that for some reason (be they ill designed or not; I guess if you search a lot you find a project that needs to raise a custom exception when accessing `__wrapped__`). At the same time I do think the Flask request object could handle this better but lack the ability to explain that to them with substantial reasoning.

The problem is that both of you don't want to solve this problem at their side, @steven.daprano, would you mind speaking with the Flask people directly? I'm not affiliated to them.
msg257478 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-01-04 18:36
No, I don't think doctest should be made robust against RuntimeErrors.  doctest's purpose is to check for bugs, and IMO this is a bug in flask.  In particular, it is not doctest that flask is not cooperating with, it is hasattr.  It used to be that hasattr would mask errors raised, and this caused a number of both debugging and production problems, to the point where hasattr was not considered safe to use.  But it eventually got fixed: http://www.gossamer-threads.com/lists/python/dev/856859
msg257479 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-01-04 18:42
Gah. Having hit send I of course had second thoughts.  doctest is suppose to *report* bugs, so there *is* a doctest bug here: it should be capturing that and probably other errors and reporting them, instead of just producing a traceback, I think.
msg347791 - (view) Author: Mickaël Schoentgen (Tiger-222) * Date: 2019-07-13 09:46
Working on it at EuroPython 2019.

FTR I tested on Python 3.6.9, 3.7.4 and 3.9.0a0 without issue.
I think this was fixed with https://github.com/rtweeks/werkzeug/commit/c643980b93e9bde4431cd99ece30f07e00160f84.

But the doctest issue remains and this is on what I will focus.
msg347845 - (view) Author: Mickaël Schoentgen (Tiger-222) * Date: 2019-07-13 16:45
Attached a simple reproduction script.
History
Date User Action Args
2019-07-13 16:45:13Tiger-222setfiles: + unwrapable_ob.py

messages: + msg347845
2019-07-13 16:43:05Tiger-222setkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request14551
2019-07-13 09:46:03Tiger-222setnosy: + Tiger-222
messages: + msg347791
2016-05-11 10:35:22jstasiaksetnosy: + jstasiak
2016-01-04 18:42:43r.david.murraysetstatus: closed -> open
versions: + Python 3.6
title: doctest crashes when accessing __wrapped__ fails other than AttributeError -> doctest terminates when accessing __wrapped__ raises an error
messages: + msg257479

resolution: third party ->
stage: resolved -> needs patch
2016-01-04 18:36:22r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg257478

resolution: third party
stage: resolved
2016-01-04 16:51:45Lasse Schuirmannsetmessages: + msg257467
2016-01-02 23:43:35steven.dapranosettype: crash -> behavior

messages: + msg257387
nosy: + steven.daprano
2016-01-02 22:35:33Lasse Schuirmannsetmessages: + msg257379
2016-01-02 22:30:43Lasse Schuirmanncreate