Issue 1578643: various datetime methods fail in restricted mode (original) (raw)

The following methods fail in restricted execution mode, because they try to import the time module: datetime.datetime.strftime datetime.datetime.strptime datetime.datetime.timetuple datetime.datetime.utctimetuple datetime.time.time datetime.date.timetuple

Example of the problem:

import datetime script = 'print dt; print dt.strftime("x")' exec script in {'dt':datetime.datetime(2006,1,1)} 2006-01-01 00:00:00 x exec script in {'dt':datetime.datetime(2006,1,1),'builtins':{}} 2006-01-01 00:00:00 Traceback (most recent call last): File "", line 1, in ? File "", line 1, in ? KeyError: 'import'

The attached adjusts the datetime module so that the time module is imported in the initialisation of the datetime module. This allows these methods to be used in restricted execution mode.

If the time module is not available, then the datetime module will not be importable either after this patch. Previously the datetime module would be importable but the methods listed above would raise exceptions. If this situation is worth considering then I can adjust the patch accordingly.