Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0a5cd98

Browse filesBrowse files
pganssleYhg1s
authored andcommitted
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.
1 parent 65c4a2b commit 0a5cd98
Copy full SHA for 0a5cd98

File tree

Expand file treeCollapse file tree

3 files changed

+27
-29
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+27
-29
lines changed

‎Lib/_pydatetime.py

Copy file name to clipboardExpand all lines: Lib/_pydatetime.py
+13-29Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
def _cmp(x, y):
1717
return 0 if x == y else 1 if x > y else -1
1818

19+
def _get_class_module(self):
20+
module_name = self.__class__.__module__
21+
if module_name == '_pydatetime':
22+
return 'datetime'
23+
else:
24+
return module_name
25+
1926
MINYEAR = 1
2027
MAXYEAR = 9999
2128
_MAXORDINAL = 3652059 # date.max.toordinal()
@@ -706,7 +713,7 @@ def __repr__(self):
706713
args.append("microseconds=%d" % self._microseconds)
707714
if not args:
708715
args.append('0')
709-
return "%s.%s(%s)" % (self.__class__.__module__,
716+
return "%s.%s(%s)" % (_get_class_module(self),
710717
self.__class__.__qualname__,
711718
', '.join(args))
712719

@@ -1016,7 +1023,7 @@ def __repr__(self):
10161023
>>> repr(dt)
10171024
'datetime.datetime(2010, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)'
10181025
"""
1019-
return "%s.%s(%d, %d, %d)" % (self.__class__.__module__,
1026+
return "%s.%s(%d, %d, %d)" % (_get_class_module(self),
10201027
self.__class__.__qualname__,
10211028
self._year,
10221029
self._month,
@@ -1510,7 +1517,7 @@ def __repr__(self):
15101517
s = ", %d" % self._second
15111518
else:
15121519
s = ""
1513-
s= "%s.%s(%d, %d%s)" % (self.__class__.__module__,
1520+
s= "%s.%s(%d, %d%s)" % (_get_class_module(self),
15141521
self.__class__.__qualname__,
15151522
self._hour, self._minute, s)
15161523
if self._tzinfo is not None:
@@ -2065,7 +2072,7 @@ def __repr__(self):
20652072
del L[-1]
20662073
if L[-1] == 0:
20672074
del L[-1]
2068-
s = "%s.%s(%s)" % (self.__class__.__module__,
2075+
s = "%s.%s(%s)" % (_get_class_module(self),
20692076
self.__class__.__qualname__,
20702077
", ".join(map(str, L)))
20712078
if self._tzinfo is not None:
@@ -2372,10 +2379,10 @@ def __repr__(self):
23722379
if self is self.utc:
23732380
return 'datetime.timezone.utc'
23742381
if self._name is None:
2375-
return "%s.%s(%r)" % (self.__class__.__module__,
2382+
return "%s.%s(%r)" % (_get_class_module(self),
23762383
self.__class__.__qualname__,
23772384
self._offset)
2378-
return "%s.%s(%r, %r)" % (self.__class__.__module__,
2385+
return "%s.%s(%r, %r)" % (_get_class_module(self),
23792386
self.__class__.__qualname__,
23802387
self._offset, self._name)
23812388

@@ -2638,26 +2645,3 @@ def _name_from_offset(delta):
26382645
# small dst() may get within its bounds; and it doesn't even matter if some
26392646
# perverse time zone returns a negative dst()). So a breaking case must be
26402647
# pretty bizarre, and a tzinfo subclass can override fromutc() if it is.
2641-
2642-
try:
2643-
from _datetime import *
2644-
except ImportError:
2645-
pass
2646-
else:
2647-
# Clean up unused names
2648-
del (_DAYNAMES, _DAYS_BEFORE_MONTH, _DAYS_IN_MONTH, _DI100Y, _DI400Y,
2649-
_DI4Y, _EPOCH, _MAXORDINAL, _MONTHNAMES, _build_struct_time,
2650-
_check_date_fields, _check_time_fields,
2651-
_check_tzinfo_arg, _check_tzname, _check_utc_offset, _cmp, _cmperror,
2652-
_date_class, _days_before_month, _days_before_year, _days_in_month,
2653-
_format_time, _format_offset, _index, _is_leap, _isoweek1monday, _math,
2654-
_ord2ymd, _time, _time_class, _tzinfo_class, _wrap_strftime, _ymd2ord,
2655-
_divide_and_round, _parse_isoformat_date, _parse_isoformat_time,
2656-
_parse_hh_mm_ss_ff, _IsoCalendarDate, _isoweek_to_gregorian,
2657-
_find_isoformat_datetime_separator, _FRACTION_CORRECTION,
2658-
_is_ascii_digit)
2659-
# XXX Since import * above excludes names that start with _,
2660-
# docstring does not get overwritten. In the future, it may be
2661-
# appropriate to maintain a single module level docstring and
2662-
# remove the following line.
2663-
from _datetime import __doc__

‎Lib/datetime.py

Copy file name to clipboard
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
try:
2+
from _datetime import *
3+
from _datetime import __doc__
4+
except ImportError:
5+
from _pydatetime import *
6+
from _pydatetime import __doc__
7+
8+
__all__ = ("date", "datetime", "time", "timedelta", "timezone", "tzinfo",
9+
"MINYEAR", "MAXYEAR")
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Create a new ``Lib/_pydatetime.py`` file that defines the Python version of
2+
the ``datetime`` module, and make ``datetime`` import the contents of the
3+
new library only if the C implementation is missing. Currently, the full
4+
Python implementation is defined and then deleted if the C implementation is
5+
not available, slowing down ``import datetime`` unnecessarily.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.