[Python-Dev] More joy with test_strptime (original) (raw)

Brett C. bac at OCF.Berkeley.EDU
Mon Jul 12 20:30:08 CEST 2004


Tim Peters wrote:

[Tim] ...

So now I'm suspecting that the tricks in PthFile are managing to create a second instance of the time module not related to the instance of the time module teststrptime is monkey-patching with its

time.tzname = (tzname, tzname) time.daylight = 1 tricks. Using the attached patch yields output test_all_ testsite teststrptime in strptime ('Eastern Standard Time', 'Eastern Daylight Time') 1 in testbadtimezone ('Eastern Standard Time', 'Eastern Standard Time') 1 in strptime ('Eastern Standard Time', 'Eastern Daylight Time') 1 ... so teststrptime's attempt to monkey-patch the time module indeed has no effect on the time module strptime.py is using.

Nuts. Some code somewhere must be importing strptime through time and that is holding a reference to the original copy of 'time' that was imported and then when test_site deletes sys.modules["time"] test_strptime imports 'time' again since it can't find a reference and uses that to monkey-patch which _strptime itself never sees since it holds the reference to the original imported 'time'.

And I just re-read your previous email and you came to essentially the same conclusion as above.

This just figures; only I could break my own test with another of my own tests.

The problem can be fixed by using this as the first line of testbadtimezone:

from strptime import time But, of course, that's insane.

But if so, I'm at a loss to explain why this would only happen on Windows. Still so.

Stop wondering; it happens on OS X as well.

OK, so the question becomes how can I have test_site (which I will fix with your suggestions for the removal of existing files and directories; when I rewrote that chunk of code I had EAFP on the brain) test to make sure that a module is imported? Is there any other way than to just try out another module that I think no one else is going mess with that has no importation side-effects? I guess I could have test_site just import site again in the .pth file. Could possibly use _strptime since I know I don't mess with that in any way. But is there some other way to check that I am not thinking of?

-Brett



More information about the Python-Dev mailing list