[Python-Dev] More joy with test_strptime

Brett C. bac at OCF.Berkeley.EDU
Tue Jul 13 09:04:03 CEST 2004


Tim Peters wrote:
> [Tim]
[SNIP]
> 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.
> 

Yep.

> 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
> 

[Tim - in another email]
 > Then make sure the .pth file puts self.imported back in sys.modules.

Using that solution solves the problem!  Now I just store a reference 
before it is deleted and have a flag that signifies if it is cleaning up 
to prep for running the test or is playing cleanup from the test having 
been run.  That signifies whether the module being checked for is 
deleted or being restored from the reference stored in the PthFile instance.

> 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.
> 

That's why I called PthFile.cleanup() before even handling the .pth 
file; that deleted the entry from sys.modules before the test ran.

> 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)
> 

The reason I did it that way was because I didn't want the hassle of 
subclassing unittest.TestCase for PthFile.  But with this assert issue I 
just moved that code into the test case class that had the functions 
calling PthFile so I could get access to self.failUnless().  Figured I 
really don't need to have the test code with the PthFile class.

Thanks for your help, Tim.  I will try not to break more code for a 
while.  =)

-Brett


More information about the Python-Dev mailing list