[Distutils] Possible bug in setuptools - test command?

Iwan Vosloo iwan at reahl.org
Mon May 11 13:53:34 CEST 2009


Hi there,

We've been having some trouble with a script which invokes
setuptools.setup(script_args=['test']) more than once in a single
process.

During each invocation, python imports different, new instances of the
same module.

We've narrowed it down to setuptools/command/test.py, in the method
with_project_on_sys_path:

It saves sys.modules like this:

        old_modules = sys.modules.copy()

Then does its thing and restores sys.modules like this:

            sys.modules.clear()
            sys.modules.update(old_modules)

But, as the code below illustrates, this results in multiple imports of
the same modules:

import sys

def i():
    old_modules = sys.modules.copy()

    from decimal import Decimal
    
    sys.modules.clear()
    sys.modules.update(old_modules)

    return Decimal


assert i() is i(), 'Two different Decimal instances returned'  # Fails


Is this a bug?  (We are on version 0.6c8 of setuptools, python 2.5)

There is an easy solution:

Save sys.modules like before:
        old_modules = sys.modules.copy()

But restore it like this:
    sys.modules = old_modules

That seems to fix our problem (not sure of other effects though).

Regards
- Iwan Vosloo



More information about the Distutils-SIG mailing list