[Python-ideas] Making it easy to prepare for PEP479

Steven D'Aprano steve at pearwood.info
Mon May 18 19:13:20 CEST 2015


On Mon, May 18, 2015 at 11:24:06AM -0400, Terry Reedy wrote:
> On 5/18/2015 10:52 AM, Steven D'Aprano wrote:
> >On Mon, May 18, 2015 at 10:17:06AM -0400, Terry Reedy wrote:
> >
> >>Try the following: add the future statement to the top of modules with
> >>generators, compile with 3.5, and when successful, comment-out the
> >>statement.  For continued testing, especially with multiple authors,
> >>write functions to un-comment and re-comment a file.  In the test file:
> >>
> >>if <3.5>: uncomment('xyz')  # triggers re-compile on import
> >>import xyz
> >>if <3.5>: recomment('xyz')  # ditto,
> >>
> >>If this works, put pep479_helper on pypi.
> 
> >I'm not entirely sure what you are trying to do,
> 
> Solve the OP's problem.  What are *you* trying to do?  If you do not 
> think that the offered solution will work, please explain why, instead 
> of diverting attention to some insane projection of yours.

You call my comments an "insane projection", but it's your code snippet 
which does exactly what I warned against: first you modify the source 
code, compile and import using the new, modified source, then change the 
source back to the way it was before the import so that what's inside 
the byte code no longer matches what's in the source. Here it is again:

    In the test file:

    if <3.5>: uncomment('xyz')  # triggers re-compile on import
    import xyz
    if <3.5>: recomment('xyz')  # ditto,

In other words: edit source, compile, revert source, use compiled 
version. See the problem now?

You might say, "But it's only a single line that is different." I say, 
*any* difference is too much. I've been burnt too badly by people using 
"clever hacks" that lead to the .pyc file being imported and the .py 
source being out of sync to trust even a single line difference.

If I interpret your words as you wrote them, the solution seems to risk 
becoming as convoluted and messy as the code I had to work with in real 
life. If I try to interpret your words more sensibly ("surely Terry 
cannot possibly mean what he said...?") the suggestion is *still* 
convoluted. If Ram is permitted multiple test files, then the simplest 
solution is to split off the code that relies on the future directive 
into its own file:

try:
    import xyz  # requires the future directive
except ImportError:
    xyz = None

if xyz:
    # tests with directive
else:
    # tests without


Instead of going through your process of editing the source code, 
compiling, importing, re-editing, just have two source files.



-- 
Steve


More information about the Python-ideas mailing list