<div dir="ltr"><br><br><div class="gmail_quote">On Tue, May 19, 2015 at 7:30 AM Nick Coghlan <<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 19 May 2015 at 13:15, Steven D'Aprano <<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>> wrote:<br>
> So, I believe that the whole __future__ directive is a red herring, and<br>
> doesn't actually help Ram do what he wants, which is to write tests<br>
> which will fail if his generators call raise StopIteration regardless of<br>
> what version of Python he runs the test under.<br>
<br>
The essential impossibility of writing such tests is one of the<br>
underlying reasons *why* PEP 479 was accepted - you can't sensibly<br>
test for inadvertently escaping StopIteration values.<br>
<br>
However, I interpreted Ram's request slightly differently: if I'm<br>
understanding the request correctly, he'd like a way to write<br>
single-source modules such that *on Python 3.5+* they effectively run<br>
with "from __future__ import generator_stop", while on older Python<br>
versions, they run unmodified. That way, running the test suite under<br>
Python 3.5 will show that at least the regression tests aren't relying<br>
on "escaping StopIteration" in order to pass.<br>
<br>
The intended answer to Ram's request is "configure the warnings module<br>
to turn the otherwise silent deprecation warning into an error". From<br>
<a href="https://www.python.org/dev/peps/pep-0479/#transition-plan" target="_blank">https://www.python.org/dev/peps/pep-0479/#transition-plan</a>:<br>
<br>
* Python 3.5: Enable new semantics under __future__ import; silent<br>
deprecation warning if StopIteration bubbles out of a generator not<br>
under __future__ import.<br>
<br>
However, we missed the second half of that in the initial PEP<br>
implementation, so it doesn't currently emit the deprecation warning<br>
at all, which means there's no way to turn it into an error instead:<br>
<a href="http://bugs.python.org/issue24237" target="_blank">http://bugs.python.org/issue24237</a><br>
<br>
Once that issue has been fixed, then "-Wall" will cause any tests<br>
relying on the deprecated behaviour to fail, *without* needing to<br>
modify the code under test to use the future import.<br></blockquote><div><br></div><div>Another option is to use a custom import loader which sets the __future__ flag passed to compile() depending under what version of Python you were running your code under; overriding <a href="https://docs.python.org/3.5/library/importlib.html#importlib.abc.InspectLoader.source_to_code">https://docs.python.org/3.5/library/importlib.html#importlib.abc.InspectLoader.source_to_code</a> is all that would be needed to make that happen.</div></div></div>