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: doctest cause warnings in tests using generators
Type: behavior Stage: needs patch
Components: Tests Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, ncoghlan, oluensdorf, terry.reedy
Priority: normal Keywords:

Created on 2014-04-08 10:49 by oluensdorf, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
doctest.txt oluensdorf, 2014-04-08 10:49 doctest with spurious warning
doctest.txt oluensdorf, 2014-04-08 12:11 corrected doctest
doctest.txt oluensdorf, 2014-04-12 08:22 simplified doctest
Messages (5)
msg215750 - (view) Author: Ontje Lünsdorf (oluensdorf) Date: 2014-04-08 10:49
The attached doctest raises a warning since Python 3.4:

$ python -m doctest doctest.txt
Exception ignored in: <generator object foo at 0x7f9ee38aaca8>
Traceback (most recent call last):
  File "<doctest doctest.txt[1]>", line 4, in foo
NameError: name 'socket' is not defined

My guess is that doctest cleans up globals() of the test snippet (thereby deleting socket). Afterwards python cleans up the generator by sending in a GeneratorExit exception. The except block will now fail because socket is not available anymore.
msg215755 - (view) Author: Ontje Lünsdorf (oluensdorf) Date: 2014-04-08 12:11
Sorry, the test does not really expose the problem with Python 3.4. Here is an updated example.

Python 2.7 and 3.3 succeed without warnings, while Python 3.4 warns about an ignored exception.
msg215948 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-04-11 19:22
I not am not sure I see a bug here, a discrepancy between doc and behavior. Even if not, you may have a legitimate enhancement request.

3.4 now warns about ignored exceptions during shutdown in case there is a fixable bug in the code being shut down. This is intended to be a feature, not a problem. I don't know if the warnings can be suppressed.

3.4 also implemented PEP 442 so "objects with __del__() methods, as well as generators with finally clauses, can be finalized when they are part of a reference cycle." Your second patch puts a generator in a reference cycle. Generators have a .__del__ method. Perhaps its body is the equivalent of "self.throw(GeneratorExit)". If so, that could be wrapped with the equivalent of "try:...except BaseException: pass" to avoid leaking exceptions.
msg215965 - (view) Author: Ontje Lünsdorf (oluensdorf) Date: 2014-04-12 08:22
I think there may be a bug in doctest which is getting exposed by Python 3.4 new handling of reference cycles. I've attached a simpler example with a global variable.

There's no error if you run the code directly, the global variable still exists during the generator exit. If you run it with doctest in Python 3.4 you get the warning because the global variable has been deleted. In this corner case, doctest execution seem to differ from Python.
msg408239 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-10 18:23
Reproduced on 3.11.
History
Date User Action Args
2022-04-11 14:58:01adminsetgithub: 65377
2021-12-10 18:23:29iritkatrielsetnosy: + iritkatriel

messages: + msg408239
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.4, Python 3.5
2014-04-12 08:22:41oluensdorfsetfiles: + doctest.txt

messages: + msg215965
2014-04-11 19:22:28terry.reedysetversions: + Python 3.5
nosy: + terry.reedy, ncoghlan

messages: + msg215948

stage: needs patch
2014-04-08 12:11:32oluensdorfsetfiles: + doctest.txt

messages: + msg215755
2014-04-08 10:49:06oluensdorfcreate