[Python-Dev] Python equivalents in stdlib Was: Include datetime.py in stdlib or not?

Alexander Belopolsky alexander.belopolsky at gmail.com
Fri Jul 9 03:44:48 CEST 2010


On Thu, Jul 8, 2010 at 5:46 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
..
> So include something along the lines of "globals()[obj.__name__] =
> obj" in the name hacking loop to make the test classes more
> discoverable? Good idea.
>

As often happens, a good idea turns quite ugly when facing real world
realities.  I've uploaded a new patch at
http://bugs.python.org/issue7989 and here is what I had to do to make
this work for datetime:

==========
import unittest
import sys; sys.modules['_pickle'] = None
from test.support import import_fresh_module, run_unittest
TESTS = 'test.datetimetester'
pure_tests = import_fresh_module(TESTS, fresh=['datetime', '_strptime', 'time'],
                                 blocked=['_datetime'])
fast_tests = import_fresh_module(TESTS, fresh=['datetime',
                                               '_datetime',
'_strptime', 'time'])

test_modules = [pure_tests, fast_tests]
test_suffixes = ["_Pure", "_Fast"]

globs = globals()
for module, suffix in zip(test_modules, test_suffixes):
    for name, cls in module.__dict__.items():
        if isinstance(cls, type) and issubclass(cls, unittest.TestCase):
            name += suffix
            cls.__name__ = name
            globs[name] = cls
            def setUp(self, module=module, setup=cls.setUp):
                self._save_sys_modules = sys.modules.copy()
                sys.modules[TESTS] = module
                sys.modules['datetime'] = module.datetime_module
                sys.modules['_strptime'] = module.datetime_module._strptime
                setup(self)
            def tearDown(self, teardown=cls.tearDown):
                teardown(self)
                sys.modules = self._save_sys_modules
            cls.setUp = setUp
            cls.tearDown = tearDown

def test_main():
    run_unittest(__name__)
=========

and it still requires that '_pickle' is disabled to pass pickle tests.


More information about the Python-Dev mailing list