[New-bugs-announce] [issue17146] Improve test.support.import_fresh_module()

Eric Snow report at bugs.python.org
Wed Feb 6 18:53:27 CET 2013

New submission from Eric Snow:

(+nosy list from #17037)

In issue 17037 (related to hiding PEP 399 boilerplate), Ezio and Eli recommended improving on the API of import_fresh_module().  I agree that it could be simplified, wrapped, or otherwise improved.

import_fresh_module() is used in the stdlib tests a number of times, primarily to support testing dual-implementations a la PEP 399 (see msg180809).  However, since it is already used for testing at least once module for a non-PEP-399 case (test_bz2), we need to be careful about any changes we'd make.  Changing test_bz2 to not use import_fresh_module() just so we could make it PEP-399 specific doesn't seem right.

Of the test modules that use import_fresh_module(), several pass multiple names for "fresh" and none pass more than one for "blocked".  Presumably a Python module may be accelerated by more than one accelerator module, hence allowing for lists for the two parameters, but none do so *now*.

Consequently, it may be worth doing something like what Eli suggested in msg181515: add another helper function that *is* PEP-399-specific.  Something like this:

def import_pure_and_accelerated_modules(name, *, fresh=(),
    if accelerators is None:
        accelerators = ['_' + name]
    if accelerated_fresh is None:
        accelerated_fresh = list(fresh) + list(accelerators)
    py_module = import_fresh_module(name, fresh=fresh, blocked=accelerators)
    acc_module = import_fresh_module(name, fresh=accelerated_fresh)
    return py_module, acc_module

Then you could use it like this:
py_module, c_module = import_pure_and_accelerated_modules('module')
py_module, c_module = import_pure_and_accelerated_modules('module', accelerators=['_other'])

Simply refactoring import_fresh_module() to work this way is an option, especially since it is used almost exclusively for PEP 399 compliance.  That way test.support would not grow yet another function.  The new interface would probably look like this:

def import_fresh_module(name, *, fresh=(), accelerated_fresh=None,
                        accelerators=None, blocked=None, deprecated=False):

It would still return a 2-tuple, but the second module would be None.  For the test_bz2 case, the change would be minimal:

bz2, = support.import_fresh_module("bz2", blocked=("threading",))

Why go to all this trouble?  It saves just one line of code.  Well, it also helps with writing and maintenance of dual-implementation tests, which I expect will be a growing segment of the stdlib unit tests as time goes by.

However, ultimately the point it moot if we have a more comprehensive test helper like what's being discussed in issue 17037.

components: Library (Lib)
messages: 181545
nosy: Arfrever, brett.cannon, eli.bendersky, eric.snow, ezio.melotti, pitrou
priority: normal
severity: normal
stage: needs patch
status: open
title: Improve test.support.import_fresh_module()
type: enhancement
versions: Python 3.4

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list