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

Alexander Belopolsky alexander.belopolsky at gmail.com
Tue Jul 13 21:52:44 CEST 2010

On Thu, Jul 8, 2010 at 3:09 PM, Brett Cannon <brett at python.org> wrote:
> I can say that all the VM representatives have all said they like the idea.

This is encouraging.  Here is an update on the status of datetime.py.
 I believe it is mostly ready to move from sandbox to py3k/Lib.  The
patch is available on the tracker at
http://bugs.python.org/file17978/issue9206b.diff and on Rietveld at
http://codereview.appspot.com/1824041 . Unfortunately since many
changes are just file renames will minor code modifications, neither
the patch nor Rietveld give a good overview of the proposed commit.  I
will try to give this overview here:

1. Copy datetime.py from sandbox to py3k/Lib.  (I need some help from
an SVN expert on how to do that.)  The diff between sandbox version
and proposed commit is just

--- ../py3k-datetime/datetime.py	2010-07-07 20:12:56.000000000 -0400
+++ Lib/datetime.py	2010-07-08 21:15:52.000000000 -0400
@@ -1555,7 +1555,6 @@
     def strptime(cls, date_string, format):
         'string, format -> new datetime parsed from a string (like
-        import _strptime
         return _strptime._strptime_datetime(cls, date_string, format)

     def utcoffset(self):
@@ -1874,6 +1873,13 @@
 timezone.min = timezone(timezone._minoffset)
 timezone.max = timezone(timezone._maxoffset)

+    from _datetime import *
+except ImportError:
+    pass
+import _strptime
The "from _datetime import *" is the standard fast implementation
override and "import _strptime" had to be moved from function level to
module level after class definitions due to circular dependency of
_strptime on datetime.

The best place to review the entire datetime.py is on Rietveld at
http://codereview.appspot.com/1824041/diff/1/4.  I have only one
remaining issue with this code - it leaves a number of "private" _xyz
functions in the datetime module that are neither used nor overridden
by the C implementation.   In my view, this is not a big issue, but it
can be dealt with by either moving module level functions to class
namespace (making them static or class methods as appropriate) or by
deleting them explicitly in the else clause of the try statement that
imports the fast overrides.

2. Rename datetimemodule.c to _datetimemodule.c. The code changes are
trivial: module name and the name of the init function.

3. Rename test_datetime.py to datetimetester.py.  The testing strategy
implements Nick Coghlan's idea of importing the test classes from a
tester module with and without _datetime and injecting them into
test_datetime namespace under different names.

The changes required to the code are minimal:
http://codereview.appspot.com/1824041/diff/1/5 .

4. Add new test_datetime.py that contains somewhat elaborate import
machinery to create duplicates of each test case from
datetimetester.py running with and without datetime acceleration.

Unfortunately, test_datetime.py did not make it to Rietveld, but it is
small enough to include here:

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

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

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
            globals()[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
            def tearDown(self, teardown=cls.tearDown):
            cls.setUp = setUp
            cls.tearDown = tearDown

def test_main():

if __name__ == "__main__":

Since this change does not introduce any new features, I think it is
safe to commit it and make further improvements to datetime.py (if
any) once it is in the main tree.

More information about the Python-Dev mailing list