Skip to content

Commit

Permalink
pythonGH-84976: Re-introduce datetime.py and fix reprs
Browse files Browse the repository at this point in the history
Without the change to the reprs, pure-python classes would have a repr
of `datetime._pydatetime.time`, etc.
  • Loading branch information
pganssle committed Apr 27, 2023
1 parent 62aa997 commit 937b144
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
42 changes: 13 additions & 29 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
def _cmp(x, y):
return 0 if x == y else 1 if x > y else -1

def _get_class_module(self):
module_name = self.__class__.__module__
if module_name == '_pydatetime':
return 'datetime'
else:
return module_name

MINYEAR = 1
MAXYEAR = 9999
_MAXORDINAL = 3652059 # date.max.toordinal()
Expand Down Expand Up @@ -706,7 +713,7 @@ def __repr__(self):
args.append("microseconds=%d" % self._microseconds)
if not args:
args.append('0')
return "%s.%s(%s)" % (self.__class__.__module__,
return "%s.%s(%s)" % (_get_class_module(self),
self.__class__.__qualname__,
', '.join(args))

Expand Down Expand Up @@ -1016,7 +1023,7 @@ def __repr__(self):
>>> repr(dt)
'datetime.datetime(2010, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)'
"""
return "%s.%s(%d, %d, %d)" % (self.__class__.__module__,
return "%s.%s(%d, %d, %d)" % (_get_class_module(self),
self.__class__.__qualname__,
self._year,
self._month,
Expand Down Expand Up @@ -1510,7 +1517,7 @@ def __repr__(self):
s = ", %d" % self._second
else:
s = ""
s= "%s.%s(%d, %d%s)" % (self.__class__.__module__,
s= "%s.%s(%d, %d%s)" % (_get_class_module(self),
self.__class__.__qualname__,
self._hour, self._minute, s)
if self._tzinfo is not None:
Expand Down Expand Up @@ -2065,7 +2072,7 @@ def __repr__(self):
del L[-1]
if L[-1] == 0:
del L[-1]
s = "%s.%s(%s)" % (self.__class__.__module__,
s = "%s.%s(%s)" % (_get_class_module(self),
self.__class__.__qualname__,
", ".join(map(str, L)))
if self._tzinfo is not None:
Expand Down Expand Up @@ -2372,10 +2379,10 @@ def __repr__(self):
if self is self.utc:
return 'datetime.timezone.utc'
if self._name is None:
return "%s.%s(%r)" % (self.__class__.__module__,
return "%s.%s(%r)" % (_get_class_module(self),
self.__class__.__qualname__,
self._offset)
return "%s.%s(%r, %r)" % (self.__class__.__module__,
return "%s.%s(%r, %r)" % (_get_class_module(self),
self.__class__.__qualname__,
self._offset, self._name)

Expand Down Expand Up @@ -2638,26 +2645,3 @@ def _name_from_offset(delta):
# small dst() may get within its bounds; and it doesn't even matter if some
# perverse time zone returns a negative dst()). So a breaking case must be
# pretty bizarre, and a tzinfo subclass can override fromutc() if it is.

try:
from _datetime import *
except ImportError:
pass
else:
# Clean up unused names
del (_DAYNAMES, _DAYS_BEFORE_MONTH, _DAYS_IN_MONTH, _DI100Y, _DI400Y,
_DI4Y, _EPOCH, _MAXORDINAL, _MONTHNAMES, _build_struct_time,
_check_date_fields, _check_time_fields,
_check_tzinfo_arg, _check_tzname, _check_utc_offset, _cmp, _cmperror,
_date_class, _days_before_month, _days_before_year, _days_in_month,
_format_time, _format_offset, _index, _is_leap, _isoweek1monday, _math,
_ord2ymd, _time, _time_class, _tzinfo_class, _wrap_strftime, _ymd2ord,
_divide_and_round, _parse_isoformat_date, _parse_isoformat_time,
_parse_hh_mm_ss_ff, _IsoCalendarDate, _isoweek_to_gregorian,
_find_isoformat_datetime_separator, _FRACTION_CORRECTION,
_is_ascii_digit)
# XXX Since import * above excludes names that start with _,
# docstring does not get overwritten. In the future, it may be
# appropriate to maintain a single module level docstring and
# remove the following line.
from _datetime import __doc__
9 changes: 9 additions & 0 deletions Lib/datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
try:
from _datetime import *
from _datetime import __doc__
except ImportError:
from _pydatetime import *
from _pydatetime import __doc__

__all__ = ("date", "datetime", "time", "timedelta", "timezone", "tzinfo",
"MINYEAR", "MAXYEAR")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Create a new ``Lib/_pydatetime.py`` file that defines the Python version of
the ``datetime`` module, and make ``datetime`` import the contents of the
new library only if the C implementation is missing. Currently, the full
Python implementation is defined and then deleted if the C implementation is
not available, slowing down ``import datetime`` unnecessarily.

0 comments on commit 937b144

Please sign in to comment.