summaryrefslogtreecommitdiffstats
path: root/Lib/_pydatetime.py
diff options
context:
space:
mode:
authorPaul Ganssle <git@m.ganssle.io>2023-03-23 15:36:22 (GMT)
committerT. Wouters <thomas@python.org>2023-05-03 09:09:45 (GMT)
commit0a5cd984b215f28d3c205eadf0daf201b3388c90 (patch)
treec516b176dc5295bbdd769c6a0134307f7774a45c /Lib/_pydatetime.py
parent65c4a2b326086875ecbedf032204d1ff24ba74d7 (diff)
downloadcpython-0a5cd984b215f28d3c205eadf0daf201b3388c90.zip
cpython-0a5cd984b215f28d3c205eadf0daf201b3388c90.tar.gz
cpython-0a5cd984b215f28d3c205eadf0daf201b3388c90.tar.bz2
GH-84976: Re-introduce `datetime.py` and fix reprs
Without the change to the reprs, pure-python classes would have a repr of `datetime._pydatetime.time`, etc.
Diffstat (limited to 'Lib/_pydatetime.py')
-rw-r--r--Lib/_pydatetime.py42
1 files changed, 13 insertions, 29 deletions
diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py
index b0eb1c2..f4fc2c5 100644
--- a/Lib/_pydatetime.py
+++ b/Lib/_pydatetime.py
@@ -16,6 +16,13 @@ from operator import index as _index
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()
@@ -706,7 +713,7 @@ class timedelta:
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))
@@ -1016,7 +1023,7 @@ class date:
>>> 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,
@@ -1510,7 +1517,7 @@ class time:
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:
@@ -2065,7 +2072,7 @@ class datetime(date):
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:
@@ -2372,10 +2379,10 @@ class timezone(tzinfo):
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)
@@ -2638,26 +2645,3 @@ _EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc)
# 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__