[Python-Dev] More joy with test_strptime

Tim Peters tim.peters at gmail.com
Tue Jul 13 07:28:48 CEST 2004


[Tim]
> FWIW, this is a minimal failing set of tests to run:
>
> test___all__
> test_site
> test_strptime

More, if you comment out the

        self.check_all("_strptime")

line in test__all__, the test passes.  If you comment out every
self.check_all line except for that one, the test fails.  If you add
this line after that one, the test passes:

        del sys.modules["_strptime"]

The regrtest framework itself tries to "unload" modules imported by
tests, but it only does so for modules from the test *package*.  So
_strptime remains loaded after test___all__, with the time module it
had at that time.  I really don't grok what test_site is doing, so am
at a loss to explain how it manages to create a distinct time module.

Ah!  It doesn't!  It's because PthFile.cleanup does

                del sys.modules[self.imported]

That destroys the time module that was in sys.modules before test_site
began running.  That's the problem.  Then every module that previously
imported time via test___all__ has a time module distinct from any
time module imported after test_site.

Get rid of that line.  Or if, you want to be anally correct, nuke
self.imported in cleanup() if and only if

    self.imported not in sys.modules

before PthFile imported self.imported

Note that the

        assert self.imported in sys.modules

in test() doesn't do anything useful right now, because time was in
sys.modules before the test ran, so it's not testing that the .pth
actually imported time.

Note too that unittests should not use assert -- they don't test
anything then in an -O run.  You can use

     self.assert_(self.imported in sys.modules)

instead, and that works fine with -O.


More information about the Python-Dev mailing list